feat: 支持设置不向用户展示异常详细信息

This commit is contained in:
Rock Chin
2023-02-08 17:06:54 +08:00
parent 6531aae617
commit 65664ae178
2 changed files with 32 additions and 23 deletions
+10 -10
View File
@@ -58,16 +58,6 @@ response_rules = {
"regexp": [] # "为什么.*", "怎么?样.*", "怎么.*", "如何.*", "[Hh]ow to.*", "[Ww]hy not.*", "[Ww]hat is.*", ".*怎么办", ".*咋办" "regexp": [] # "为什么.*", "怎么?样.*", "怎么.*", "如何.*", "[Hh]ow to.*", "[Ww]hy not.*", "[Ww]hat is.*", ".*怎么办", ".*咋办"
} }
# 单个api-key的费用警告阈值
# 当使用此api-key进行请求所消耗的费用估算达到此阈值时,会在控制台输出警告并通知管理员
# 若之后还有未使用超过此值的api-key,则会切换到新的api-key进行请求
# 单位:美元
api_key_fee_threshold = 18.0
# 是否根据估算的使用费用切换api-key
# 设置为False将只在接口报错超额时自动切换
auto_switch_api_key = False
# 敏感词过滤开关,以同样数量的*代替敏感词回复 # 敏感词过滤开关,以同样数量的*代替敏感词回复
# 请在sensitive.json中添加敏感词 # 请在sensitive.json中添加敏感词
sensitive_word_filter = True sensitive_word_filter = True
@@ -121,6 +111,16 @@ show_prefix = False
# 消息处理超时重试次数 # 消息处理超时重试次数
retry_times = 3 retry_times = 3
# 消息处理出错时是否向用户隐藏错误详细信息
# 设置为True时,仅向管理员发送错误详细信息
# 设置为False时,向用户及管理员发送错误详细信息
hide_exce_info_to_user = False
# 消息处理出错时向用户发送的提示信息
# 仅当hide_exce_info_to_user为True时生效
# 设置为空字符串时,不发送提示信息
alter_tip_message = '出错了,请稍后再试'
# 每个会话的过期时间,单位为秒 # 每个会话的过期时间,单位为秒
# 默认值20分钟 # 默认值20分钟
session_expire_time = 60 * 20 session_expire_time = 60 * 20
+22 -13
View File
@@ -8,6 +8,19 @@ import pkg.plugin.host as plugin_host
import pkg.plugin.models as plugin_models import pkg.plugin.models as plugin_models
def handle_exception(notify_admin: str = "", set_reply: str = "") -> list:
"""处理异常,当notify_admin不为空时,会通知管理员,返回通知用户的消息"""
import config
pkg.utils.context.get_qqbot_manager().notify_admin(notify_admin)
if hasattr(config, 'hide_exce_info_to_user') and config.hide_exce_info_to_user:
if hasattr(config, 'alter_tip_message'):
return [config.alter_tip_message] if config.alter_tip_message else []
else:
return ["[bot]出错了,请重试或联系管理员"]
else:
return [set_reply]
def process_normal_message(text_message: str, mgr, config, launcher_type: str, def process_normal_message(text_message: str, mgr, config, launcher_type: str,
launcher_id: int, sender_id: int) -> list: launcher_id: int, sender_id: int) -> list:
session_name = f"{launcher_type}_{launcher_id}" session_name = f"{launcher_type}_{launcher_id}"
@@ -44,8 +57,7 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str,
if not event.is_prevented_default(): if not event.is_prevented_default():
reply = [prefix + text] reply = [prefix + text]
except openai.error.APIConnectionError as e: except openai.error.APIConnectionError as e:
mgr.notify_admin("{}会话调用API失败:{}".format(session_name, e)) reply = handle_exception("{}会话调用API失败:{}".format(session_name, e), "[bot]err:调用API失败,请重试或联系作者,或等待修复")
reply = ["[bot]err:调用API失败,请重试或联系作者,或等待修复"]
except openai.error.RateLimitError as e: except openai.error.RateLimitError as e:
logging.debug(type(e)) logging.debug(type(e))
logging.debug(e.error['message']) logging.debug(e.error['message'])
@@ -70,10 +82,9 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str,
switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch() switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch()
if not switched: if not switched:
mgr.notify_admin( reply = handle_exception(
"api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format( "api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format(
current_key_name)) current_key_name), "[bot]err:API调用额度超额,请联系作者,或等待修复")
reply = ["[bot]err:API调用额度超额,请联系作者,或等待修复"]
else: else:
openai.api_key = pkg.utils.context.get_openai_manager().key_mgr.get_using_key() openai.api_key = pkg.utils.context.get_openai_manager().key_mgr.get_using_key()
mgr.notify_admin("api-key调用额度超限({}),接口报错,已切换到{}".format(current_key_name, name)) mgr.notify_admin("api-key调用额度超限({}),接口报错,已切换到{}".format(current_key_name, name))
@@ -83,19 +94,17 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str,
# 重试 # 重试
continue continue
else: else:
mgr.notify_admin("{}会话调用API失败:{}".format(session_name, e)) reply = handle_exception("{}会话调用API失败:{}".format(session_name, e),
reply = ["[bot]err:RateLimitError,请重试或联系作者,或等待修复"] "[bot]err:RateLimitError,请重试或联系作者,或等待修复")
except openai.error.InvalidRequestError as e: except openai.error.InvalidRequestError as e:
mgr.notify_admin("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或" reply = handle_exception("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或"
"completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format( "completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format(
session_name, e)) session_name, e), "[bot]err:API调用参数错误,请联系作者,或等待修复")
reply = ["[bot]err:API调用参数错误,请联系作者,或等待修复"]
except openai.error.ServiceUnavailableError as e: except openai.error.ServiceUnavailableError as e:
# mgr.notify_admin("{}API调用服务不可用:{}".format(session_name, e)) reply = handle_exception("{}API调用服务不可用:{}".format(session_name, e), "[bot]err:API调用服务不可用,请重试或联系作者,或等待修复")
reply = ["[bot]err:API调用服务暂不可用,请尝试重试"]
except Exception as e: except Exception as e:
logging.exception(e) logging.exception(e)
reply = ["[bot]err:{}".format(e)] reply = handle_exception("{}会话处理异常:{}".format(session_name, e), "[bot]err:{}".format(e))
break break
return reply return reply