From 56664f9fbcfd964b7f2fb4135d1d6e21862ac201 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sat, 14 Jan 2023 20:34:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(qqbot):=20=E7=8B=AC=E7=AB=8B=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E5=92=8C=E6=99=AE=E9=80=9A=E6=B6=88=E6=81=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 2 +- pkg/qqbot/command.py | 232 +++++++++++++++++++++++++++++++++++++ pkg/qqbot/manager.py | 12 +- pkg/qqbot/message.py | 54 +++++++++ pkg/qqbot/process.py | 267 ++----------------------------------------- 5 files changed, 301 insertions(+), 266 deletions(-) create mode 100644 pkg/qqbot/command.py create mode 100644 pkg/qqbot/message.py diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index ed9f5c30..1e7fd489 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -139,7 +139,7 @@ class PluginHost: hook(plugin['instance'], **kwargs) if event_context.is_prevented_default(): - logging.debug("插件 {} 要求阻止事件{}的默认行为".format(plugin['name'], event_name)) + logging.debug("插件 {} 已要求阻止事件 {} 的默认行为".format(plugin['name'], event_name)) if event_context.is_prevented_postorder(): logging.debug("插件 {} 阻止了后序插件的执行".format(plugin['name'])) diff --git a/pkg/qqbot/command.py b/pkg/qqbot/command.py new file mode 100644 index 00000000..001f27bd --- /dev/null +++ b/pkg/qqbot/command.py @@ -0,0 +1,232 @@ +# 指令处理模块 +import logging +import json +import datetime +import threading + +import pkg.openai.session +import pkg.openai.manager +import pkg.utils.reloader +import pkg.utils.updater +import pkg.utils.context +import pkg.qqbot.message + +from mirai import Image + + +def config_operation(cmd, params): + reply = [] + config = pkg.utils.context.get_config() + reply_str = "" + if len(params) == 0: + reply = ["[bot]err:请输入配置项"] + else: + cfg_name = params[0] + if cfg_name == 'all': + reply_str = "[bot]所有配置项:\n\n" + for cfg in dir(config): + if not cfg.startswith('__') and not cfg == 'logging': + # 根据配置项类型进行格式化,如果是字典则转换为json并格式化 + if isinstance(getattr(config, cfg), str): + reply_str += "{}: \"{}\"\n".format(cfg, getattr(config, cfg)) + elif isinstance(getattr(config, cfg), dict): + # 不进行unicode转义,并格式化 + reply_str += "{}: {}\n".format(cfg, + json.dumps(getattr(config, cfg), + ensure_ascii=False, indent=4)) + else: + reply_str += "{}: {}\n".format(cfg, getattr(config, cfg)) + reply = [reply_str] + elif cfg_name in dir(config): + if len(params) == 1: + # 按照配置项类型进行格式化 + if isinstance(getattr(config, cfg_name), str): + reply_str = "[bot]配置项{}: \"{}\"\n".format(cfg_name, getattr(config, cfg_name)) + elif isinstance(getattr(config, cfg_name), dict): + reply_str = "[bot]配置项{}: {}\n".format(cfg_name, + json.dumps(getattr(config, cfg_name), + ensure_ascii=False, indent=4)) + else: + reply_str = "[bot]配置项{}: {}\n".format(cfg_name, getattr(config, cfg_name)) + reply = [reply_str] + else: + cfg_value = " ".join(params[1:]) + # 类型转换,如果是json则转换为字典 + if cfg_value == 'true': + cfg_value = True + elif cfg_value == 'false': + cfg_value = False + elif cfg_value.isdigit(): + cfg_value = int(cfg_value) + elif cfg_value.startswith('{') and cfg_value.endswith('}'): + cfg_value = json.loads(cfg_value) + else: + try: + cfg_value = float(cfg_value) + except ValueError: + pass + + # 检查类型是否匹配 + if isinstance(getattr(config, cfg_name), type(cfg_value)): + setattr(config, cfg_name, cfg_value) + pkg.utils.context.set_config(config) + reply = ["[bot]配置项{}修改成功".format(cfg_name)] + else: + reply = ["[bot]err:配置项{}类型不匹配".format(cfg_name)] + + else: + reply = ["[bot]err:未找到配置项 {}".format(cfg_name)] + + return reply + + +def process_command(session_name: str, text_message: str, mgr, config, launcher_type: str, launcher_id: int) -> list: + reply = [] + try: + logging.info( + "[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + ( + "..." if len(text_message) > 20 else ""))) + + cmd = text_message[1:].strip().split(' ')[0] + + params = text_message[1:].strip().split(' ')[1:] + if cmd == 'help': + reply = ["[bot]" + config.help_message] + elif cmd == 'reset': + pkg.openai.session.get_session(session_name).reset(explicit=True) + reply = ["[bot]会话已重置"] + elif cmd == 'last': + result = pkg.openai.session.get_session(session_name).last_session() + if result is None: + reply = ["[bot]没有前一次的对话"] + else: + datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime( + '%Y-%m-%d %H:%M:%S') + reply = ["[bot]已切换到前一次的对话:\n创建时间:{}\n".format( + datetime_str) + result.prompt[ + :min(100, + len(result.prompt))] + \ + ("..." if len(result.prompt) > 100 else "#END#")] + elif cmd == 'next': + result = pkg.openai.session.get_session(session_name).next_session() + if result is None: + reply = ["[bot]没有后一次的对话"] + else: + datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime( + '%Y-%m-%d %H:%M:%S') + reply = ["[bot]已切换到后一次的对话:\n创建时间:{}\n".format( + datetime_str) + result.prompt[ + :min(100, + len(result.prompt))] + \ + ("..." if len(result.prompt) > 100 else "#END#")] + elif cmd == 'prompt': + reply = ["[bot]当前对话所有内容:\n" + pkg.openai.session.get_session(session_name).prompt] + elif cmd == 'list': + pkg.openai.session.get_session(session_name).persistence() + page = 0 + + if len(params) > 0: + try: + page = int(params[0]) + except ValueError: + pass + + results = pkg.openai.session.get_session(session_name).list_history(page=page) + if len(results) == 0: + reply = ["[bot]第{}页没有历史会话".format(page)] + else: + reply_str = "[bot]历史会话 第{}页:\n".format(page) + current = -1 + for i in range(len(results)): + # 时间(使用create_timestamp转换) 序号 部分内容 + datetime_obj = datetime.datetime.fromtimestamp(results[i]['create_timestamp']) + reply_str += "#{} 创建:{} {}\n".format(i + page * 10, + datetime_obj.strftime("%Y-%m-%d %H:%M:%S"), + results[i]['prompt'][ + :min(20, len(results[i]['prompt']))]) + if results[i]['create_timestamp'] == pkg.openai.session.get_session( + session_name).create_timestamp: + current = i + page * 10 + + reply_str += "\n以上信息倒序排列" + if current != -1: + reply_str += ",当前会话是 #{}\n".format(current) + else: + reply_str += ",当前处于全新会话或不在此页" + + reply = [reply_str] + elif cmd == 'resend': + session = pkg.openai.session.get_session(session_name) + to_send = session.undo() + + reply = pkg.qqbot.message.process_normal_message(to_send, mgr, config, launcher_type, launcher_id) + elif cmd == 'usage': + reply_str = "[bot]各api-key使用情况:\n\n" + + api_keys = pkg.utils.context.get_openai_manager().key_mgr.api_key + for key_name in api_keys: + text_length = pkg.utils.context.get_openai_manager().audit_mgr \ + .get_text_length_of_key(api_keys[key_name]) + image_count = pkg.utils.context.get_openai_manager().audit_mgr \ + .get_image_count_of_key(api_keys[key_name]) + reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length), + int(image_count)) + + reply = [reply_str] + elif cmd == 'draw': + if len(params) == 0: + reply = ["[bot]err:请输入图片描述文字"] + else: + session = pkg.openai.session.get_session(session_name) + + res = session.draw_image(" ".join(params)) + + logging.debug("draw_image result:{}".format(res)) + reply = [Image(url=res['data'][0]['url'])] + if not (hasattr(config, 'include_image_description') + and not config.include_image_description): + reply.append(" ".join(params)) + elif cmd == 'version': + reply_str = "[bot]当前版本:\n{}\n".format(pkg.utils.updater.get_current_version_info()) + try: + if pkg.utils.updater.is_new_version_available(): + reply_str += "\n有新版本可用,请使用命令 !update 进行更新" + except: + pass + + reply = [reply_str] + elif cmd == 'reload' and launcher_type == 'person' and launcher_id == config.admin_qq: + def reload_task(): + pkg.utils.reloader.reload_all() + + threading.Thread(target=reload_task, daemon=True).start() + elif cmd == 'update' and launcher_type == 'person' and launcher_id == config.admin_qq: + def update_task(): + try: + if pkg.utils.updater.update_all(): + pkg.utils.reloader.reload_all(notify=False) + pkg.utils.context.get_qqbot_manager().notify_admin("更新完成") + else: + pkg.utils.context.get_qqbot_manager().notify_admin("无新版本") + except Exception as e0: + pkg.utils.context.get_qqbot_manager().notify_admin("更新失败:{}".format(e0)) + return + + threading.Thread(target=update_task, daemon=True).start() + + reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."] + elif cmd == 'cfg' and launcher_type == 'person' and launcher_id == config.admin_qq: + reply = config_operation(cmd, params) + else: + if cmd.startswith("~") and launcher_type == 'person' and launcher_id == config.admin_qq: + config_item = cmd[1:] + params = [config_item] + params + reply = config_operation("cfg", params) + else: + reply = ["[bot]err:未知的指令或权限不足: " + cmd] + except Exception as e: + mgr.notify_admin("{}指令执行失败:{}".format(session_name, e)) + logging.exception(e) + reply = ["[bot]err:{}".format(e)] + + return reply diff --git a/pkg/qqbot/manager.py b/pkg/qqbot/manager.py index 79bebfce..b23e8d2d 100644 --- a/pkg/qqbot/manager.py +++ b/pkg/qqbot/manager.py @@ -105,9 +105,9 @@ class QQBotManager: "sender_id": event.sender.id, "message_chain": event.message_chain, } - event = plugin_host.emit(plugin_models.PersonMessage, **args) + plugin_event = plugin_host.emit(plugin_models.PersonMessage, **args) - if event.is_prevented_default(): + if plugin_event.is_prevented_default(): return go(self.on_person_message, (event,)) @@ -121,9 +121,9 @@ class QQBotManager: "sender_id": event.sender.id, "message_chain": event.message_chain, } - event = plugin_host.emit(plugin_models.PersonMessage, **args) + plugin_event = plugin_host.emit(plugin_models.PersonMessage, **args) - if event.is_prevented_default(): + if plugin_event.is_prevented_default(): return go(self.on_person_message, (event,)) @@ -137,9 +137,9 @@ class QQBotManager: "sender_id": event.sender.id, "message_chain": event.message_chain, } - event = plugin_host.emit(plugin_models.GroupMessage, **args) + plugin_event = plugin_host.emit(plugin_models.GroupMessage, **args) - if event.is_prevented_default(): + if plugin_event.is_prevented_default(): return go(self.on_group_message, (event,)) diff --git a/pkg/qqbot/message.py b/pkg/qqbot/message.py new file mode 100644 index 00000000..e2db9989 --- /dev/null +++ b/pkg/qqbot/message.py @@ -0,0 +1,54 @@ +# 普通消息处理模块 +import logging +import openai +import pkg.utils.context +import pkg.openai.session + + +def process_normal_message(text_message: str, mgr, config, launcher_type: str, launcher_id: int) -> list: + session_name = f"{launcher_type}_{launcher_id}" + logging.info("[{}]发送消息:{}".format(session_name, text_message[:min(20, len(text_message))] + ( + "..." if len(text_message) > 20 else ""))) + + session = pkg.openai.session.get_session(session_name) + + while True: + try: + prefix = "[GPT]" if hasattr(config, "show_prefix") and config.show_prefix else "" + reply = [prefix + session.append(text_message)] + except openai.error.APIConnectionError as e: + mgr.notify_admin("{}会话调用API失败:{}".format(session_name, e)) + 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() + 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 + except openai.error.InvalidRequestError as e: + mgr.notify_admin("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或" + "completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format( + session_name, e)) + reply = ["[bot]err:API调用参数错误,请联系作者,或等待修复"] + except openai.error.ServiceUnavailableError as e: + # mgr.notify_admin("{}API调用服务不可用:{}".format(session_name, e)) + reply = ["[bot]err:API调用服务暂不可用,请尝试重试"] + except Exception as e: + logging.exception(e) + reply = ["[bot]err:{}".format(e)] + break + + return reply \ No newline at end of file diff --git a/pkg/qqbot/process.py b/pkg/qqbot/process.py index 9aabac9d..c17c7ffa 100644 --- a/pkg/qqbot/process.py +++ b/pkg/qqbot/process.py @@ -20,76 +20,12 @@ import pkg.openai.manager import pkg.utils.reloader import pkg.utils.updater import pkg.utils.context +import pkg.qqbot.message +import pkg.qqbot.command processing = [] -def config_operation(cmd, params): - reply = [] - config = pkg.utils.context.get_config() - reply_str = "" - if len(params) == 0: - reply = ["[bot]err:请输入配置项"] - else: - cfg_name = params[0] - if cfg_name == 'all': - reply_str = "[bot]所有配置项:\n\n" - for cfg in dir(config): - if not cfg.startswith('__') and not cfg == 'logging': - # 根据配置项类型进行格式化,如果是字典则转换为json并格式化 - if isinstance(getattr(config, cfg), str): - reply_str += "{}: \"{}\"\n".format(cfg, getattr(config, cfg)) - elif isinstance(getattr(config, cfg), dict): - # 不进行unicode转义,并格式化 - reply_str += "{}: {}\n".format(cfg, - json.dumps(getattr(config, cfg), - ensure_ascii=False, indent=4)) - else: - reply_str += "{}: {}\n".format(cfg, getattr(config, cfg)) - reply = [reply_str] - elif cfg_name in dir(config): - if len(params) == 1: - # 按照配置项类型进行格式化 - if isinstance(getattr(config, cfg_name), str): - reply_str = "[bot]配置项{}: \"{}\"\n".format(cfg_name, getattr(config, cfg_name)) - elif isinstance(getattr(config, cfg_name), dict): - reply_str = "[bot]配置项{}: {}\n".format(cfg_name, - json.dumps(getattr(config, cfg_name), - ensure_ascii=False, indent=4)) - else: - reply_str = "[bot]配置项{}: {}\n".format(cfg_name, getattr(config, cfg_name)) - reply = [reply_str] - else: - cfg_value = " ".join(params[1:]) - # 类型转换,如果是json则转换为字典 - if cfg_value == 'true': - cfg_value = True - elif cfg_value == 'false': - cfg_value = False - elif cfg_value.isdigit(): - cfg_value = int(cfg_value) - elif cfg_value.startswith('{') and cfg_value.endswith('}'): - cfg_value = json.loads(cfg_value) - else: - try: - cfg_value = float(cfg_value) - except ValueError: - pass - - # 检查类型是否匹配 - if isinstance(getattr(config, cfg_name), type(cfg_value)): - setattr(config, cfg_name, cfg_value) - pkg.utils.context.set_config(config) - reply = ["[bot]配置项{}修改成功".format(cfg_name)] - else: - reply = ["[bot]err:配置项{}类型不匹配".format(cfg_name)] - - else: - reply = ["[bot]err:未找到配置项 {}".format(cfg_name)] - - return reply - - @func_set_timeout(config_init_import.process_message_timeout) def process_message(launcher_type: str, launcher_id: int, text_message: str, message_chain: MessageChain, sender_id: int) -> MessageChain: @@ -120,210 +56,23 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes pkg.openai.session.get_session(session_name).acquire_response_lock() + # 处理消息 try: if session_name in processing: pkg.openai.session.get_session(session_name).release_response_lock() return MessageChain([Plain("[bot]err:正在处理中,请稍后再试")]) - processing.append(session_name) - config = pkg.utils.context.get_config() - is_message = True + processing.append(session_name) try: if text_message.startswith('!') or text_message.startswith("!"): # 指令 - is_message = False - try: - logging.info( - "[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))] + ( - "..." if len(text_message) > 20 else ""))) + reply = pkg.qqbot.command.process_command(session_name, text_message, + mgr, config, launcher_type, launcher_id) - cmd = text_message[1:].strip().split(' ')[0] - - params = text_message[1:].strip().split(' ')[1:] - if cmd == 'help': - reply = ["[bot]" + config.help_message] - elif cmd == 'reset': - pkg.openai.session.get_session(session_name).reset(explicit=True) - reply = ["[bot]会话已重置"] - elif cmd == 'last': - result = pkg.openai.session.get_session(session_name).last_session() - if result is None: - reply = ["[bot]没有前一次的对话"] - else: - datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime( - '%Y-%m-%d %H:%M:%S') - reply = ["[bot]已切换到前一次的对话:\n创建时间:{}\n".format( - datetime_str) + result.prompt[ - :min(100, - len(result.prompt))] + \ - ("..." if len(result.prompt) > 100 else "#END#")] - elif cmd == 'next': - result = pkg.openai.session.get_session(session_name).next_session() - if result is None: - reply = ["[bot]没有后一次的对话"] - else: - datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime( - '%Y-%m-%d %H:%M:%S') - reply = ["[bot]已切换到后一次的对话:\n创建时间:{}\n".format( - datetime_str) + result.prompt[ - :min(100, - len(result.prompt))] + \ - ("..." if len(result.prompt) > 100 else "#END#")] - elif cmd == 'prompt': - reply = ["[bot]当前对话所有内容:\n" + pkg.openai.session.get_session(session_name).prompt] - elif cmd == 'list': - pkg.openai.session.get_session(session_name).persistence() - page = 0 - - if len(params) > 0: - try: - page = int(params[0]) - except ValueError: - pass - - results = pkg.openai.session.get_session(session_name).list_history(page=page) - if len(results) == 0: - reply = ["[bot]第{}页没有历史会话".format(page)] - else: - reply_str = "[bot]历史会话 第{}页:\n".format(page) - current = -1 - for i in range(len(results)): - # 时间(使用create_timestamp转换) 序号 部分内容 - datetime_obj = datetime.datetime.fromtimestamp(results[i]['create_timestamp']) - reply_str += "#{} 创建:{} {}\n".format(i + page * 10, - datetime_obj.strftime("%Y-%m-%d %H:%M:%S"), - results[i]['prompt'][ - :min(20, len(results[i]['prompt']))]) - if results[i]['create_timestamp'] == pkg.openai.session.get_session( - session_name).create_timestamp: - current = i + page * 10 - - reply_str += "\n以上信息倒序排列" - if current != -1: - reply_str += ",当前会话是 #{}\n".format(current) - else: - reply_str += ",当前处于全新会话或不在此页" - - reply = [reply_str] - elif cmd == 'resend': - session = pkg.openai.session.get_session(session_name) - to_send = session.undo() - text_message = to_send - is_message = True - elif cmd == 'usage': - reply_str = "[bot]各api-key使用情况:\n\n" - - api_keys = pkg.utils.context.get_openai_manager().key_mgr.api_key - for key_name in api_keys: - text_length = pkg.utils.context.get_openai_manager().audit_mgr \ - .get_text_length_of_key(api_keys[key_name]) - image_count = pkg.utils.context.get_openai_manager().audit_mgr \ - .get_image_count_of_key(api_keys[key_name]) - reply_str += "{}:\n - 文本长度:{}\n - 图片数量:{}\n".format(key_name, int(text_length), - int(image_count)) - - reply = [reply_str] - elif cmd == 'draw': - if len(params) == 0: - reply = ["[bot]err:请输入图片描述文字"] - else: - session = pkg.openai.session.get_session(session_name) - - res = session.draw_image(" ".join(params)) - - logging.debug("draw_image result:{}".format(res)) - reply = [Image(url=res['data'][0]['url'])] - if not (hasattr(config, 'include_image_description') - and not config.include_image_description): - reply.append(" ".join(params)) - elif cmd == 'version': - reply_str = "[bot]当前版本:\n{}\n".format(pkg.utils.updater.get_current_version_info()) - try: - if pkg.utils.updater.is_new_version_available(): - reply_str += "\n有新版本可用,请使用命令 !update 进行更新" - except: - pass - - reply = [reply_str] - elif cmd == 'reload' and launcher_type == 'person' and launcher_id == config.admin_qq: - def reload_task(): - pkg.utils.reloader.reload_all() - - threading.Thread(target=reload_task, daemon=True).start() - elif cmd == 'update' and launcher_type == 'person' and launcher_id == config.admin_qq: - def update_task(): - try: - if pkg.utils.updater.update_all(): - pkg.utils.reloader.reload_all(notify=False) - pkg.utils.context.get_qqbot_manager().notify_admin("更新完成") - else: - pkg.utils.context.get_qqbot_manager().notify_admin("无新版本") - except Exception as e0: - pkg.utils.context.get_qqbot_manager().notify_admin("更新失败:{}".format(e0)) - return - - threading.Thread(target=update_task, daemon=True).start() - - reply = ["[bot]正在更新,请耐心等待,请勿重复发起更新..."] - elif cmd == 'cfg' and launcher_type == 'person' and launcher_id == config.admin_qq: - reply = config_operation(cmd, params) - else: - if cmd.startswith("~") and launcher_type == 'person' and launcher_id == config.admin_qq: - config_item = cmd[1:] - params = [config_item] + params - reply = config_operation("cfg", params) - else: - reply = ["[bot]err:未知的指令或权限不足: " + cmd] - except Exception as e: - mgr.notify_admin("{}指令执行失败:{}".format(session_name, e)) - logging.exception(e) - reply = ["[bot]err:{}".format(e)] - - if is_message: # 消息 - logging.info("[{}]发送消息:{}".format(session_name, text_message[:min(20, len(text_message))] + ( - "..." if len(text_message) > 20 else ""))) - - session = pkg.openai.session.get_session(session_name) - - while True: - try: - prefix = "[GPT]" if hasattr(config, "show_prefix") and config.show_prefix else "" - reply = [prefix + session.append(text_message)] - except openai.error.APIConnectionError as e: - mgr.notify_admin("{}会话调用API失败:{}".format(session_name, e)) - 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() - 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 - except openai.error.InvalidRequestError as e: - mgr.notify_admin("{}API调用参数错误:{}\n\n这可能是由于config.py中的prompt_submit_length参数或" - "completion_api_params中的max_tokens参数数值过大导致的,请尝试将其降低".format( - session_name, e)) - reply = ["[bot]err:API调用参数错误,请联系作者,或等待修复"] - except openai.error.ServiceUnavailableError as e: - # mgr.notify_admin("{}API调用服务不可用:{}".format(session_name, e)) - reply = ["[bot]err:API调用服务暂不可用,请尝试重试"] - except Exception as e: - logging.exception(e) - reply = ["[bot]err:{}".format(e)] - break + else: # 消息 + reply = pkg.qqbot.message.process_normal_message(text_message, mgr, config, launcher_type, launcher_id) if reply is not None and type(reply[0]) == str: logging.info(