Skip to content

Commit

Permalink
Merge pull request #33 from Theodo-UK/feat/console-log-fusion
Browse files Browse the repository at this point in the history
Fuse the data from the request and the response console logs
  • Loading branch information
Lerri-Cofannos authored Aug 22, 2023
2 parents 9573f15 + 8a43935 commit 7b0e0a1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 91 deletions.
42 changes: 26 additions & 16 deletions sdk-python/omnilogger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,58 @@ def start_listener(url: str):
sys.stderr = StreamToLogger(logger, logging.ERROR)


def handle_openai_response(data: str, url: str):
output = data.split("body='")[1].split("' headers=")[0]
output = bytes(output, "utf-8").decode("unicode_escape")
output = json.loads(output)

log = {
"input": "This is a new dummy input!",
"datetime_utc": datetime.utcfromtimestamp(output["created"]),
"output": output["choices"][0]["text"],
"total_tokens": output["usage"]["total_tokens"],
}
send_to_db(url, log)


class OpenAIFilter(logging.Filter):
url: str
prompt: str

def __init__(self, url):
self.url = url
self.prompt = ""

def filter(self, record) -> bool:
msg = record.getMessage()

if "OpenAI" in msg or "openai" in msg:
if "OpenAI" in msg or "openai" in msg or "text-davinci-003" in msg:
if "message='Request to OpenAI API'" in msg:
return False

if "message='Post details'" in msg:
try:
self.extract_prompt_from_request(msg)
except IndexError:
record.msg = "Prompt not found in OpenAI request: " + msg
return True
return False

if "message='OpenAI API response'" in msg:
return False

if "message='API response body'" in msg:
try:
handle_openai_response(msg, self.url)
self.handle_openai_response(msg)
except json.decoder.JSONDecodeError:
record.msg = "JSONDecodeError for OpenAI response: " + msg
return True
return False
return True

def extract_output_from_response(self, data: str):
output = data.split("body='")[1].split("' headers=")[0]
output = bytes(output, "utf-8").decode("unicode_escape")
output = json.loads(output)

log = {
"input": self.prompt,
"datetime_utc": datetime.utcfromtimestamp(output["created"]),
"output": output["choices"][0]["text"],
"total_tokens": output["usage"]["total_tokens"],
}
send_to_db(self.url, log)

def extract_prompt_from_request(self, data: str):
self.prompt = data.split('"prompt": "')[1]
self.prompt = self.prompt.split('", "temperature"')[0]


class StreamToLogger(object):
"""
Expand Down
89 changes: 16 additions & 73 deletions sdk-python/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sdk-python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ classifiers = [
]

[tool.poetry.dependencies]
python = "^3.7.1"
python = "^3.8"
psycopg2-binary = "^2.9.7"
openai = "^0.27.8"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
python-dotenv = "^1.0.0"

[project.urls]
"Homepage" = "https://github.com/Theodo-UK/OmniLog"
Expand Down
2 changes: 1 addition & 1 deletion sdk-python/test/openai_filter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_filter_post_details(self):
When it is processed by the filter,
then the record is removed from the stream.
"""
self.input = "message='Post details' path=https://api.openai.com"
self.input = 'message=\'Post details\' path=https://api.openai.com body={"prompt": "This is a prompt"}'
self.compute_result("test_filter_post_details")
self.assertEqual(self.result, "")

Expand Down
18 changes: 18 additions & 0 deletions sdk-python/test/playground.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from dotenv import load_dotenv
import openai

from omnilogger import start_listener

load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

start_listener(DATABASE_URL)

openai.api_key = OPENAI_API_KEY
openai.Completion.create(
model="text-davinci-003",
prompt="How are rubber ducks used in IT",
temperature=0.6,
)

0 comments on commit 7b0e0a1

Please sign in to comment.