auto retry
Browse files- config.py +3 -0
- functional_crazy.py +4 -4
- predict.py +28 -19
config.py
CHANGED
@@ -18,6 +18,9 @@ TIMEOUT_SECONDS = 20
|
|
18 |
# 网页的端口, -1代表随机端口
|
19 |
WEB_PORT = -1
|
20 |
|
|
|
|
|
|
|
21 |
# 检查一下是不是忘了改config
|
22 |
if API_KEY == "sk-此处填API秘钥":
|
23 |
assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行"
|
|
|
18 |
# 网页的端口, -1代表随机端口
|
19 |
WEB_PORT = -1
|
20 |
|
21 |
+
# 如果OpenAI不响应(网络卡顿、代理失败、KEY失效),重试的次数限制
|
22 |
+
MAX_RETRY = 2
|
23 |
+
|
24 |
# 检查一下是不是忘了改config
|
25 |
if API_KEY == "sk-此处填API秘钥":
|
26 |
assert False, "请在config文件中修改API密钥, 添加海外代理之后再运行"
|
functional_crazy.py
CHANGED
@@ -30,7 +30,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|
30 |
前言 = "接下来请你分析自己的程序构成,别紧张," if index==0 else ""
|
31 |
i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{fp},文件代码是 ```{file_content}```'
|
32 |
i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
33 |
-
chatbot.append((i_say_show_user, "[waiting gpt response
|
34 |
yield chatbot, history, '正常'
|
35 |
|
36 |
if not fast_debug:
|
@@ -43,7 +43,7 @@ def 解析项目本身(txt, top_p, temperature, chatbot, history, systemPromptTx
|
|
43 |
time.sleep(2)
|
44 |
|
45 |
i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。'
|
46 |
-
chatbot.append((i_say, "[waiting gpt response
|
47 |
yield chatbot, history, '正常'
|
48 |
|
49 |
if not fast_debug:
|
@@ -73,7 +73,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
|
|
73 |
前言 = "接下来请你逐文件分析下面的工程" if index==0 else ""
|
74 |
i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{os.path.relpath(fp, project_folder)},文件代码是 ```{file_content}```'
|
75 |
i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
76 |
-
chatbot.append((i_say_show_user, "[waiting gpt response
|
77 |
print('[1] yield chatbot, history')
|
78 |
yield chatbot, history, '正常'
|
79 |
|
@@ -98,7 +98,7 @@ def 解析源代码(file_manifest, project_folder, top_p, temperature, chatbot,
|
|
98 |
|
99 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
100 |
i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{all_file})。'
|
101 |
-
chatbot.append((i_say, "[waiting gpt response
|
102 |
yield chatbot, history, '正常'
|
103 |
|
104 |
if not fast_debug:
|
|
|
30 |
前言 = "接下来请你分析自己的程序构成,别紧张," if index==0 else ""
|
31 |
i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{fp},文件代码是 ```{file_content}```'
|
32 |
i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
33 |
+
chatbot.append((i_say_show_user, "[local] waiting gpt response."))
|
34 |
yield chatbot, history, '正常'
|
35 |
|
36 |
if not fast_debug:
|
|
|
43 |
time.sleep(2)
|
44 |
|
45 |
i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{file_manifest})。'
|
46 |
+
chatbot.append((i_say, "[local] waiting gpt response."))
|
47 |
yield chatbot, history, '正常'
|
48 |
|
49 |
if not fast_debug:
|
|
|
73 |
前言 = "接下来请你逐文件分析下面的工程" if index==0 else ""
|
74 |
i_say = 前言 + f'请对下面的程序文件做一个概述文件名是{os.path.relpath(fp, project_folder)},文件代码是 ```{file_content}```'
|
75 |
i_say_show_user = 前言 + f'[{index}/{len(file_manifest)}] 请对下面的程序文件做一个概述: {os.path.abspath(fp)}'
|
76 |
+
chatbot.append((i_say_show_user, "[local] waiting gpt response."))
|
77 |
print('[1] yield chatbot, history')
|
78 |
yield chatbot, history, '正常'
|
79 |
|
|
|
98 |
|
99 |
all_file = ', '.join([os.path.relpath(fp, project_folder) for index, fp in enumerate(file_manifest)])
|
100 |
i_say = f'根据以上你自己的分析,对程序的整体功能和构架做出概括。然后用一张markdown表格整理每个文件的功能(包括{all_file})。'
|
101 |
+
chatbot.append((i_say, "[local] waiting gpt response."))
|
102 |
yield chatbot, history, '正常'
|
103 |
|
104 |
if not fast_debug:
|
predict.py
CHANGED
@@ -9,10 +9,10 @@ import importlib
|
|
9 |
|
10 |
# config_private.py放自己的秘密如API和代理网址
|
11 |
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
12 |
-
try: from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS
|
13 |
-
except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS
|
14 |
|
15 |
-
timeout_bot_msg = 'Request timeout, network error. please check proxy settings in config.py.'
|
16 |
|
17 |
def get_full_error(chunk, stream_response):
|
18 |
while True:
|
@@ -63,13 +63,18 @@ def predict_no_ui(inputs, top_p, temperature, history=[]):
|
|
63 |
"Content-Type": "application/json",
|
64 |
"Authorization": f"Bearer {API_KEY}"
|
65 |
}
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
try:
|
75 |
result = json.loads(response.text)["choices"][0]["message"]["content"]
|
@@ -146,14 +151,18 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
146 |
|
147 |
history.append(inputs)
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
157 |
|
158 |
token_counter = 0
|
159 |
partial_words = ""
|
@@ -194,7 +203,7 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
|
|
194 |
chunk = get_full_error(chunk, stream_response)
|
195 |
error_msg = chunk.decode()
|
196 |
if "reduce the length" in error_msg:
|
197 |
-
chatbot[-1] = (history[-1], "
|
198 |
yield chatbot, history, "Json解析不合常规,很可能是文本过长" + error_msg
|
199 |
return
|
200 |
|
|
|
9 |
|
10 |
# config_private.py放自己的秘密如API和代理网址
|
11 |
# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件
|
12 |
+
try: from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY
|
13 |
+
except: from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS, MAX_RETRY
|
14 |
|
15 |
+
timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.'
|
16 |
|
17 |
def get_full_error(chunk, stream_response):
|
18 |
while True:
|
|
|
63 |
"Content-Type": "application/json",
|
64 |
"Authorization": f"Bearer {API_KEY}"
|
65 |
}
|
66 |
+
|
67 |
+
retry = 0
|
68 |
+
while True:
|
69 |
+
try:
|
70 |
+
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
71 |
+
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
72 |
+
json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break
|
73 |
+
except TimeoutError as e:
|
74 |
+
retry += 1
|
75 |
+
traceback.print_exc()
|
76 |
+
if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……')
|
77 |
+
if retry > MAX_RETRY: raise TimeoutError
|
78 |
|
79 |
try:
|
80 |
result = json.loads(response.text)["choices"][0]["message"]["content"]
|
|
|
151 |
|
152 |
history.append(inputs)
|
153 |
|
154 |
+
retry = 0
|
155 |
+
while True:
|
156 |
+
try:
|
157 |
+
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
158 |
+
response = requests.post(API_URL, headers=headers, proxies=proxies,
|
159 |
+
json=payload, stream=True, timeout=TIMEOUT_SECONDS);break
|
160 |
+
except:
|
161 |
+
retry += 1
|
162 |
+
chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg))
|
163 |
+
retry_msg = f",正在重试 ({retry}/{MAX_RETRY}) ……" if MAX_RETRY > 0 else ""
|
164 |
+
yield chatbot, history, "请求超时"+retry_msg
|
165 |
+
if retry > MAX_RETRY: raise TimeoutError
|
166 |
|
167 |
token_counter = 0
|
168 |
partial_words = ""
|
|
|
203 |
chunk = get_full_error(chunk, stream_response)
|
204 |
error_msg = chunk.decode()
|
205 |
if "reduce the length" in error_msg:
|
206 |
+
chatbot[-1] = (history[-1], "[local] input is too long, reduce input or clear history.")
|
207 |
yield chatbot, history, "Json解析不合常规,很可能是文本过长" + error_msg
|
208 |
return
|
209 |
|