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.
This commit is contained in:
Kumi 2023-11-29 15:48:56 +01:00
parent ad600faf4b
commit 75360d040a
Signed by: kumi
GPG key ID: ECBCC9082395383F
5 changed files with 61 additions and 3 deletions

View file

@ -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?")

View file

@ -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")

View file

@ -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:

View file

@ -14,6 +14,7 @@ for tool in [
"imagedescription",
"wikipedia",
"datetime",
"newroom",
]:
tool_class = getattr(import_module(
"." + tool, "gptbot.tools"), tool.capitalize())

View file

@ -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."