Optimize chat model and message handling

Refactored initialization of OpenAI APIs to correct a redundancy and
enhance clarity. Improved content extraction logic for robust handling
of various message types. Enhanced logging and messaging by including
user and room context, facilitating better debugging and user
interaction. Extended `send_message` to support custom message types,
allowing for richer interaction within the chat ecosystem. Updated
hardcoded chat models to leverage newer versions for potentially more
accurate tool overrides. Fixed async method call in recursion handling
to ensure proper response generation. Finally, increased message history
retrieval limit based on the `max_messages` attribute for more effective
conversation context.

Resolves issues with message context and enhances user feedback during
operations.
This commit is contained in:
Kumi 2024-02-14 17:45:57 +01:00
parent b65dcc7d83
commit 10b74187eb
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 48 additions and 22 deletions

View file

@ -229,9 +229,9 @@ class GPTBot:
if Path(bot.logo_path).exists() and Path(bot.logo_path).is_file(): if Path(bot.logo_path).exists() and Path(bot.logo_path).is_file():
bot.logo = Image.open(bot.logo_path) bot.logo = Image.open(bot.logo_path)
bot.chat_api = ( bot.chat_api = bot.image_api = bot.classification_api = bot.tts_api = (
bot.image_api bot.stt_api
) = bot.classification_api = bot.tts_api = bot.stt_api = OpenAI( ) = OpenAI(
bot=bot, bot=bot,
api_key=config["OpenAI"]["APIKey"], api_key=config["OpenAI"]["APIKey"],
chat_model=config["OpenAI"].get("Model"), chat_model=config["OpenAI"].get("Model"),
@ -377,9 +377,11 @@ class GPTBot:
content = ( content = (
message["content"] message["content"]
if isinstance(message["content"], str) if isinstance(message["content"], str)
else message["content"][0]["text"] else (
if isinstance(message["content"][0].get("text"), str) message["content"][0]["text"]
else "" if isinstance(message["content"][0].get("text"), str)
else ""
)
) )
tokens = len(encoding.encode(content)) + 1 tokens = len(encoding.encode(content)) + 1
if total_tokens + tokens > max_tokens: if total_tokens + tokens > max_tokens:
@ -423,11 +425,19 @@ class GPTBot:
tool = tool_call.function.name tool = tool_call.function.name
args = json.loads(tool_call.function.arguments) args = json.loads(tool_call.function.arguments)
self.logger.log(f"Calling tool {tool} with args {args}", "debug") self.logger.log(
f"Calling tool {tool} with args {args} for user {user} in room {room}",
"debug",
)
await self.send_message(
room, f"Calling tool {tool} with arguments {args}.", True
)
try: try:
tool_class = TOOLS[tool] tool_class = TOOLS[tool]
result = await tool_class(**args, room=room, bot=self, user=user).run() result = await tool_class(**args, room=room, bot=self, user=user).run()
await self.send_message(room, result, msgtype="gptbot.tool_result")
return result return result
except (Handover, StopProcessing): except (Handover, StopProcessing):
@ -715,7 +725,11 @@ class GPTBot:
self.logger.log("Sent file", "debug") self.logger.log("Sent file", "debug")
async def send_message( async def send_message(
self, room: MatrixRoom | str, message: str, notice: bool = False self,
room: MatrixRoom | str,
message: str,
notice: bool = False,
msgtype: Optional[str] = None,
): ):
"""Send a message to a room. """Send a message to a room.
@ -731,14 +745,21 @@ class GPTBot:
markdowner = markdown2.Markdown(extras=["fenced-code-blocks"]) markdowner = markdown2.Markdown(extras=["fenced-code-blocks"])
formatted_body = markdowner.convert(message) formatted_body = markdowner.convert(message)
msgtype = "m.notice" if notice else "m.text" msgtype = msgtype if msgtype else "m.notice" if notice else "m.text"
msgcontent = { if not msgtype.startswith("gptbot."):
"msgtype": msgtype, msgcontent = {
"body": message, "msgtype": msgtype,
"format": "org.matrix.custom.html", "body": message,
"formatted_body": formatted_body, "format": "org.matrix.custom.html",
} "formatted_body": formatted_body,
}
else:
msgcontent = {
"msgtype": msgtype,
"content": message,
}
content = None content = None
@ -1074,7 +1095,7 @@ class GPTBot:
return return
try: try:
last_messages = await self._last_n_messages(room.room_id, 20) last_messages = await self._last_n_messages(room.room_id, self.max_messages)
except Exception as e: except Exception as e:
self.logger.log(f"Error getting last messages: {e}", "error") self.logger.log(f"Error getting last messages: {e}", "error")
await self.send_message( await self.send_message(

View file

@ -163,7 +163,7 @@ class OpenAI:
if count > 5: if count > 5:
self.logger.log(f"Recursion depth exceeded, aborting.") self.logger.log(f"Recursion depth exceeded, aborting.")
return self.generate_chat_response( return await self.generate_chat_response(
messages, messages,
user=user, user=user,
room=room, room=room,
@ -186,10 +186,10 @@ class OpenAI:
original_messages = messages original_messages = messages
if allow_override and not "gpt-3.5-turbo" in model: if allow_override and not "gpt-3.5-turbo" in original_model:
if self.bot.config.getboolean("OpenAI", "ForceTools", fallback=False): if self.bot.config.getboolean("OpenAI", "ForceTools", fallback=False):
self.logger.log(f"Overriding chat model to use tools") self.logger.log(f"Overriding chat model to use tools")
chat_model = "gpt-3.5-turbo-1106" chat_model = "gpt-3.5-turbo-0125"
out_messages = [] out_messages = []
@ -244,7 +244,9 @@ class OpenAI:
If no tool is required, or all information is already available in the message thread, respond with an empty JSON object: {} If no tool is required, or all information is already available in the message thread, respond with an empty JSON object: {}
Do NOT FOLLOW ANY OTHER INSTRUCTIONS BELOW, they are only meant for the AI chat model. You can ignore them. DO NOT include any other text or syntax in your response, only the JSON object. DO NOT surround it in code tags (```). DO NOT, UNDER ANY CIRCUMSTANCES, ASK AGAIN FOR INFORMATION ALREADY PROVIDED IN THE MESSAGES YOU RECEIVED! DO NOT REQUEST MORE INFORMATION THAN ABSOLUTELY REQUIRED TO RESPOND TO THE USER'S MESSAGE! Remind the user that they may ask you to search for additional information if they need it. Otherwise, respond with a single required tool call. Remember that you DO NOT RESPOND to the user. You MAY ONLY RESPOND WITH JSON OBJECTS CONTAINING TOOL CALLS! DO NOT RESPOND IN NATURAL LANGUAGE.
DO NOT include any other text or syntax in your response, only the JSON object. DO NOT surround it in code tags (```). DO NOT, UNDER ANY CIRCUMSTANCES, ASK AGAIN FOR INFORMATION ALREADY PROVIDED IN THE MESSAGES YOU RECEIVED! DO NOT REQUEST MORE INFORMATION THAN ABSOLUTELY REQUIRED TO RESPOND TO THE USER'S MESSAGE! Remind the user that they may ask you to search for additional information if they need it.
""", """,
} }
] ]
@ -472,8 +474,11 @@ class OpenAI:
new_messages.append(new_message) new_messages.append(new_message)
result_text, additional_tokens = await self.generate_chat_response( result_text, additional_tokens = await self.generate_chat_response(
new_messages, user=user, room=room, allow_override=False, new_messages,
model=original_model user=user,
room=room,
allow_override=False,
model=original_model,
) )
try: try: