Compare commits
1 commit
main
...
room-name-
Author | SHA1 | Date | |
---|---|---|---|
f2d376501e |
1 changed files with 54 additions and 1 deletions
|
@ -328,7 +328,7 @@ class GPTBot:
|
||||||
event_type = event.source["content"]["msgtype"]
|
event_type = event.source["content"]["msgtype"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.logger.log(f"Could not process event: {event}", "debug")
|
self.logger.log(f"Could not process event: {event}", "debug")
|
||||||
continue # This is most likely not a message event
|
continue # This is most likely not a message event
|
||||||
|
|
||||||
if event_type.startswith("gptbot"):
|
if event_type.startswith("gptbot"):
|
||||||
messages.append(event)
|
messages.append(event)
|
||||||
|
@ -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.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue