feat(logging): enhance debug logging for message processing

Added debug logging to capture incoming, prepared, and truncated messages in the OpenAI class. Also, included logging for last messages fetched in the bot class. These additions aid in the traceability and debugging of message flows and processing errors.

Additionally, an option to log detailed error tracebacks in debug mode was implemented to facilitate better error analysis.
This commit is contained in:
Kumi 2024-08-04 18:28:12 +02:00
parent ca7245696a
commit 48decdc9e2
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 13 additions and 1 deletions

View file

@ -173,6 +173,8 @@ class OpenAI(BaseAI):
) -> List[Any]:
chat_messages = []
self.logger.log(f"Incoming messages: {messages}", "debug")
for message in messages:
if isinstance(message, (RoomMessageNotice, RoomMessageText)):
role = (
@ -390,6 +392,8 @@ class OpenAI(BaseAI):
)
chat_messages.append({"role": "system", "content": message_body})
self.logger.log(f"Prepared messages: {chat_messages}", "debug")
# Truncate messages to fit within the token limit
self._truncate(
messages=chat_messages,
@ -450,6 +454,8 @@ class OpenAI(BaseAI):
total_tokens = len(system_message) + 1
truncated_messages = []
self.logger.log(f"Messages: {messages}", "debug")
for message in [messages[0]] + list(reversed(messages[1:])):
content = (
message["content"]

View file

@ -1103,6 +1103,7 @@ class GPTBot:
last_messages = await self._last_n_messages(
room.room_id, self.chat_api.max_messages
)
self.logger.log(f"Last messages: {last_messages}", "debug")
except Exception as e:
self.logger.log(f"Error getting last messages: {e}", "error")
await self.send_message(
@ -1113,7 +1114,7 @@ class GPTBot:
system_message = self.get_system_message(room)
chat_messages = await self.chat_api.prepare_messages(
last_messages, system_message
event, last_messages, system_message
)
# Check for a model override
@ -1158,6 +1159,11 @@ class GPTBot:
await self.send_message(
room, "Something went wrong generating audio file.", True
)
if self.debug:
await self.send_message(
room, f"Error: {e}\n\n```\n{traceback.format_exc()}\n```", True
)
await self.send_message(room, response)