diff --git a/src/gptbot/classes/bot.py b/src/gptbot/classes/bot.py index 62c28c0..3361b77 100644 --- a/src/gptbot/classes/bot.py +++ b/src/gptbot/classes/bot.py @@ -229,9 +229,9 @@ class GPTBot: if Path(bot.logo_path).exists() and Path(bot.logo_path).is_file(): bot.logo = Image.open(bot.logo_path) - bot.chat_api = ( - bot.image_api - ) = bot.classification_api = bot.tts_api = bot.stt_api = OpenAI( + bot.chat_api = bot.image_api = bot.classification_api = bot.tts_api = ( + bot.stt_api + ) = OpenAI( bot=bot, api_key=config["OpenAI"]["APIKey"], chat_model=config["OpenAI"].get("Model"), @@ -377,9 +377,11 @@ class GPTBot: content = ( message["content"] if isinstance(message["content"], str) - else message["content"][0]["text"] - if isinstance(message["content"][0].get("text"), str) - else "" + else ( + message["content"][0]["text"] + if isinstance(message["content"][0].get("text"), str) + else "" + ) ) tokens = len(encoding.encode(content)) + 1 if total_tokens + tokens > max_tokens: @@ -423,11 +425,19 @@ class GPTBot: tool = tool_call.function.name 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: tool_class = TOOLS[tool] result = await tool_class(**args, room=room, bot=self, user=user).run() + await self.send_message(room, result, msgtype="gptbot.tool_result") return result except (Handover, StopProcessing): @@ -715,7 +725,11 @@ class GPTBot: self.logger.log("Sent file", "debug") 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. @@ -731,14 +745,21 @@ class GPTBot: markdowner = markdown2.Markdown(extras=["fenced-code-blocks"]) 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 = { - "msgtype": msgtype, - "body": message, - "format": "org.matrix.custom.html", - "formatted_body": formatted_body, - } + if not msgtype.startswith("gptbot."): + msgcontent = { + "msgtype": msgtype, + "body": message, + "format": "org.matrix.custom.html", + "formatted_body": formatted_body, + } + + else: + msgcontent = { + "msgtype": msgtype, + "content": message, + } content = None @@ -1074,7 +1095,7 @@ class GPTBot: return 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: self.logger.log(f"Error getting last messages: {e}", "error") await self.send_message( diff --git a/src/gptbot/classes/openai.py b/src/gptbot/classes/openai.py index 65ece46..612a124 100644 --- a/src/gptbot/classes/openai.py +++ b/src/gptbot/classes/openai.py @@ -163,7 +163,7 @@ class OpenAI: if count > 5: self.logger.log(f"Recursion depth exceeded, aborting.") - return self.generate_chat_response( + return await self.generate_chat_response( messages, user=user, room=room, @@ -186,10 +186,10 @@ class OpenAI: 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): 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 = [] @@ -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: {} - 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) result_text, additional_tokens = await self.generate_chat_response( - new_messages, user=user, room=room, allow_override=False, - model=original_model + new_messages, + user=user, + room=room, + allow_override=False, + model=original_model, ) try: