This commit is contained in:
Kumi 2023-04-16 14:29:28 +00:00
parent 67779b8335
commit 543f8229d2
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 7 additions and 3 deletions

View file

@ -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 You may want to run the bot in a screen or tmux session, or use a process
manager like systemd. manager like systemd.
Once it is running, just invite it to a room and it will start responding to
messages.
## License ## License
This project is licensed under the terms of the MIT license. This project is licensed under the terms of the MIT license.

View file

@ -77,7 +77,7 @@ async def fetch_last_n_messages(room_id, n=20):
room = await client.join(room_id) room = await client.join(room_id)
messages = [] 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( response = await client.room_messages(
room_id=room_id, 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 total_tokens = len(SYSTEM_MESSAGE) + 1
truncated_messages = [] truncated_messages = []
for message in messages[0] + reversed(messages[1:]): for message in [messages[0]] + list(reversed(messages[1:])):
content = message["content"] content = message["content"]
tokens = len(encoding.encode(content)) + 1 tokens = len(encoding.encode(content)) + 1
if total_tokens + tokens > max_tokens: if total_tokens + tokens > max_tokens:
@ -124,7 +124,7 @@ def truncate_messages_to_fit_tokens(messages, max_tokens=MAX_TOKENS):
total_tokens += tokens total_tokens += tokens
truncated_messages.append(message) 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): async def message_callback(room: MatrixRoom, event: RoomMessageText):
@ -220,6 +220,7 @@ async def accept_pending_invites():
async def sync_cb(response): async def sync_cb(response):
global SYNC_TOKEN
logging(f"Sync response received (next batch: {response.next_batch})") logging(f"Sync response received (next batch: {response.next_batch})")
SYNC_TOKEN = response.next_batch SYNC_TOKEN = response.next_batch