From 75360d040a8bd2695c4da6f616f73db9d0c1bb2f Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 29 Nov 2023 15:48:56 +0100 Subject: [PATCH] Introduce Async Enhancements and New Room Tool Enabled asynchronous key upload in the roommember callback to improve efficiency. Fixed the chat response generation by properly referencing the event sender rather than the room ID, aligning user context with chat messages. Corrected the user parameter misuse in the OpenAI class to utilize the room ID. Extended the toolkit to include a 'newroom' feature for creating and setting up new Matrix rooms, thereby enhancing bot functionality. This commit significantly improves bot response times and contextual accuracy while interacting within rooms and adds a valuable feature for users to create rooms seamlessly. --- src/gptbot/callbacks/roommember.py | 2 +- src/gptbot/classes/bot.py | 2 +- src/gptbot/classes/openai.py | 2 +- src/gptbot/tools/__init__.py | 1 + src/gptbot/tools/newroom.py | 57 ++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/gptbot/tools/newroom.py diff --git a/src/gptbot/callbacks/roommember.py b/src/gptbot/callbacks/roommember.py index 08ba079..3016f03 100644 --- a/src/gptbot/callbacks/roommember.py +++ b/src/gptbot/callbacks/roommember.py @@ -1,7 +1,7 @@ from nio import RoomMemberEvent, MatrixRoom async def roommember_callback(room: MatrixRoom, event: RoomMemberEvent, bot): - bot.matrix_client.keys_upload() + await bot.matrix_client.keys_upload() if event.membership == "leave": bot.logger.log(f"User {event.state_key} left room {room.room_id} - am I alone now?") diff --git a/src/gptbot/classes/bot.py b/src/gptbot/classes/bot.py index fcab089..5281f26 100644 --- a/src/gptbot/classes/bot.py +++ b/src/gptbot/classes/bot.py @@ -1120,7 +1120,7 @@ class GPTBot: try: response, tokens_used = await self.chat_api.generate_chat_response( - chat_messages, user=room.room_id, room=room.room_id + chat_messages, user=event.sender, room=room.room_id ) except Exception as e: self.logger.log(f"Error generating response: {e}", "error") diff --git a/src/gptbot/classes/openai.py b/src/gptbot/classes/openai.py index 5352230..f4088fc 100644 --- a/src/gptbot/classes/openai.py +++ b/src/gptbot/classes/openai.py @@ -263,7 +263,7 @@ class OpenAI: kwargs = { "model": chat_model, "messages": messages, - "user": user, + "user": room, } if "gpt-3.5-turbo" in chat_model and use_tools: diff --git a/src/gptbot/tools/__init__.py b/src/gptbot/tools/__init__.py index 747e502..7b66687 100644 --- a/src/gptbot/tools/__init__.py +++ b/src/gptbot/tools/__init__.py @@ -14,6 +14,7 @@ for tool in [ "imagedescription", "wikipedia", "datetime", + "newroom", ]: tool_class = getattr(import_module( "." + tool, "gptbot.tools"), tool.capitalize()) diff --git a/src/gptbot/tools/newroom.py b/src/gptbot/tools/newroom.py new file mode 100644 index 0000000..c874aea --- /dev/null +++ b/src/gptbot/tools/newroom.py @@ -0,0 +1,57 @@ +from .base import BaseTool + +from nio import RoomCreateError, RoomInviteError + +from contextlib import closing + +class Newroom(BaseTool): + DESCRIPTION = "Create a new Matrix room" + PARAMETERS = { + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "The name of the room to create.", + "default": "GPTBot" + } + }, + } + + async def run(self): + """Create a new Matrix room""" + name = self.kwargs.get("name", "GPTBot") + + self.bot.logger.log("Creating new room...") + new_room = await self.bot.matrix_client.room_create(name=name) + + if isinstance(new_room, RoomCreateError): + self.bot.logger.log(f"Failed to create room: {new_room.message}") + raise + + self.bot.logger.log(f"Inviting {self.user} to new room...") + invite = await self.bot.matrix_client.room_invite(new_room.room_id, self.user) + + if isinstance(invite, RoomInviteError): + self.bot.logger.log(f"Failed to invite user: {invite.message}") + raise + + await self.bot.send_message(new_room.room_id, "Welcome to your new room! What can I do for you?") + + with closing(self.bot.database.cursor()) as cursor: + cursor.execute( + "SELECT space_id FROM user_spaces WHERE user_id = ? AND active = TRUE", (event.sender,)) + space = cursor.fetchone() + + if space: + self.bot.logger.log(f"Adding new room to space {space[0]}...") + await self.bot.add_rooms_to_space(space[0], [new_room.room_id]) + + if self.bot.logo_uri: + await self.bot.matrix_client.room_put_state(room, "m.room.avatar", { + "url": self.bot.logo_uri + }, "") + + await self.bot.matrix_client.room_put_state( + new_room.room_id, "m.room.power_levels", {"users": {self.user: 100, self.bot.matrix_client.user_id: 100}}) + + return "Created new Matrix room with ID " + new_room.room_id + " and invited user." \ No newline at end of file