This commit is contained in:
Cyberes 2023-03-19 19:46:07 -06:00
parent d92506d84f
commit 98805b1878
1 changed files with 6 additions and 8 deletions

View File

@ -2,7 +2,6 @@ import asyncio
import functools import functools
import logging import logging
import time import time
import traceback
from types import ModuleType from types import ModuleType
from typing import List, Optional, Union from typing import List, Optional, Union
@ -183,7 +182,7 @@ async def process_chat(
return await loop.run_in_executor(None, functools.partial(openai_obj.ChatCompletion.create, model=openai_model, messages=messages, temperature=openai_temperature, timeout=20)) return await loop.run_in_executor(None, functools.partial(openai_obj.ChatCompletion.create, model=openai_model, messages=messages, temperature=openai_temperature, timeout=20))
# r = openai_obj.ChatCompletion.create(model=openai_model, messages=messages, temperature=openai_temperature, timeout=20) # r = openai_obj.ChatCompletion.create(model=openai_model, messages=messages, temperature=openai_temperature, timeout=20)
text_response = response = None response = None
for i in range(openai_retries): for i in range(openai_retries):
try: try:
task = asyncio.create_task(generate(timeout=20)) task = asyncio.create_task(generate(timeout=20))
@ -192,17 +191,14 @@ async def process_chat(
if response is not None: if response is not None:
break break
except Exception as e: # (stopit.utils.TimeoutException, openai.error.APIConnectionError) except Exception as e: # (stopit.utils.TimeoutException, openai.error.APIConnectionError)
logger.warning(f'Got exception when trying to reply to event {event.event_id}, retrying: {e}') logger.warning(f'Got exception when generating response to event {event.event_id}, retrying: {e}')
await client.room_typing(room.room_id, typing_state=True, timeout=15000) await client.room_typing(room.room_id, typing_state=True, timeout=15000)
time.sleep(2) time.sleep(2)
continue continue
except Exception as e:
logger.critical(f'OpenAI API error: {e}\n{traceback.format_exc()}')
await react_to_event(client, room.room_id, event.event_id, '')
return
if response is None: if response is None:
logger.critical(f'OpenAI API timeout for event {event.event_id} in room {room.room_id}.') logger.critical(f'Could not generate response to event {event.event_id} in room {room.room_id}.')
await client.room_typing(room.room_id, typing_state=False, timeout=15000)
await react_to_event(client, room.room_id, event.event_id, '') await react_to_event(client, room.room_id, event.event_id, '')
return return
text_response = response["choices"][0]["message"]["content"].strip().strip('\n') text_response = response["choices"][0]["message"]["content"].strip().strip('\n')
@ -222,6 +218,8 @@ async def process_chat(
resp = await send_text_to_room(client, room.room_id, text_response, reply_to_event_id=event.event_id, thread=True, thread_root_id=thread_root_id if thread_root_id else event.event_id) resp = await send_text_to_room(client, room.room_id, text_response, reply_to_event_id=event.event_id, thread=True, thread_root_id=thread_root_id if thread_root_id else event.event_id)
await client.room_typing(room.room_id, typing_state=False, timeout=3000) await client.room_typing(room.room_id, typing_state=False, timeout=3000)
print(thread_root_id)
store.add_event_id(event.event_id) store.add_event_id(event.event_id)
if not isinstance(resp, RoomSendResponse): if not isinstance(resp, RoomSendResponse):
logger.critical(f'Failed to respond to event {event.event_id} in room {room.room_id}:\n{vars(resp)}') logger.critical(f'Failed to respond to event {event.event_id} in room {room.room_id}:\n{vars(resp)}')