diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 4e6b0a4..7d5b132 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -262,8 +262,8 @@ class QQBotManager: except FunctionTimedOut: logging.warning("group_{}: 超时,重试中({})".format(event.group.id, i)) pkg.openai.session.get_session('group_{}'.format(event.group.id)).release_response_lock() - if "group_{}".format(event.sender.id) in pkg.qqbot.process.processing: - pkg.qqbot.process.processing.remove('group_{}'.format(event.sender.id)) + if "group_{}".format(event.group.id) in pkg.qqbot.process.processing: + pkg.qqbot.process.processing.remove('group_{}'.format(event.group.id)) failed += 1 continue diff --git a/pkg/qqbot/message.py b/pkg/qqbot/message.py index 1a3c459..186c283 100644 --- a/pkg/qqbot/message.py +++ b/pkg/qqbot/message.py @@ -48,34 +48,40 @@ def process_normal_message(text_message: str, mgr, config, launcher_type: str, reply = ["[bot]err:调用API失败,请重试或联系作者,或等待修复"] except openai.error.RateLimitError as e: logging.debug(type(e)) - # 尝试切换api-key - current_key_name = pkg.utils.context.get_openai_manager().key_mgr.get_key_name( - pkg.utils.context.get_openai_manager().key_mgr.using_key - ) - pkg.utils.context.get_openai_manager().key_mgr.set_current_exceeded() + logging.debug(e.error['message']) - # 触发插件事件 - args = { - 'key_name': current_key_name, - 'usage': pkg.utils.context.get_openai_manager().audit_mgr - .get_usage(pkg.utils.context.get_openai_manager().key_mgr.get_using_key_md5()), - 'exceeded_keys': pkg.utils.context.get_openai_manager().key_mgr.exceeded, - } - event = plugin_host.emit(plugin_models.KeyExceeded, **args) + if 'message' in e.error and e.error['message'].__contains__('You exceeded your current quota'): + # 尝试切换api-key + current_key_name = pkg.utils.context.get_openai_manager().key_mgr.get_key_name( + pkg.utils.context.get_openai_manager().key_mgr.using_key + ) + pkg.utils.context.get_openai_manager().key_mgr.set_current_exceeded() - if not event.is_prevented_default(): - switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch() + # 触发插件事件 + args = { + 'key_name': current_key_name, + 'usage': pkg.utils.context.get_openai_manager().audit_mgr + .get_usage(pkg.utils.context.get_openai_manager().key_mgr.get_using_key_md5()), + 'exceeded_keys': pkg.utils.context.get_openai_manager().key_mgr.exceeded, + } + event = plugin_host.emit(plugin_models.KeyExceeded, **args) - if not switched: - mgr.notify_admin( - "api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format( - current_key_name)) - reply = ["[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)) - reply = ["[bot]err:API调用额度超额,已自动切换,请重新发送消息"] - continue + if not event.is_prevented_default(): + switched, name = pkg.utils.context.get_openai_manager().key_mgr.auto_switch() + + if not switched: + mgr.notify_admin( + "api-key调用额度超限({}),无可用api_key,请向OpenAI账户充值或在config.py中更换api_key".format( + current_key_name)) + reply = ["[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)) + reply = ["[bot]err:API调用额度超额,已自动切换,请重新发送消息"] + continue + else: + mgr.notify_admin("{}会话调用API失败:{}".format(session_name, e)) + reply = ["[bot]err:RateLimitError,请重试或联系作者,或等待修复"] except openai.error.InvalidRequestError as e: mgr.notify_admin("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或" "completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format(