-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Still-Human/jinyang
Jinyang
- Loading branch information
Showing
18 changed files
with
267 additions
and
334 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
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 |
---|---|---|
@@ -1,32 +1,76 @@ | ||
import json | ||
import logging | ||
from typing import Any | ||
from openai import OpenAI | ||
import os | ||
|
||
from langchain.schema import HumanMessage, SystemMessage | ||
from langchain_openai import ChatOpenAI | ||
|
||
from app.exceptions.exception import InferenceFailure | ||
from app.llm.base import LLMBaseModel, LLMConfig | ||
from app.prompts.config import PromptMessageConfig | ||
from app.prompts.examiner.functions import PracticeFunctions, get_practice_functions | ||
from app.prompts.summariser.functions import get_summary_functions, SummaryFunctions | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") | ||
|
||
class OpenAI(LLMBaseModel): | ||
class OpenAi(LLMBaseModel): | ||
"""This class handles the interaction with OpenAI API.""" | ||
|
||
|
||
def __init__(self, model_name: str, model_config: LLMConfig): | ||
super().__init__(model_name=model_name, model_config=model_config) | ||
self._model = ChatOpenAI( | ||
model_name=model_name, | ||
temperature=model_config.temperature, | ||
max_tokens=model_config.max_tokens, | ||
self._client = OpenAI( | ||
api_key=OPENAI_API_KEY, | ||
) | ||
|
||
|
||
async def send_message(self, system_message: str, user_message: str) -> str: | ||
async def send_message(self, system_message: str, user_message: str, config: PromptMessageConfig) -> Any: | ||
"""Sends a message to OpenAI and returns the response.""" | ||
messages = [ | ||
SystemMessage(content=system_message), | ||
HumanMessage(content=user_message), | ||
] | ||
|
||
log.info(f"Sending messages to OpenAI") | ||
response = (await self._model.ainvoke(messages)).content | ||
return response | ||
match config: | ||
case PromptMessageConfig.SUMMARY: | ||
response = self._client.chat.completions.create( | ||
model = self._model_name, | ||
messages = [ | ||
{"role": "system", "content": system_message}, | ||
{"role": "user", "content": user_message} | ||
], | ||
functions=get_summary_functions(), | ||
function_call = {"name": SummaryFunctions.GET_SUMMARY} | ||
) | ||
try: | ||
json_response: dict[str, str] = json.loads(response.choices[0].message.function_call.arguments) | ||
topic: str = json_response[SummaryFunctions.TOPIC] | ||
content: str = json_response[SummaryFunctions.CONTENT] | ||
log.info(f"Topic: {topic}, Content: {content}") | ||
return (topic, content) | ||
except Exception as e: | ||
log.error(f"Error processing or receiving OpenAI response: {str(e)}") | ||
raise InferenceFailure("Error processing OpenAI response") | ||
case PromptMessageConfig.PRACTICE: | ||
response = self._client.chat.completions.create( | ||
model = self._model_name, | ||
messages = [ | ||
{"role": "system", "content": system_message}, | ||
{"role": "user", "content": user_message} | ||
], | ||
functions=get_practice_functions(), | ||
function_call = {"name": PracticeFunctions.GET_PRACTICE} | ||
) | ||
try: | ||
json_response: dict[str, str] = json.loads(response.choices[0].message.function_call.arguments) | ||
log.info(f"Practice: {json_response}") | ||
language: str = json_response[PracticeFunctions.LANGUAGE] | ||
question: str = json_response[PracticeFunctions.QUESTION] | ||
half_completed_code: str = json_response[PracticeFunctions.HALF_COMPLETED_CODE] | ||
fully_completed_code: str = json_response[PracticeFunctions.FULLY_COMPLETED_CODE] | ||
log.info(f"Language: {language}, Question: {question}, Half-completed-code: {half_completed_code}, Fully-completed-code: {fully_completed_code}") | ||
return (language, question, half_completed_code, fully_completed_code) | ||
except Exception as e: | ||
log.error(f"Error processing or receiving OpenAI response: {str(e)}") | ||
raise InferenceFailure("Error processing OpenAI response") | ||
case _: | ||
raise InferenceFailure("Invalid config type") | ||
|
||
|
Oops, something went wrong.