From 762f5ea30f0b714be582dcf134f84b1475b8ab75 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Wed, 5 Apr 2023 19:10:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AE=98=E6=96=B9?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E6=A8=A1=E5=9E=8B=E5=88=87=E6=8D=A2key?= =?UTF-8?q?=E6=97=B6=E5=8F=AF=E8=83=BD=E5=8F=91=E7=94=9F=E7=9A=84=E6=AD=BB?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E9=97=AE=E9=A2=98=EF=BC=9B=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E8=AF=AD=E8=A8=80=E6=A8=A1=E5=9E=8B=E6=97=B6?= =?UTF-8?q?=E5=89=8D=E7=BC=80=E4=B8=8D=E6=9B=B4=E6=96=B0=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/qqbot/core.py | 30 ++++++++++------------ main.py | 2 +- model/provider/provider_openai_official.py | 5 ++++ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cores/qqbot/core.py b/cores/qqbot/core.py index 485d9b55d..4a4c0c6e5 100644 --- a/cores/qqbot/core.py +++ b/cores/qqbot/core.py @@ -76,7 +76,7 @@ gpt_config = {} # 百度内容审核实例 baidu_judge = None # 回复前缀 -reply_prefix = '' +reply_prefix = {} def new_sub_thread(func, args=()): @@ -162,12 +162,11 @@ def upload(): def initBot(cfg, prov): global chatgpt, provider, rev_chatgpt, baidu_judge, rev_edgegpt, chosen_provider global reply_prefix, gpt_config, config, uniqueSession, frequency_count, frequency_time,announcement, direct_message_mode, version - global command_openai_official, command_rev_chatgpt, command_rev_edgegpt + global command_openai_official, command_rev_chatgpt, command_rev_edgegpt,reply_prefix provider = prov config = cfg - reply_prefix_config = None if 'reply_prefix' in cfg: - reply_prefix_config = cfg['reply_prefix'] + reply_prefix = cfg['reply_prefix'] # 语言模型提供商 if REV_CHATGPT in prov: @@ -176,9 +175,6 @@ def initBot(cfg, prov): from model.command.command_rev_chatgpt import CommandRevChatGPT rev_chatgpt = ProviderRevChatGPT(cfg['rev_ChatGPT']) command_rev_chatgpt = CommandRevChatGPT(cfg['rev_ChatGPT']) - - if REV_CHATGPT in reply_prefix_config: - reply_prefix = reply_prefix_config[REV_CHATGPT] chosen_provider = REV_CHATGPT else: input("[System-err] 请退出本程序, 然后在配置文件中填写rev_ChatGPT相关配置") @@ -188,16 +184,12 @@ def initBot(cfg, prov): from model.command.command_rev_edgegpt import CommandRevEdgeGPT rev_edgegpt = ProviderRevEdgeGPT() command_rev_edgegpt = CommandRevEdgeGPT(rev_edgegpt) - if REV_EDGEGPT in reply_prefix_config: - reply_prefix = reply_prefix_config[REV_EDGEGPT] chosen_provider = REV_EDGEGPT if OPENAI_OFFICIAL in prov: from model.provider.provider_openai_official import ProviderOpenAIOfficial from model.command.command_openai_official import CommandOpenAIOfficial chatgpt = ProviderOpenAIOfficial(cfg['openai']) command_openai_official = CommandOpenAIOfficial(chatgpt) - if OPENAI_OFFICIAL in reply_prefix_config: - reply_prefix = reply_prefix_config[OPENAI_OFFICIAL] chosen_provider = OPENAI_OFFICIAL @@ -348,7 +340,7 @@ def oper_msg(message, at=False, msg_ref = None): session_id = '' user_id = message.author.id user_name = message.author.username - global chosen_provider + global chosen_provider, reply_prefix print(chosen_provider) # 检查发言频率 @@ -430,7 +422,10 @@ def oper_msg(message, at=False, msg_ref = None): return # 请求chatGPT获得结果 try: - chatgpt_res = reply_prefix + chatgpt.text_chat(qq_msg, session_id) + + chatgpt_res = chatgpt.text_chat(qq_msg, session_id) + if OPENAI_OFFICIAL in reply_prefix: + chatgpt_res = reply_prefix[OPENAI_OFFICIAL] + chatgpt_res except (BaseException) as e: print("[System-Err] OpenAI API错误。原因如下:\n"+str(e)) if 'exceeded' in str(e): @@ -455,7 +450,9 @@ def oper_msg(message, at=False, msg_ref = None): send_qq_msg(message, f"指令调用错误: \n{command_result[1]}", msg_ref=msg_ref) return try: - chatgpt_res = reply_prefix+str(rev_chatgpt.text_chat(qq_msg)) + chatgpt_res = str(rev_chatgpt.text_chat(qq_msg)) + if REV_CHATGPT in reply_prefix: + chatgpt_res = reply_prefix[REV_CHATGPT] + chatgpt_res except BaseException as e: print("[System-Err] Rev ChatGPT API错误。原因如下:\n"+str(e)) send_qq_msg(message, f"Rev ChatGPT API错误。原因如下: \n{str(e)} \n前往官方频道反馈~") @@ -477,8 +474,9 @@ def oper_msg(message, at=False, msg_ref = None): send_qq_msg(message, f"[RevBing] 正忙,请稍后再试",msg_ref=msg_ref) return else: - chatgpt_res = reply_prefix - chatgpt_res += str(asyncio.run_coroutine_threadsafe(rev_edgegpt.text_chat(qq_msg), client.loop).result()) + chatgpt_res = str(asyncio.run_coroutine_threadsafe(rev_edgegpt.text_chat(qq_msg), client.loop).result()) + if REV_EDGEGPT in reply_prefix: + chatgpt_res = reply_prefix[REV_EDGEGPT] + chatgpt_res except BaseException as e: print("[System-Err] Rev NewBing API错误。原因如下:\n"+str(e)) send_qq_msg(message, f"Rev NewBing API错误。原因如下:\n{str(e)} \n前往官方频道反馈~") diff --git a/main.py b/main.py index e549b60e4..0b8c0bb70 100644 --- a/main.py +++ b/main.py @@ -34,7 +34,7 @@ def privider_chooser(cfg): l.append('rev_ernie') if 'rev_edgegpt' in cfg and cfg['rev_edgegpt']['enable']: l.append('rev_edgegpt') - if 'openai' in cfg and len(cfg['openai']['key'])>0: + if 'openai' in cfg and cfg['openai']['key'] != None and len(cfg['openai']['key'])>0: l.append('openai_official') return l diff --git a/model/provider/provider_openai_official.py b/model/provider/provider_openai_official.py index 091092d51..e8dde0d56 100644 --- a/model/provider/provider_openai_official.py +++ b/model/provider/provider_openai_official.py @@ -302,6 +302,8 @@ class ProviderOpenAIOfficial(Provider): while True: is_all_exceed = True for key in self.key_stat: + if key == None: + continue if not self.key_stat[key]['exceed']: is_all_exceed = False openai.api_key = key @@ -326,6 +328,9 @@ class ProviderOpenAIOfficial(Provider): if is_all_exceed: print("[System] 所有Key已超额") return None, False + else: + print("[System] 在切换key时程序异常。") + return None, False def getConfigs(self): return self.openai_configs