From ad2113e986008c4c4d988e1c466fe7fbc571ea81 Mon Sep 17 00:00:00 2001 From: Showdown76py Date: Sat, 27 Jan 2024 22:40:03 +0100 Subject: [PATCH] fix: Fixed bot not remembering conversations at startup --- main.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index b876b58..b75b69c 100644 --- a/main.py +++ b/main.py @@ -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)