diff --git a/config-template.py b/config-template.py index 2588ca35..401526ee 100644 --- a/config-template.py +++ b/config-template.py @@ -195,6 +195,10 @@ encourage_sponsor_at_start = True # 注意:较大的prompt_submit_length会导致OpenAI账户额度消耗更快 prompt_submit_length = 2048 +# 是否在token超限报错时自动重置会话 +# 可在tips.py中编辑提示语 +auto_reset = True + # OpenAI补全API的参数 # 请在下方填写模型,程序自动选择接口 # 现已支持的模型有: diff --git a/override-all.json b/override-all.json index 298f9b61..eafbbd5e 100644 --- a/override-all.json +++ b/override-all.json @@ -53,6 +53,7 @@ "inappropriate_message_tips": "[百度云]请珍惜机器人,当前返回内容不合规", "encourage_sponsor_at_start": true, "prompt_submit_length": 2048, + "auto_reset": true, "completion_api_params": { "model": "gpt-3.5-turbo", "temperature": 0.9, diff --git a/pkg/openai/session.py b/pkg/openai/session.py index 4a74e971..fc217ab3 100644 --- a/pkg/openai/session.py +++ b/pkg/openai/session.py @@ -83,7 +83,7 @@ def load_sessions(): # 获取指定名称的session,如果不存在则创建一个新的 -def get_session(session_name: str): +def get_session(session_name: str) -> 'Session': global sessions if session_name not in sessions: sessions[session_name] = Session(session_name) diff --git a/pkg/qqbot/message.py b/pkg/qqbot/message.py index 4fb6b991..4c0207df 100644 --- a/pkg/qqbot/message.py +++ b/pkg/qqbot/message.py @@ -114,8 +114,12 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, reply = handle_exception("{}会话调用API失败:{}".format(session_name, e), "[bot]err:RateLimitError,请重试或联系作者,或等待修复") except openai.error.InvalidRequestError as e: - reply = handle_exception("{}API调用参数错误:{}\n".format( - session_name, e), "[bot]err:API调用参数错误,请联系管理员,或等待修复") + if config.auto_reset and "This model's maximum context length is" in str(e): + session.reset() + reply = [tips_custom.session_auto_reset_message] + else: + reply = handle_exception("{}API调用参数错误:{}\n".format( + session_name, e), "[bot]err:API调用参数错误,请联系管理员,或等待修复") except openai.error.ServiceUnavailableError as e: reply = handle_exception("{}API调用服务不可用:{}".format(session_name, e), "[bot]err:API调用服务不可用,请重试或联系管理员,或等待修复") except Exception as e: diff --git a/pkg/qqbot/process.py b/pkg/qqbot/process.py index 5af3c084..3ae95437 100644 --- a/pkg/qqbot/process.py +++ b/pkg/qqbot/process.py @@ -74,7 +74,7 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes import config if config.income_msg_check: if mgr.reply_filter.is_illegal(text_message): - return MessageChain(Plain("[bot] 你的提问中有不合适的内容, 请更换措辞~")) + return MessageChain(Plain("[bot] 消息中存在不合适的内容, 请更换措辞")) pkg.openai.session.get_session(session_name).acquire_response_lock() diff --git a/tips-custom-template.py b/tips-custom-template.py index 09eb7f19..639751d2 100644 --- a/tips-custom-template.py +++ b/tips-custom-template.py @@ -30,5 +30,8 @@ command_admin_message = "[bot]err:权限不足: " command_err_message = "[bot]err:指令不存在:" # 会话重置提示 -command_reset_message = "[bot]:会话已重置" -command_reset_name_message = "[bot]:会话已重置,使用场景预设:" +command_reset_message = "[bot]会话已重置" +command_reset_name_message = "[bot]会话已重置,使用场景预设:" + +# 会话自动重置时的提示 +session_auto_reset_message = "[bot]会话token超限,已自动重置,请重新发送消息"