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

24
main.py
View File

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