From 543f8229d25bfdea91c1861104ab911c839c1e62 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 16 Apr 2023 14:29:28 +0000 Subject: [PATCH] Fix --- README.md | 3 +++ gptbot.py | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 285f82c..99a75dd 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,9 @@ The bot can be run with `python -m gptbot`. If required, activate a venv first. You may want to run the bot in a screen or tmux session, or use a process manager like systemd. +Once it is running, just invite it to a room and it will start responding to +messages. + ## License This project is licensed under the terms of the MIT license. \ No newline at end of file diff --git a/gptbot.py b/gptbot.py index 1fa0422..692bfda 100644 --- a/gptbot.py +++ b/gptbot.py @@ -77,7 +77,7 @@ async def fetch_last_n_messages(room_id, n=20): room = await client.join(room_id) messages = [] - logging(f"Fetching last {n} messages from room {room_id}...") + logging(f"Fetching last {n} messages from room {room_id} (starting at {SYNC_TOKEN})...") response = await client.room_messages( room_id=room_id, @@ -116,7 +116,7 @@ def truncate_messages_to_fit_tokens(messages, max_tokens=MAX_TOKENS): total_tokens = len(SYSTEM_MESSAGE) + 1 truncated_messages = [] - for message in messages[0] + reversed(messages[1:]): + for message in [messages[0]] + list(reversed(messages[1:])): content = message["content"] tokens = len(encoding.encode(content)) + 1 if total_tokens + tokens > max_tokens: @@ -124,7 +124,7 @@ def truncate_messages_to_fit_tokens(messages, max_tokens=MAX_TOKENS): total_tokens += tokens truncated_messages.append(message) - return list(truncated_messages[0] + reversed(truncated_messages[1:])) + return [truncated_messages[0]] + list(reversed(truncated_messages[1:])) async def message_callback(room: MatrixRoom, event: RoomMessageText): @@ -220,6 +220,7 @@ async def accept_pending_invites(): async def sync_cb(response): + global SYNC_TOKEN logging(f"Sync response received (next batch: {response.next_batch})") SYNC_TOKEN = response.next_batch