From efd54c9ea48a5fbd0e15bfbfdb42732106068465 Mon Sep 17 00:00:00 2001 From: Tuchuanhuhuhu Date: Thu, 12 Oct 2023 23:50:21 +0800 Subject: [PATCH] bugfix: corrupted history may exceed token limit fix #916 --- ChuanhuChatbot.py | 11 ++++++----- modules/models/OpenAI.py | 2 +- modules/models/base_model.py | 4 ++++ modules/utils.py | 3 --- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ChuanhuChatbot.py b/ChuanhuChatbot.py index c60d0008..76722411 100644 --- a/ChuanhuChatbot.py +++ b/ChuanhuChatbot.py @@ -1,4 +1,10 @@ # -*- coding:utf-8 -*- +import logging +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s", +) + from modules.models.models import get_model from modules.train_func import * from modules.repo import * @@ -10,11 +16,6 @@ from modules import config import gradio as gr import colorama -import logging -logging.basicConfig( - level=logging.INFO, - format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s", -) logging.getLogger("httpx").setLevel(logging.WARNING) diff --git a/modules/models/OpenAI.py b/modules/models/OpenAI.py index 3cada2a2..ae2a4bd8 100644 --- a/modules/models/OpenAI.py +++ b/modules/models/OpenAI.py @@ -217,7 +217,7 @@ def _decode_chat_response(self, response): except: print(f"ERROR: {chunk}") continue - if error_msg: + if error_msg and not error_msg=="data: [DONE]": raise Exception(error_msg) def set_key(self, new_access_key): diff --git a/modules/models/base_model.py b/modules/models/base_model.py index 64083c12..5aa32e5b 100644 --- a/modules/models/base_model.py +++ b/modules/models/base_model.py @@ -747,6 +747,10 @@ def load_chat_history(self, new_history_file_path=None, username=None): logging.info(new_history) except: pass + if len(json_s["chatbot"]) < len(json_s["history"]): + logging.info("Trimming corrupted history...") + json_s["history"] = json_s["history"][-len(json_s["chatbot"]):] + logging.info(f"Trimmed history: {json_s['history']}") logging.debug(f"{self.user_identifier} 加载对话历史完毕") self.history = json_s["history"] return os.path.basename(self.history_file_path), json_s["system"], json_s["chatbot"] diff --git a/modules/utils.py b/modules/utils.py index 328be2c0..a71ddcaa 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -77,9 +77,6 @@ def auto_name_chat_history(current_model, *args): def export_markdown(current_model, *args): return current_model.export_markdown(*args) -def load_chat_history(current_model, *args): - return current_model.load_chat_history(*args) - def upload_chat_history(current_model, *args): return current_model.load_chat_history(*args)