Spaces:
Building
Building
Upload 4 files
Browse files
app.py
CHANGED
@@ -168,7 +168,7 @@ def handle_api_error(error, attempt, stream=False):
|
|
168 |
})
|
169 |
|
170 |
if isinstance(error, InvalidArgument):
|
171 |
-
logger.error(f"{current_api_key[:11]} →
|
172 |
key_manager.blacklist_key(current_api_key)
|
173 |
switch_api_key()
|
174 |
return False, None
|
@@ -176,9 +176,9 @@ def handle_api_error(error, attempt, stream=False):
|
|
176 |
elif isinstance(error, (ResourceExhausted, Aborted, InternalServerError, ServiceUnavailable)):
|
177 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
178 |
if isinstance(error, ResourceExhausted):
|
179 |
-
logger.warning(f"{current_api_key[:11]} → 429
|
180 |
else:
|
181 |
-
logger.warning(f"{current_api_key[:11]} → 未知错误↙
|
182 |
time.sleep(delay)
|
183 |
if isinstance(error, (ResourceExhausted)):
|
184 |
key_manager.blacklist_key(current_api_key)
|
@@ -186,7 +186,7 @@ def handle_api_error(error, attempt, stream=False):
|
|
186 |
return False, None
|
187 |
|
188 |
elif isinstance(error, generation_types.StopCandidateException):
|
189 |
-
logger.warning(f"
|
190 |
switch_api_key()
|
191 |
return False, None
|
192 |
|
@@ -212,7 +212,7 @@ def chat_completions():
|
|
212 |
gemini_history, user_message, error_response = func.process_messages_for_gemini(messages)
|
213 |
|
214 |
if error_response:
|
215 |
-
logger.error(f"
|
216 |
return jsonify(error_response), 400
|
217 |
|
218 |
def do_request(current_api_key, attempt):
|
@@ -254,13 +254,12 @@ def chat_completions():
|
|
254 |
}
|
255 |
yield f"data: {json.dumps(data)}\n\n"
|
256 |
|
257 |
-
|
258 |
yield "data: [DONE]\n\n"
|
259 |
logger.info(f"200!")
|
260 |
|
261 |
except Exception as e:
|
262 |
switch_api_key()
|
263 |
-
logger.error(f"
|
264 |
error_data = {
|
265 |
'error': {
|
266 |
'message': '流式输出时截断,请关闭流式输出或修改你的输入',
|
@@ -276,7 +275,7 @@ def chat_completions():
|
|
276 |
|
277 |
while attempt < MAX_RETRIES and not success:
|
278 |
attempt += 1
|
279 |
-
logger.info(f"
|
280 |
success, response = do_request(current_api_key, attempt)
|
281 |
|
282 |
if not success:
|
@@ -295,10 +294,10 @@ def chat_completions():
|
|
295 |
try:
|
296 |
text_content = response.text
|
297 |
except (AttributeError, IndexError, TypeError) as e:
|
298 |
-
logger.error(f"
|
299 |
return jsonify({
|
300 |
'error': {
|
301 |
-
'message': '
|
302 |
'type': 'response_processing_error'
|
303 |
}
|
304 |
}), 500
|
@@ -344,8 +343,8 @@ if __name__ == '__main__':
|
|
344 |
scheduler.add_job(keep_alive, 'interval', hours=12)
|
345 |
scheduler.start()
|
346 |
|
347 |
-
logger.info(f"最大尝试次数/
|
348 |
-
logger.info(f"最大请求次数/
|
349 |
-
logger.info(f"请求限额窗口/
|
350 |
|
351 |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
168 |
})
|
169 |
|
170 |
if isinstance(error, InvalidArgument):
|
171 |
+
logger.error(f"{current_api_key[:11]} → 无效,可能已过期或被删除")
|
172 |
key_manager.blacklist_key(current_api_key)
|
173 |
switch_api_key()
|
174 |
return False, None
|
|
|
176 |
elif isinstance(error, (ResourceExhausted, Aborted, InternalServerError, ServiceUnavailable)):
|
177 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
178 |
if isinstance(error, ResourceExhausted):
|
179 |
+
logger.warning(f"{current_api_key[:11]} → 429 官方资源耗尽 → {delay} 秒后重试...")
|
180 |
else:
|
181 |
+
logger.warning(f"{current_api_key[:11]} → 未知错误↙ {delay} 秒后重试...\n{type(error).__name__}\n")
|
182 |
time.sleep(delay)
|
183 |
if isinstance(error, (ResourceExhausted)):
|
184 |
key_manager.blacklist_key(current_api_key)
|
|
|
186 |
return False, None
|
187 |
|
188 |
elif isinstance(error, generation_types.StopCandidateException):
|
189 |
+
logger.warning(f"AI输出内容被Gemini官方阻挡,代理没有得到有效回复")
|
190 |
switch_api_key()
|
191 |
return False, None
|
192 |
|
|
|
212 |
gemini_history, user_message, error_response = func.process_messages_for_gemini(messages)
|
213 |
|
214 |
if error_response:
|
215 |
+
logger.error(f"处理输入消息时出错↙\n {error_response}")
|
216 |
return jsonify(error_response), 400
|
217 |
|
218 |
def do_request(current_api_key, attempt):
|
|
|
254 |
}
|
255 |
yield f"data: {json.dumps(data)}\n\n"
|
256 |
|
|
|
257 |
yield "data: [DONE]\n\n"
|
258 |
logger.info(f"200!")
|
259 |
|
260 |
except Exception as e:
|
261 |
switch_api_key()
|
262 |
+
logger.error(f"流式输出时截断,请关闭流式输出或修改你的输入")
|
263 |
error_data = {
|
264 |
'error': {
|
265 |
'message': '流式输出时截断,请关闭流式输出或修改你的输入',
|
|
|
275 |
|
276 |
while attempt < MAX_RETRIES and not success:
|
277 |
attempt += 1
|
278 |
+
logger.info(f"第 {attempt}/{MAX_RETRIES} 次尝试 ...")
|
279 |
success, response = do_request(current_api_key, attempt)
|
280 |
|
281 |
if not success:
|
|
|
294 |
try:
|
295 |
text_content = response.text
|
296 |
except (AttributeError, IndexError, TypeError) as e:
|
297 |
+
logger.error(f"处理AI返回消息时出错↙\n{e}")
|
298 |
return jsonify({
|
299 |
'error': {
|
300 |
+
'message': '处理AI返回消息时出错',
|
301 |
'type': 'response_processing_error'
|
302 |
}
|
303 |
}), 500
|
|
|
343 |
scheduler.add_job(keep_alive, 'interval', hours=12)
|
344 |
scheduler.start()
|
345 |
|
346 |
+
logger.info(f"最大尝试次数/MaxRetries: {MAX_RETRIES}")
|
347 |
+
logger.info(f"最大请求次数/MaxRequests: {MAX_REQUESTS}")
|
348 |
+
logger.info(f"请求限额窗口/LimitWindow: {LIMIT_WINDOW} 秒")
|
349 |
|
350 |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|