Compare commits

...

1 commit

Author SHA1 Message Date
f2d376501e
feat: auto-generate room names on message send
Introduces functionality to auto-generate and set room names for conversations that still have the default name upon sending a message. This leverages a new `generate_room_name` method that creates a room name based on the last few messages. Enhancements include better event handling with refined logging during event processing errors. This update aims to enrich room identification for users by providing more contextually relevant names, improving navigation and user experience in multi-room setups.
2024-04-23 08:21:09 +02:00

View file

@ -1320,6 +1320,17 @@ class GPTBot:
message = await self.send_message(room, response) message = await self.send_message(room, response)
# Set room name
if self.generate_room_name and room.name == self.default_room_name:
try:
name = await self.generate_room_name(room)
await self.matrix_client.room_put_state(
room.room_id, "m.room.name", {"name": name}, ""
)
except Exception as e:
self.logger.log(f"Error generating room name: {e}", "error")
await self.matrix_client.room_typing(room.room_id, False) await self.matrix_client.room_typing(room.room_id, False)
async def download_file(self, mxc) -> Optional[bytes]: async def download_file(self, mxc) -> Optional[bytes]:
@ -1340,6 +1351,48 @@ class GPTBot:
return download return download
async def generate_room_name(self, room: MatrixRoom | str) -> str:
"""Generate a name for a room.
Args:
room (MatrixRoom | str): The room to generate a name for.
Returns:
str: The generated name.
"""
if isinstance(room, MatrixRoom):
room = room.room_id
prompt = f"Generate a short, descriptive name for this conversation. It should start with '{self.default_room_name}:' and be no more than 50 characters long. Return only the name, without any additional text."
messages = await self._last_n_messages(room, 2)
chat_messages = [[{"role": "system", "content": prompt}]]
for message in messages:
if isinstance(message, (RoomMessageNotice, RoomMessageText)):
role = (
"assistant"
if message.sender == self.matrix_client.user_id
else "user"
)
message_body = (
message.body
if not self.chat_api.supports_chat_images()
else [{"type": "text", "text": message.body}]
)
chat_messages.append({"role": role, "content": message_body})
response, tokens_used = await self.chat_api.generate_chat_response(
chat_messages,
room=room,
allow_override=False,
use_tools=False,
)
return response
def get_system_message(self, room: MatrixRoom | str) -> str: def get_system_message(self, room: MatrixRoom | str) -> str:
"""Get the system message for a room. """Get the system message for a room.