Skip to content

Commit

Permalink
switching to real logger
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanDietzMorris committed Nov 7, 2024
1 parent cade63b commit 49744a2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions parsers/LitCoin/src/bagel/bagel_gpt.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import json
import os
from collections import defaultdict

from Common.config import CONFIG
from Common.utils import LoggingUtil

OPENAI_API_KEY = CONFIG.get("OPENAI_API_KEY")

LLM_RESULTS = []


logger = LoggingUtil.init_logging("ORION.Common.BagelGPT",
line_format='medium',
log_file_path=os.environ['ORION_LOGS'])

def ask_classes_and_descriptions(text, term, termlist, abstract_id, requests_session):
"""Get GPT results based only on the labels of the terms."""

Expand Down Expand Up @@ -100,19 +106,19 @@ def query(prompt, requests_session):

response = requests_session.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
if response.status_code != 200:
print(f'openai call returned non-200 status: {response.status_code}')
logger.error(f'openai call returned non-200 status: {response.status_code}')
response.raise_for_status()
try:
content = response.json()["choices"][0]["message"]["content"]
# print(content)
except KeyError as k:
print(f'openai json did not contain expected key {k}: {response.json()}')
logger.warning(f'openai json did not contain expected key {k}: {response.json()}')
raise k

try:
chunk = content[content.index("["):(content.rindex("]") + 1)]
output = json.loads(chunk)
except (json.JSONDecodeError, ValueError) as e:
print(f'openai results did not contain valid json chunk: {content}')
logger.warning(f'openai results did not contain valid json chunk: {content}')
raise e
return output

0 comments on commit 49744a2

Please sign in to comment.