Skip to content

Commit

Permalink
refactor: add a interface for chat completions
Browse files Browse the repository at this point in the history
Co-authored-by: Kaamya Shinde <[email protected]>
  • Loading branch information
Knolaisen and kaamyashinde committed Mar 5, 2024
1 parent c2a3e92 commit 4f18272
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions backend/flashcards/textToFlashcards.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
import openai
from config import Config
from dataclasses import dataclass
from abc import ABC, ABCMeta, abstractmethod

api_key = Config().API_KEY

openai.api_key = api_key
sample_info = "Cristiano Ronaldo dos Santos Aveiro GOIH ComM (Portuguese pronunciation: [kɾiʃˈtjɐnu ʁɔˈnaldu]; born 5 February 1985) is a Portuguese professional footballer who plays as a forward for and captains both Saudi Pro League club Al Nassr and the Portugal national team. Widely regarded as one of the greatest players of all-time, Ronaldo has won five Ballon d'Or awards,[note 3] a record three UEFA Men's Player of the Year Awards, and four European Golden Shoes, the most by a European player. Kaamya does not like coffe. Kristoffer does like coffee. "

class FlashcardGenerator(ABC):
@abstractmethod
def request_chat_completion(self, role: str, message: str) -> list[str]:
"""
Takes a role and a message and returns a response from a LLM
"""

def request_chat_completion(role: str = "system", message: str = "") -> list[str]:
"""
Returns a response from the OpenAI API
Args:
role (str, optional): The role of the message. Defaults to "system".
message (str, optional): The message to be sent. Defaults to "".
pass

Returns:
response list[str]: The response from the OpenAI API
if empty string, an error has occured
"""
result = ""
if message == "":
result = "Error: No message provided"
else:
response = openai.chat.completions.create(
model="gpt-4",
messages=[
{"role": role, "content": message}
]
)
result = response.choices[0].message.content
return result
class OpenAIFlashcardGenerator(FlashcardGenerator):
def request_chat_completion(role: str = "system", message: str = "") -> list[str]:
"""
Returns a response from the OpenAI API
Args:
role (str, optional): The role of the message. Defaults to "system".
message (str, optional): The message to be sent. Defaults to "".
Returns:
response list[str]: The response from the OpenAI API
if empty string, an error has occured
"""
result = ""
if message == "":
result = "Error: No message provided"
else:
response = openai.chat.completions.create(
model="gpt-4",
messages=[
{"role": role, "content": message}
]
)
result = response.choices[0].message.content
return result

def generate_template(sample_info: str = sample_info) -> str:
"""
Expand All @@ -54,7 +64,7 @@ def generate_flashcards(sample_info: str = sample_info) -> list[str]:
list: The list of flashcards generated from the sample text
"""
template = generate_template(sample_info)
response = request_chat_completion("system", message=template)
response = OpenAIFlashcardGenerator.request_chat_completion("system", message=template)
response = response.split("|")
return parse_flashcard(response)

Expand Down

0 comments on commit 4f18272

Please sign in to comment.