catch timeout

This commit is contained in:
Cyberes 2023-03-19 15:52:15 -06:00
parent e3c0f15744
commit a80eb6d484
2 changed files with 10 additions and 6 deletions

View File

@ -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)

View File

@ -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):
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, '')