From 256dd3d8b6d28526cdcaca6e37294e573147addd Mon Sep 17 00:00:00 2001 From: niquepolice Date: Tue, 9 Apr 2024 21:29:58 +0300 Subject: [PATCH] Fix storing and passing previous messages to API --- bot/bot.py | 2 +- bot/openai_utils.py | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 2d9a758af..033639774 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -429,7 +429,7 @@ async def fake_gen(): prev_answer = answer # update user data - new_dialog_message = {"user": _message, "bot": answer, "date": datetime.now()} + new_dialog_message = {"user": [{"type": "text", "text": _message}], "bot": answer, "date": datetime.now()} db.set_dialog_messages( user_id, diff --git a/bot/openai_utils.py b/bot/openai_utils.py index cf19257e7..f45194ab6 100644 --- a/bot/openai_utils.py +++ b/bot/openai_utils.py @@ -254,25 +254,23 @@ def _generate_prompt_messages(self, message, dialog_messages, chat_mode, image_b prompt = config.chat_modes[chat_mode]["prompt_start"] messages = [{"role": "system", "content": prompt}] - user_messages = {"role": "user", "content": []} for dialog_message in dialog_messages: - user_messages["content"].extend(dialog_message["user"]) + messages.append({"role": "user", "content": dialog_message["user"]}) messages.append({"role": "assistant", "content": dialog_message["bot"]}) - user_messages["content"].append({"type": "text", "text": message}) + messages.append({"role": "user", "content": [{"type": "text", "text": message}]}) if image_buffer is not None: - user_messages["content"].append( + messages[-1]["content"].append( { "type": "image", "image": self._encode_image(image_buffer), } ) - response = messages + ([user_messages] if len(user_messages["content"]) > 0 else []) - return response + return messages def _postprocess_answer(self, answer): answer = answer.strip()