From e64c26e6176ae5d09b7c37495a29224769e8c780 Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sat, 28 Oct 2023 19:53:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B4=A7=E6=80=A5=E4=BF=AE=E5=A4=8D=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request_llm/bridge_all.py | 2 +- request_llm/bridge_chatgpt.py | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/request_llm/bridge_all.py b/request_llm/bridge_all.py index 70e2c43..f85d1b6 100644 --- a/request_llm/bridge_all.py +++ b/request_llm/bridge_all.py @@ -505,7 +505,7 @@ if len(AZURE_CFG_ARRAY) > 0: } }) if azure_model_name not in AVAIL_LLM_MODELS: - azure_model_name += [azure_model_name] + AVAIL_LLM_MODELS += [azure_model_name] diff --git a/request_llm/bridge_chatgpt.py b/request_llm/bridge_chatgpt.py index f91eea5..9903da9 100644 --- a/request_llm/bridge_chatgpt.py +++ b/request_llm/bridge_chatgpt.py @@ -45,16 +45,18 @@ def decode_chunk(chunk): chunk_decoded = chunk.decode() chunkjson = None has_choices = False + choice_valid = False has_content = False has_role = False try: chunkjson = json.loads(chunk_decoded[6:]) - has_choices = ('choices' in chunkjson) and (len(chunkjson['choices']) > 0) - if has_choices: has_content = "content" in chunkjson['choices'][0]["delta"] - if has_choices: has_role = "role" in chunkjson['choices'][0]["delta"] + has_choices = 'choices' in chunkjson + if has_choices: choice_valid = (len(chunkjson['choices']) > 0) + if has_choices and choice_valid: has_content = "content" in chunkjson['choices'][0]["delta"] + if has_choices and choice_valid: has_role = "role" in chunkjson['choices'][0]["delta"] except: pass - return chunk_decoded, chunkjson, has_choices, has_content, has_role + return chunk_decoded, chunkjson, has_choices, choice_valid, has_content, has_role from functools import lru_cache @lru_cache(maxsize=32) @@ -64,7 +66,6 @@ def verify_endpoint(endpoint): """ if "你亲手写的api名称" in endpoint: raise ValueError("Endpoint不正确, 请检查AZURE_ENDPOINT的配置! 当前的Endpoint为:" + endpoint) - print(endpoint) return endpoint def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", observe_window=None, console_slience=False): @@ -97,7 +98,7 @@ def predict_no_ui_long_connection(inputs, llm_kwargs, history=[], sys_prompt="", if retry > MAX_RETRY: raise TimeoutError if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……') - stream_response = response.iter_lines() + stream_response = response.iter_lines() result = '' json_data = None while True: @@ -227,7 +228,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp return # 提前读取一些信息 (用于判断异常) - chunk_decoded, chunkjson, has_choices, has_content, has_role = decode_chunk(chunk) + chunk_decoded, chunkjson, has_choices, choice_valid, has_content, has_role = decode_chunk(chunk) if is_head_of_the_stream and (r'"object":"error"' not in chunk_decoded) and (r"content" not in chunk_decoded): # 数据流的第一帧不携带content @@ -235,7 +236,7 @@ def predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_promp if chunk: try: - if not has_choices: + if has_choices and not choice_valid: # 一些垃圾第三方接口的出现这样的错误 continue # 前者是API2D的结束条件,后者是OPENAI的结束条件 @@ -287,6 +288,8 @@ def handle_error(inputs, llm_kwargs, chatbot, history, chunk_decoded, error_msg) chatbot[-1] = (chatbot[-1][0], "[Local Message] Your account is not active. OpenAI以账户失效为由, 拒绝服务." + openai_website) elif "associated with a deactivated account" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] You are associated with a deactivated account. OpenAI以账户失效为由, 拒绝服务." + openai_website) + elif "API key has been deactivated" in error_msg: + chatbot[-1] = (chatbot[-1][0], "[Local Message] API key has been deactivated. OpenAI以账户失效为由, 拒绝服务." + openai_website) elif "bad forward key" in error_msg: chatbot[-1] = (chatbot[-1][0], "[Local Message] Bad forward key. API2D账户额度不足.") elif "Not enough point" in error_msg: