-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ✨ OpenAI parser #245
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3aba61f
feat: :sparkles: OpenAI parser
chadell a4eec2e
refinement in logic
chadell e7ace76
logic refinement
chadell c98bf4c
Update README.md
chadell 0f9dcf6
Update circuit_maintenance_parser/parser.py
chadell 5eec491
improve question
chadell 6094f3f
make more explicit the text parsing
chadell 86f8154
Automate token management for local tests
chadell de559e8
Make openai library an extra
chadell 38c43bf
Adopt OpenAI library changes
chadell 1656bef
fix mypy
chadell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What purpose does this method serve? When would subclasses need/want to override it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it more explicit why it is used. the final parsers could have a different way to decode bytes to string