Chuan Hu commited on
Commit
d260cf4
·
unverified ·
2 Parent(s): 53f8133 1143012

Merge pull request #23 from MZhao-ouo/main

Browse files
Files changed (1) hide show
  1. ChuanhuChatbot.py +37 -5
ChuanhuChatbot.py CHANGED
@@ -57,9 +57,21 @@ def predict(chatbot, input_sentence, system, context, myKey):
57
 
58
  try:
59
  message, message_with_stats = get_response(system, context, myKey)
60
- except:
61
  chatbot.append((input_sentence, "请求失败,请检查API-key是否正确。"))
62
  return chatbot, context
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  context.append({"role": "assistant", "content": message})
65
 
@@ -70,11 +82,25 @@ def predict(chatbot, input_sentence, system, context, myKey):
70
  def retry(chatbot, system, context, myKey):
71
  if len(context) == 0:
72
  return [], []
 
73
  try:
74
  message, message_with_stats = get_response(system, context[:-1], myKey)
75
- except:
76
  chatbot.append(("重试请求", "请求失败,请检查API-key是否正确。"))
77
  return chatbot, context
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  context[-1] = {"role": "assistant", "content": message}
79
 
80
  chatbot[-1] = (context[-2]["content"], message_with_stats)
@@ -131,12 +157,18 @@ def update_system(new_system_prompt):
131
 
132
  def set_apikey(new_api_key, myKey):
133
  old_api_key = myKey
 
134
  try:
135
  get_response(update_system(initial_prompt), [{"role": "user", "content": "test"}], new_api_key)
136
- except:
137
- traceback.print_exc()
138
- print("API key校验失败,请检查API key是否正确,或者检查网络是否畅通。")
139
  return "无效的api-key", myKey
 
 
 
 
 
 
 
140
  encryption_str = "验证成功,api-key已做遮挡处理:" + new_api_key[:4] + "..." + new_api_key[-4:]
141
  return encryption_str, new_api_key
142
 
 
57
 
58
  try:
59
  message, message_with_stats = get_response(system, context, myKey)
60
+ except openai.error.AuthenticationError:
61
  chatbot.append((input_sentence, "请求失败,请检查API-key是否正确。"))
62
  return chatbot, context
63
+ except openai.error.Timeout:
64
+ chatbot.append((input_sentence, "请求超时,请检查网络连接。"))
65
+ return chatbot, context
66
+ except openai.error.APIConnectionError:
67
+ chatbot.append((input_sentence, "连接失败,请检查网络连接。"))
68
+ return chatbot, context
69
+ except openai.error.RateLimitError:
70
+ chatbot.append((input_sentence, "请求过于频繁,请5s后再试。"))
71
+ return chatbot, context
72
+ except:
73
+ chatbot.append((input_sentence, "发生了未知错误Orz"))
74
+ return chatbot, context
75
 
76
  context.append({"role": "assistant", "content": message})
77
 
 
82
  def retry(chatbot, system, context, myKey):
83
  if len(context) == 0:
84
  return [], []
85
+
86
  try:
87
  message, message_with_stats = get_response(system, context[:-1], myKey)
88
+ except openai.error.AuthenticationError:
89
  chatbot.append(("重试请求", "请求失败,请检查API-key是否正确。"))
90
  return chatbot, context
91
+ except openai.error.Timeout:
92
+ chatbot.append(("重试请求", "请求超时,请检查网络连接。"))
93
+ return chatbot, context
94
+ except openai.error.APIConnectionError:
95
+ chatbot.append(("重试请求", "连接失败,请检查网络连接。"))
96
+ return chatbot, context
97
+ except openai.error.RateLimitError:
98
+ chatbot.append(("重试请求", "请求过于频繁,请5s后再试。"))
99
+ return chatbot, context
100
+ except:
101
+ chatbot.append(("重试请求", "发生了未知错误Orz"))
102
+ return chatbot, context
103
+
104
  context[-1] = {"role": "assistant", "content": message}
105
 
106
  chatbot[-1] = (context[-2]["content"], message_with_stats)
 
157
 
158
  def set_apikey(new_api_key, myKey):
159
  old_api_key = myKey
160
+
161
  try:
162
  get_response(update_system(initial_prompt), [{"role": "user", "content": "test"}], new_api_key)
163
+ except openai.error.AuthenticationError:
 
 
164
  return "无效的api-key", myKey
165
+ except openai.error.Timeout:
166
+ return "请求超时,请检查网络设置", myKey
167
+ except openai.error.APIConnectionError:
168
+ return "网络错误", myKey
169
+ except:
170
+ return "发生了未知错误Orz", myKey
171
+
172
  encryption_str = "验证成功,api-key已做遮挡处理:" + new_api_key[:4] + "..." + new_api_key[-4:]
173
  return encryption_str, new_api_key
174