mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 12:05:54 +00:00
Merge pull request #123 from RockChinQ/error-msg-process
feta: 支持设置不向用户展示异常详细信息
This commit is contained in:
@@ -58,16 +58,6 @@ response_rules = {
|
||||
"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_word_filter = True
|
||||
@@ -121,6 +111,16 @@ show_prefix = False
|
||||
# 消息处理超时重试次数
|
||||
retry_times = 3
|
||||
|
||||
# 消息处理出错时是否向用户隐藏错误详细信息
|
||||
# 设置为True时,仅向管理员发送错误详细信息
|
||||
# 设置为False时,向用户及管理员发送错误详细信息
|
||||
hide_exce_info_to_user = False
|
||||
|
||||
# 消息处理出错时向用户发送的提示信息
|
||||
# 仅当hide_exce_info_to_user为True时生效
|
||||
# 设置为空字符串时,不发送提示信息
|
||||
alter_tip_message = '出错了,请稍后再试'
|
||||
|
||||
# 每个会话的过期时间,单位为秒
|
||||
# 默认值20分钟
|
||||
session_expire_time = 60 * 20
|
||||
|
||||
@@ -8,6 +8,19 @@ import pkg.plugin.host as plugin_host
|
||||
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,
|
||||
launcher_id: int, sender_id: int) -> list:
|
||||
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():
|
||||
reply = [prefix + text]
|
||||
except openai.error.APIConnectionError as e:
|
||||
mgr.notify_admin("{}会话调用API失败:{}".format(session_name, e))
|
||||
reply = ["[bot]err:调用API失败,请重试或联系作者,或等待修复"]
|
||||
reply = handle_exception("{}会话调用API失败:{}".format(session_name, e), "[bot]err:调用API失败,请重试或联系作者,或等待修复")
|
||||
except openai.error.RateLimitError as e:
|
||||
logging.debug(type(e))
|
||||
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()
|
||||
|
||||
if not switched:
|
||||
mgr.notify_admin(
|
||||
reply = handle_exception(
|
||||
"api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format(
|
||||
current_key_name))
|
||||
reply = ["[bot]err:API调用额度超额,请联系作者,或等待修复"]
|
||||
current_key_name), "[bot]err:API调用额度超额,请联系作者,或等待修复")
|
||||
else:
|
||||
openai.api_key = pkg.utils.context.get_openai_manager().key_mgr.get_using_key()
|
||||
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
|
||||
else:
|
||||
mgr.notify_admin("{}会话调用API失败:{}".format(session_name, e))
|
||||
reply = ["[bot]err:RateLimitError,请重试或联系作者,或等待修复"]
|
||||
reply = handle_exception("{}会话调用API失败:{}".format(session_name, e),
|
||||
"[bot]err:RateLimitError,请重试或联系作者,或等待修复")
|
||||
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(
|
||||
session_name, e))
|
||||
reply = ["[bot]err:API调用参数错误,请联系作者,或等待修复"]
|
||||
session_name, e), "[bot]err:API调用参数错误,请联系作者,或等待修复")
|
||||
except openai.error.ServiceUnavailableError as e:
|
||||
# mgr.notify_admin("{}API调用服务不可用:{}".format(session_name, e))
|
||||
reply = ["[bot]err:API调用服务暂不可用,请尝试重试"]
|
||||
reply = handle_exception("{}API调用服务不可用:{}".format(session_name, e), "[bot]err:API调用服务不可用,请重试或联系作者,或等待修复")
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
reply = ["[bot]err:{}".format(e)]
|
||||
reply = handle_exception("{}会话处理异常:{}".format(session_name, e), "[bot]err:{}".format(e))
|
||||
break
|
||||
|
||||
return reply
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
from pip._internal import main as pipmain
|
||||
|
||||
import main
|
||||
|
||||
|
||||
def install(package):
|
||||
pipmain(['install', package])
|
||||
main.reset_logging()
|
||||
|
||||
|
||||
def install_requirements(file):
|
||||
pipmain(['install', '-r', file])
|
||||
main.reset_logging()
|
||||
|
||||
|
||||
def ensure_dulwich():
|
||||
|
||||
Reference in New Issue
Block a user