From a38f3b9c28e739bd85553ffeafd639435107f4cf Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Fri, 3 Mar 2023 10:08:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E8=B6=85=E9=99=90=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/qqbot/core.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/cores/qqbot/core.py b/cores/qqbot/core.py index 1f7241eaa..4c1642e7e 100644 --- a/cores/qqbot/core.py +++ b/cores/qqbot/core.py @@ -131,6 +131,7 @@ def dump_history(): # 上传统计信息并检查更新 def upload(): global object_id + global version while True: addr = '' try: @@ -147,7 +148,7 @@ def upload(): 'Content-Type': 'application/json' } key_stat = chatgpt.get_key_stat() - d = {"data": {"guild_count": guild_count, "guild_msg_count": guild_msg_count, "guild_direct_msg_count": guild_direct_msg_count, "session_count": session_count, 'addr': addr, 'winver': '2.3', 'key_stat':key_stat}} + d = {"data": {"guild_count": guild_count, "guild_msg_count": guild_msg_count, "guild_direct_msg_count": guild_direct_msg_count, "session_count": session_count, 'addr': addr, 'winver': version, 'key_stat':key_stat}} d = json.dumps(d).encode("utf-8") res = requests.put(f'https://uqfxtww1.lc-cn-n1-shared.com/1.1/classes/bot_record/{object_id}', headers = headers, data = d) if json.loads(res.text)['code'] == 1: @@ -462,19 +463,20 @@ def oper_msg(message, at=False, loop=None): cache_data_list = session_dict[session_id] cache_prompt = get_prompts_by_cache_list(cache_data_list) cache_prompt += "\nHuman: "+ qq_msg + "\nAI: " + # 请求chatGPT获得结果 try: chatgpt_res, current_usage_tokens = get_chatGPT_response(prompts_str=cache_prompt) - except (PromptExceededError) as e: - print("token超限, 清空对应缓存") - session_dict[session_id] = [] - cache_data_list = [] - cache_prompt = "Human: "+ qq_msg + "\nAI: " - chatgpt_res, current_usage_tokens = get_chatGPT_response(prompts_str=cache_prompt) except (BaseException) as e: - print("OpenAI API错误:(") - if 'exceeded' in str(e): - send_qq_msg(message, f"OpenAI API错误。原因:\n{str(e)} \n超额了。您可自己搭建一个机器人(Github仓库:QQChannelChatGPT)") + print("[System-Err] OpenAI API错误。原因如下:\n"+str(e)) + if 'maximum context length' in str(e): + print("token超限, 清空对应缓存") + session_dict[session_id] = [] + cache_data_list = [] + cache_prompt = "Human: "+ qq_msg + "\nAI: " + chatgpt_res, current_usage_tokens = get_chatGPT_response(prompts_str=cache_prompt) + elif 'exceeded' in str(e): + send_qq_msg(message, f"OpenAI API错误。原因:\n{str(e)} \n超额了。可自己搭建一个机器人(Github仓库:QQChannelChatGPT)") else: send_qq_msg(message, f"OpenAI API错误。原因如下:\n{str(e)} \n前往官方频道反馈~") return