From a80eb6d48466640ee13b65841e1d1dea4b1a8b11 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Sun, 19 Mar 2023 15:52:15 -0600 Subject: [PATCH] catch timeout --- matrix_gpt/bot/callbacks.py | 2 +- matrix_gpt/bot/chat_functions.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/matrix_gpt/bot/callbacks.py b/matrix_gpt/bot/callbacks.py index 86f71b8..39bbf79 100644 --- a/matrix_gpt/bot/callbacks.py +++ b/matrix_gpt/bot/callbacks.py @@ -61,7 +61,7 @@ class Callbacks: # Extract the message text msg = event.body.strip().strip('\n') - logger.debug(f"Bot message received from {event.sender} in {room} --> {msg}") + logger.debug(f"Bot message received from {event.sender} in {room.room_id} --> {msg}") await self.client.room_read_markers(room.room_id, event.event_id, event.event_id) diff --git a/matrix_gpt/bot/chat_functions.py b/matrix_gpt/bot/chat_functions.py index 694bd82..e8ea2ae 100644 --- a/matrix_gpt/bot/chat_functions.py +++ b/matrix_gpt/bot/chat_functions.py @@ -157,16 +157,20 @@ async def process_chat(client, room, event, command, store, openai_obj: ModuleTy logger.debug(f'Generating reply to event {event.event_id}') + # I don't think the OpenAI py api has a built-in timeout @stopit.threading_timeoutable(default=(None, None)) def generate(): - response = openai_obj.ChatCompletion.create(model=openai_model, messages=messages, temperature=0, timeout=10) - return response["choices"][0]["message"]["content"].strip().strip('\n'), response + r = openai_obj.ChatCompletion.create(model=openai_model, messages=messages, temperature=0, timeout=10) + return r["choices"][0]["message"]["content"].strip().strip('\n'), r text_response = response = None for i in range(openai_retries): - text_response, response = generate(timeout=5) - if text_response is not None: - break + try: + text_response, response = generate(timeout=5) + if text_response is not None: + break + except stopit.utils.TimeoutException: + continue if text_response is None: logger.critical(f'OpenAI API timeout for event {event.event_id} in room {room.room_id}.') await react_to_event(client, room.room_id, event.event_id, '❌')