add proxy debug funtion

This commit is contained in:
qingxu fu
2023-03-22 17:25:37 +08:00
parent f8a44a82a9
commit d00f6bb1a6
5 changed files with 84 additions and 52 deletions

View File

@@ -8,11 +8,11 @@ import os
if os.path.exists('config_private.py'):
# 放自己的秘密如API和代理网址
from config_private import proxies, API_URL, API_KEY
from config_private import proxies, API_URL, API_KEY, TIMEOUT_SECONDS
else:
from config import proxies, API_URL, API_KEY
from config import proxies, API_URL, API_KEY, TIMEOUT_SECONDS
timeout_bot_msg = 'Request timeout, network error. please check proxy settings in config.py.'
def compose_system(system_prompt):
return {"role": "system", "content": system_prompt}
@@ -35,7 +35,7 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
raw_input = inputs
logging.info(f'[raw_input] {raw_input}')
chatbot.append((inputs, ""))
yield chatbot, history, "Waiting"
yield chatbot, history, "等待响应"
headers = {
"Content-Type": "application/json",
@@ -49,26 +49,29 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
messages = [compose_system(system_prompt)]
if chat_counter:
for index in range(0, 2*chat_counter, 2):
d1 = {}
d1["role"] = "user"
d1["content"] = history[index]
d2 = {}
d2["role"] = "assistant"
d2["content"] = history[index+1]
if d1["content"] != "":
if d2["content"] != "" or retry:
messages.append(d1)
messages.append(d2)
what_i_have_asked = {}
what_i_have_asked["role"] = "user"
what_i_have_asked["content"] = history[index]
what_gpt_answer = {}
what_gpt_answer["role"] = "assistant"
what_gpt_answer["content"] = history[index+1]
if what_i_have_asked["content"] != "":
if not (what_gpt_answer["content"] != "" or retry): continue
if what_gpt_answer["content"] == timeout_bot_msg: continue
messages.append(what_i_have_asked)
messages.append(what_gpt_answer)
else:
messages[-1]['content'] = d2['content']
messages[-1]['content'] = what_gpt_answer['content']
if retry and chat_counter:
messages.pop()
else:
temp3 = {}
temp3["role"] = "user"
temp3["content"] = inputs
messages.append(temp3)
what_i_ask_now = {}
what_i_ask_now["role"] = "user"
what_i_ask_now["content"] = inputs
messages.append(what_i_ask_now)
chat_counter += 1
# messages
payload = {
"model": "gpt-3.5-turbo",
@@ -87,10 +90,10 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
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=15)
json=payload, stream=True, timeout=TIMEOUT_SECONDS)
except:
chatbot[-1] = ((chatbot[-1][0], 'Request timeout, network error. please check proxy settings in config.py.'))
yield chatbot, history, "Requests Timeout"
chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg))
yield chatbot, history, "请求超时"
raise TimeoutError
token_counter = 0
@@ -101,8 +104,6 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
stream_response = response.iter_lines()
while True:
chunk = next(stream_response)
# print(chunk)
if chunk == b'data: [DONE]':
break
@@ -119,16 +120,21 @@ def predict(inputs, top_p, temperature, chatbot=[], history=[], system_prompt=''
break
except Exception as e:
traceback.print_exc()
print(chunk.decode())
chunkjson = json.loads(chunk.decode()[6:])
status_text = f"id: {chunkjson['id']}, finish_reason: {chunkjson['choices'][0]['finish_reason']}"
partial_words = partial_words + \
json.loads(chunk.decode()[6:])[
'choices'][0]["delta"]["content"]
if token_counter == 0:
history.append(" " + partial_words)
else:
history[-1] = partial_words
chatbot[-1] = (history[-2], history[-1])
token_counter += 1
yield chatbot, history, status_text
try:
chunkjson = json.loads(chunk.decode()[6:])
status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"
partial_words = partial_words + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]
if token_counter == 0:
history.append(" " + partial_words)
else:
history[-1] = partial_words
chatbot[-1] = (history[-2], history[-1])
token_counter += 1
yield chatbot, history, status_text
except Exception as e:
traceback.print_exc()
print(chunk.decode())
yield chatbot, history, "Json解析不合常规"