feat: 支持在token超限时自动重置会话

This commit is contained in:
Rock Chin 2023-06-08 13:45:54 +08:00
parent b8308f8c57
commit 43aa68a55d
4 changed files with 9 additions and 5 deletions

View File

@ -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)

View File

@ -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:
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:

View File

@ -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()

View File

@ -34,4 +34,4 @@ command_reset_message = "[bot]会话已重置"
command_reset_name_message = "[bot]会话已重置,使用场景预设:"
# 会话自动重置时的提示
session_auto_reset_message = "[bot]会话token超限已自动重置"
session_auto_reset_message = "[bot]会话token超限已自动重置,请重新发送消息"