MZhaovo commited on
Commit
86a1ff5
1 Parent(s): 5a7e86e

add error code Detection

Browse files
Files changed (1) hide show
  1. ChuanhuChatbot.py +37 -5
ChuanhuChatbot.py CHANGED
@@ -56,9 +56,21 @@ def predict(chatbot, input_sentence, system, context, myKey):
56
 
57
  try:
58
  message, message_with_stats = get_response(system, context, myKey)
59
- except:
60
  chatbot.append((input_sentence, "请求失败,请检查API-key是否正确。"))
61
  return chatbot, context
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  context.append({"role": "assistant", "content": message})
64
 
@@ -69,11 +81,25 @@ def predict(chatbot, input_sentence, system, context, myKey):
69
  def retry(chatbot, system, context, myKey):
70
  if len(context) == 0:
71
  return [], []
 
72
  try:
73
  message, message_with_stats = get_response(system, context[:-1], myKey)
74
- except:
75
  chatbot.append(("重试请求", "请求失败,请检查API-key是否正确。"))
76
  return chatbot, context
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  context[-1] = {"role": "assistant", "content": message}
78
 
79
  chatbot[-1] = (context[-2]["content"], message_with_stats)
@@ -130,10 +156,18 @@ def update_system(new_system_prompt):
130
 
131
  def set_apikey(new_api_key, myKey):
132
  old_api_key = myKey
 
133
  try:
134
  get_response(update_system(initial_prompt), [{"role": "user", "content": "test"}], new_api_key)
135
- except:
136
  return "无效的api-key", myKey
 
 
 
 
 
 
 
137
  encryption_str = "验证成功,api-key已做遮挡处理:" + new_api_key[:4] + "..." + new_api_key[-4:]
138
  return encryption_str, new_api_key
139
 
@@ -179,8 +213,6 @@ with gr.Blocks() as demo:
179
  delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
180
  reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context, myKey], [chatbot, context], show_progress=True)
181
  keyTxt.submit(set_apikey, [keyTxt, myKey], [keyTxt, myKey], show_progress=True)
182
- reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context, myKey], [chatbot, context], show_progress=True)
183
- keyTxt.submit(set_apikey, [keyTxt, myKey], [keyTxt, myKey], show_progress=True)
184
  uploadBtn.upload(load_chat_history, uploadBtn, [chatbot, systemPrompt, context, systemPromptDisplay], show_progress=True)
185
  saveBtn.click(save_chat_history, [saveFileName, systemPrompt, context], None, show_progress=True)
186
 
 
56
 
57
  try:
58
  message, message_with_stats = get_response(system, context, myKey)
59
+ except openai.error.AuthenticationError:
60
  chatbot.append((input_sentence, "请求失败,请检查API-key是否正确。"))
61
  return chatbot, context
62
+ except openai.error.Timeout:
63
+ chatbot.append((input_sentence, "请求超时,请检查网络连接。"))
64
+ return chatbot, context
65
+ except openai.error.APIConnectionError:
66
+ chatbot.append((input_sentence, "连接失败,请检查网络连接。"))
67
+ return chatbot, context
68
+ except openai.error.RateLimitError:
69
+ chatbot.append((input_sentence, "请求过于频繁,请5s后再试。"))
70
+ return chatbot, context
71
+ except:
72
+ chatbot.append((input_sentence, "发生了未知错误Orz"))
73
+ return chatbot, context
74
 
75
  context.append({"role": "assistant", "content": message})
76
 
 
81
  def retry(chatbot, system, context, myKey):
82
  if len(context) == 0:
83
  return [], []
84
+
85
  try:
86
  message, message_with_stats = get_response(system, context[:-1], myKey)
87
+ except openai.error.AuthenticationError:
88
  chatbot.append(("重试请求", "请求失败,请检查API-key是否正确。"))
89
  return chatbot, context
90
+ except openai.error.Timeout:
91
+ chatbot.append(("重试请求", "请求超时,请检查网络连接。"))
92
+ return chatbot, context
93
+ except openai.error.APIConnectionError:
94
+ chatbot.append(("重试请求", "连接失败,请检查网络连接。"))
95
+ return chatbot, context
96
+ except openai.error.RateLimitError:
97
+ chatbot.append(("重试请求", "请求过于频繁,请5s后再试。"))
98
+ return chatbot, context
99
+ except:
100
+ chatbot.append(("重试请求", "发生了未知错误Orz"))
101
+ return chatbot, context
102
+
103
  context[-1] = {"role": "assistant", "content": message}
104
 
105
  chatbot[-1] = (context[-2]["content"], message_with_stats)
 
156
 
157
  def set_apikey(new_api_key, myKey):
158
  old_api_key = myKey
159
+
160
  try:
161
  get_response(update_system(initial_prompt), [{"role": "user", "content": "test"}], new_api_key)
162
+ except openai.error.AuthenticationError:
163
  return "无效的api-key", myKey
164
+ except openai.error.Timeout:
165
+ return "请求超时,请检查网络设置", myKey
166
+ except openai.error.APIConnectionError:
167
+ return "网络错误", myKey
168
+ except:
169
+ return "发生了未知错误Orz", myKey
170
+
171
  encryption_str = "验证成功,api-key已做遮挡处理:" + new_api_key[:4] + "..." + new_api_key[-4:]
172
  return encryption_str, new_api_key
173
 
 
213
  delLastBtn.click(delete_last_conversation, [chatbot, context], [chatbot, context], show_progress=True)
214
  reduceTokenBtn.click(reduce_token, [chatbot, systemPrompt, context, myKey], [chatbot, context], show_progress=True)
215
  keyTxt.submit(set_apikey, [keyTxt, myKey], [keyTxt, myKey], show_progress=True)
 
 
216
  uploadBtn.upload(load_chat_history, uploadBtn, [chatbot, systemPrompt, context, systemPromptDisplay], show_progress=True)
217
  saveBtn.click(save_chat_history, [saveFileName, systemPrompt, context], None, show_progress=True)
218