diff --git a/backend/flashcards/textToFlashcards.py b/backend/flashcards/textToFlashcards.py index c044df09..9fd5dedf 100644 --- a/backend/flashcards/textToFlashcards.py +++ b/backend/flashcards/textToFlashcards.py @@ -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: """ @@ -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)