diff --git a/pkg/qqbot/message.py b/pkg/qqbot/message.py index 05e0d6f1..2b2cc6c2 100644 --- a/pkg/qqbot/message.py +++ b/pkg/qqbot/message.py @@ -29,8 +29,15 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, session = pkg.openai.session.get_session(session_name) + unexpected_exception_times = 0 + + max_unexpected_exception_times = 3 + reply = [] while True: + if unexpected_exception_times >= max_unexpected_exception_times: + reply = handle_exception(notify_admin=f"{session_name},多次尝试失败。", set_reply=f"[bot]多次尝试失败,请重试或联系管理员") + break try: prefix = "[GPT]" if hasattr(config, "show_prefix") and config.show_prefix else "" @@ -57,7 +64,12 @@ 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: - reply = handle_exception("{}会话调用API失败:{}".format(session_name, e), "[bot]err:调用API失败,请重试或联系作者,或等待修复") + err_msg = str(e) + if err_msg.__contains__('Error communicating with OpenAI'): + reply = handle_exception("{}会话调用API失败:{}\n请尝试关闭网络代理来解决此问题。".format(session_name, e), + "[bot]err:调用API失败,请重试或联系管理员,或等待修复") + else: + 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']) @@ -83,8 +95,8 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, if not switched: reply = handle_exception( - "api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format( - current_key_name), "[bot]err:API调用额度超额,请联系作者,或等待修复") + "api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key;如果你认为这是误判,请尝试重启程序。".format( + 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)) @@ -92,6 +104,12 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, continue elif 'message' in e.error and e.error['message'].__contains__('You can retry your request'): # 重试 + unexpected_exception_times += 1 + continue + elif 'message' in e.error and e.error['message']\ + .__contains__('The server had an error while processing your request'): + # 重试 + unexpected_exception_times += 1 continue else: reply = handle_exception("{}会话调用API失败:{}".format(session_name, e), @@ -99,9 +117,9 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, except openai.error.InvalidRequestError as e: reply = handle_exception("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或" "completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format( - session_name, e), "[bot]err:API调用参数错误,请联系作者,或等待修复") + session_name, e), "[bot]err:API调用参数错误,请联系管理员,或等待修复") except openai.error.ServiceUnavailableError as e: - reply = handle_exception("{}API调用服务不可用:{}".format(session_name, e), "[bot]err:API调用服务不可用,请重试或联系作者,或等待修复") + reply = handle_exception("{}API调用服务不可用:{}".format(session_name, e), "[bot]err:API调用服务不可用,请重试或联系管理员,或等待修复") except Exception as e: logging.exception(e) reply = handle_exception("{}会话处理异常:{}".format(session_name, e), "[bot]err:{}".format(e))