From 554d3d8aa0b5d2903ea6cf1b717163e635b39d4c Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 19 Nov 2023 17:02:40 +0100 Subject: [PATCH] Refactor OpenAI class methods to retrieve assistant and thread IDs. --- src/gptbot/classes/openai.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/gptbot/classes/openai.py b/src/gptbot/classes/openai.py index cc9adec..68e2c7d 100644 --- a/src/gptbot/classes/openai.py +++ b/src/gptbot/classes/openai.py @@ -120,6 +120,42 @@ class OpenAI: return assistant_id, thread_id + async def get_assistant_id(self, room: str) -> str: + """Get the assistant ID for a room. + + Args: + room (str): The room to get the assistant ID for. + + Returns: + str: The assistant ID. + """ + with closing(self.bot.database.cursor()) as cursor: + cursor.execute("SELECT value FROM room_settings WHERE room_id = ? AND setting = ?", (room, "openai_assistant")) + result = cursor.fetchone() + + if result is None: + raise Exception("No assistant ID found for room.") + + return result[0] + + async def get_thread_id(self, room: str) -> str: + """Get the thread ID for a room. + + Args: + room (str): The room to get the thread ID for. + + Returns: + str: The thread ID. + """ + with closing(self.bot.database.cursor()) as cursor: + cursor.execute("SELECT value FROM room_settings WHERE room_id = ? AND setting = ?", (room, "openai_thread")) + result = cursor.fetchone() + + if result is None: + raise Exception("No thread ID found for room.") + + return result[0] + async def generate_assistant_response(self, messages: List[Dict[str, str]], room: str, user: Optional[str] = None) -> Tuple[str, int]: """Generate a response to a chat message using an assistant.