-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: ✨ OpenAI parser * refinement in logic * logic refinement * Update README.md Co-authored-by: Glenn Matthews <[email protected]> * Update circuit_maintenance_parser/parser.py Co-authored-by: Glenn Matthews <[email protected]> * improve question * make more explicit the text parsing * Automate token management for local tests * Make openai library an extra * Adopt OpenAI library changes * fix mypy --------- Co-authored-by: Glenn Matthews <[email protected]>
- Loading branch information
1 parent
e611c91
commit 13dfea4
Showing
12 changed files
with
862 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""OpenAI Parser.""" | ||
import os | ||
import logging | ||
import json | ||
from typing import List, Optional | ||
|
||
try: | ||
from openai import OpenAI # type: ignore | ||
except ImportError: | ||
_HAS_OPENAI = False | ||
else: | ||
_HAS_OPENAI = True | ||
|
||
from circuit_maintenance_parser.parser import LLM | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class OpenAIParser(LLM): | ||
"""Notifications Parser powered by OpenAI ChatGPT.""" | ||
|
||
def get_llm_response(self, content) -> Optional[List]: | ||
"""Get LLM processing from OpenAI.""" | ||
if not _HAS_OPENAI: | ||
raise ImportError("openai extra is required to use OpenAIParser.") | ||
|
||
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | ||
model = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo") | ||
try: | ||
response = client.chat.completions.create( | ||
model=model, | ||
messages=[ | ||
{ # type: ignore | ||
"role": "system", | ||
"content": self._llm_question, | ||
}, | ||
{ # type: ignore | ||
"role": "user", | ||
"content": content, | ||
}, | ||
], | ||
) | ||
|
||
# TODO: Maybe asking again about the generated response could refine it | ||
|
||
except Exception as err: # pylint: disable=broad-exception-caught | ||
logger.error(err) | ||
return None | ||
|
||
logger.info("Used OpenAI tokens: %s", response.usage) | ||
generated_text = response.choices[0].message.content | ||
logger.info("Response from LLM: %s", generated_text) | ||
try: | ||
return json.loads(generated_text) # type: ignore | ||
except ValueError as err: | ||
logger.error(err) | ||
return None | ||
|
||
return None |
Oops, something went wrong.