father-bot / chatgpt_telegram_bot

💬 Telegram bot with ChatGPT, Python-based, using OpenAI's API.
https://t.me/chatgpt_karfly_bot
MIT License
5.2k stars 1.82k forks source link

Fix storing and passing previous messages to API #449

Open niquepolice opened 7 months ago

niquepolice commented 7 months ago

Critical bug: conversation history is lost, bot remembers only the current message

Current version does not pass chat history to the API correctly: vision_message_handle_fn introduced new format for saving user messages

new_dialog_message = {"user": [{"type": "text", "text": message}], "bot": answer, "date": datetime.now()}

and _generate_prompt_messages has been changed accordingly. But in message_handle_fn it is still

new_dialog_message = {"user": _message, "bot": answer, "date": datetime.now()}

Call of extend(dialog_message["user"]) for the dialog message saved in message_handle_fn results in splitting dialog_message["user"]= _message of type str into symbols, thus the structure in response is messed and API returns something like error_code=None error_message="'$.messages[3].content' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference." error_param=None error_type=invalid_request_error message='OpenAI API error received' stream_error=False chatgpt_telegram_bot | 2024-04-09 18:47:58 DEBUG: message='Request to OpenAI API' method=post path=https://api.openai.com/v1/chat/completions and finally ignores the conversation context.