diff --git a/config.py b/config.py index ea17789..a894068 100644 --- a/config.py +++ b/config.py @@ -18,6 +18,9 @@ TIMEOUT_SECONDS = 20 # 网页的端口, -1代表随机端口 WEB_PORT = -1 +# 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制 +MAX_RETRY = 2 + # 检查一下是不是忘了改config if API_KEY == "sk-此处填API秘钥": assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行" \ No newline at end of file diff --git a/functional_crazy.py b/functional_crazy.py index 6b7d48c..8b641d9 100644 --- a/functional_crazy.py +++ b/functional_crazy.py @@ -30,7 +30,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx 前言 = "接下来请你分析自己的程序构成,别紧张," if index==0 else "" i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{fp},文件代码是 ```{file_content}```' i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}' - chatbot.append((i_say_show_user, "[waiting gpt response]")) + chatbot.append((i_say_show_user, "[local] waiting gpt response.")) yield chatbot, history, '正常' if not fast_debug: @@ -43,7 +43,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx time.sleep(2) i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。' - chatbot.append((i_say, "[waiting gpt response]")) + chatbot.append((i_say, "[local] waiting gpt response.")) yield chatbot, history, '正常' if not fast_debug: @@ -73,7 +73,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, 前言 = "接下来请你逐文件分析下面的工程" if index==0 else "" i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{os.path.relpath(fp, project_folder)},文件代码是 ```{file_content}```' i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}' - chatbot.append((i_say_show_user, "[waiting gpt response]")) + chatbot.append((i_say_show_user, "[local] waiting gpt response.")) print('[1] yield chatbot, history') yield chatbot, history, '正常' @@ -98,7 +98,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot, all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)]) i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{all_file})。' - chatbot.append((i_say, "[waiting gpt response]")) + chatbot.append((i_say, "[local] waiting gpt response.")) yield chatbot, history, '正常' if not fast_debug: diff --git a/predict.py b/predict.py index fd597d7..8a18710 100644 --- a/predict.py +++ b/predict.py @@ -9,10 +9,10 @@ import importlib # config_private.py放自己的秘密如API和代理网址 # 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件 -try: from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS -except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS +try: from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY +except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY -timeout_bot_msg = 'Request timeout, network error. please check proxy settings in config.py.' +timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.' def get_full_error(chunk, stream_response): while True: @@ -63,13 +63,18 @@ def predict_no_ui(inputs, top_p, temperature, history=[]): "Content-Type": "application/json", "Authorization": f"Bearer {API_KEY}" } - try: - # make a POST request to the API endpoint using the requests.post method, passing in stream=True - response = requests.post(API_URL, headers=headers, proxies=proxies, - json=payload, stream=True, timeout=TIMEOUT_SECONDS*2) - except Exception as e: - traceback.print_exc() - raise TimeoutError + + retry = 0 + while True: + try: + # make a POST request to the API endpoint using the requests.post method, passing in stream=True + response = requests.post(API_URL, headers=headers, proxies=proxies, + json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break + except TimeoutError as e: + retry += 1 + traceback.print_exc() + if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……') + if retry > MAX_RETRY: raise TimeoutError try: result = json.loads(response.text)["choices"][0]["message"]["content"] @@ -146,14 +151,18 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='' history.append(inputs) - try: - # make a POST request to the API endpoint using the requests.post method, passing in stream=True - response = requests.post(API_URL, headers=headers, proxies=proxies, - json=payload, stream=True, timeout=TIMEOUT_SECONDS) - except: - chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg)) - yield chatbot, history, "请求超时" - raise TimeoutError + retry = 0 + while True: + try: + # make a POST request to the API endpoint using the requests.post method, passing in stream=True + response = requests.post(API_URL, headers=headers, proxies=proxies, + json=payload, stream=True, timeout=TIMEOUT_SECONDS);break + except: + retry += 1 + chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg)) + retry_msg = f",正在重试 ({retry}/{MAX_RETRY}) ……" if MAX_RETRY > 0 else "" + yield chatbot, history, "请求超时"+retry_msg + if retry > MAX_RETRY: raise TimeoutError token_counter = 0 partial_words = "" @@ -194,7 +203,7 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt='' chunk = get_full_error(chunk, stream_response) error_msg = chunk.decode() if "reduce the length" in error_msg: - chatbot[-1] = (history[-1], "老铁,输入的文本太长了") + chatbot[-1] = (history[-1], "[local] input is too long, reduce input or clear history.") yield chatbot, history, "Json解析不合常规,很可能是文本过长" + error_msg return