fix: Fixed bot not remembering conversations at startup

This commit is contained in:
Showdown76 2024-01-27 22:40:03 +01:00
parent ea9196e66a
commit ad2113e986
Signed by: showdown
GPG Key ID: 062A80AA93C13988
1 changed files with 18 additions and 6 deletions

24
main.py
View File

@ -98,17 +98,26 @@ async def on_message(message: discord.Message):
# order by timestamp (most recent to least recent)
usable_messages = []
for msg in msgs:
print(msg)
d = previous_tokens + msg[3]
if d >= max_token:
print("je break")
break
previous_tokens += msg[3]
print('jappend')
usable_messages.append(msg)
usable_messages.reverse()
messages = [{"role": "system", "content": bprompt}]
for v in usable_messages: messages.append({"role": v[4], "content": v[2]})
cached_conversations[message.author].add_message(
role=copeai_backend.conversation.Role.SYSTEM,
message=bprompt
)
for v in usable_messages:
cached_conversations[message.author].add_message(
role=copeai_backend.conversation.Role(v[4]),
message=v[2]
)
else:
total_tokens = copeai_backend.conversation.text_to_tokens(cached_conversations[message.author])
while total_tokens > int(os.environ['MAX_TOKEN_PER_REQUEST']) - 400:
@ -155,9 +164,12 @@ async def on_message(message: discord.Message):
else:
await MSG.edit(content=response, view=views.GenerationState.GenerationStateView(views.GenerationState.GenerationState.FINISHED))
c.execute('INSERT INTO message_history VALUES (?, ?, ?, ?, ?, ?)', (message.id, message.author.id, message.content, copeai_backend.conversation.text_to_tokens(message.content), 'user', int(message.created_at.timestamp())))
c.execute('INSERT INTO message_history VALUES (?, ?, ?, ?, ?, ?)', (MSG.id, message.author.id, response, copeai_backend.conversation.text_to_tokens(response), 'assistant', int(time.time())))
db.commit()
with db:
db.executemany('INSERT INTO message_history VALUES (?, ?, ?, ?, ?, ?)', [
(message.id, message.author.id, message.content, copeai_backend.conversation.text_to_tokens(message.content), 'user', int(message.created_at.timestamp())),
(MSG.id, message.author.id, response, copeai_backend.conversation.text_to_tokens(response), 'assistant', int(time.time()))
]
)
except Exception as e:
traceback.print_exc()
if message.channel in typing: typing.remove(message.channel)