Spaces:
Building
Building
Upload 2 files
Browse files
app.py
CHANGED
@@ -321,19 +321,17 @@ def handle_api_error(error, attempt, current_api_key):
|
|
321 |
error_message = error_data['error'].get('message', 'Bad Request')
|
322 |
error_type = error_data['error'].get('type', 'invalid_request_error')
|
323 |
logger.warning(f"400 错误请求: {error_message}")
|
324 |
-
return
|
325 |
except ValueError:
|
326 |
logger.warning("400 错误请求:响应不是有效的JSON格式")
|
327 |
-
return
|
328 |
|
329 |
elif status_code == 429:
|
330 |
-
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
331 |
logger.warning(
|
332 |
-
f"{current_api_key[:8]} ... {current_api_key[-3:]} → 429 官方资源耗尽 →
|
333 |
)
|
334 |
key_manager.blacklist_key(current_api_key)
|
335 |
switch_api_key()
|
336 |
-
time.sleep(delay)
|
337 |
return 0, None
|
338 |
|
339 |
elif status_code == 403:
|
@@ -345,41 +343,35 @@ def handle_api_error(error, attempt, current_api_key):
|
|
345 |
return 0, None
|
346 |
|
347 |
elif status_code == 500:
|
348 |
-
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
349 |
logger.warning(
|
350 |
-
f"{current_api_key[:8]} ... {current_api_key[-3:]} → 500 服务器内部错误 →
|
351 |
)
|
352 |
switch_api_key()
|
353 |
-
time.sleep(delay)
|
354 |
return 0, None
|
355 |
|
356 |
elif status_code == 503:
|
357 |
-
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
358 |
logger.warning(
|
359 |
-
f"{current_api_key[:8]} ... {current_api_key[-3:]} → 503 服务不可用 →
|
360 |
)
|
361 |
switch_api_key()
|
362 |
-
time.sleep(delay)
|
363 |
return 0, None
|
364 |
|
365 |
else:
|
366 |
-
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
367 |
logger.warning(
|
368 |
-
f"{current_api_key[:8]} ... {current_api_key[-3:]} → {status_code} 未知错误/模型不可用 →
|
369 |
)
|
370 |
switch_api_key()
|
371 |
-
time.sleep(delay)
|
372 |
return 2, None
|
373 |
|
374 |
elif isinstance(error, requests.exceptions.ConnectionError):
|
375 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
376 |
-
logger.warning(f"连接错误 →
|
377 |
time.sleep(delay)
|
378 |
return 0, None
|
379 |
|
380 |
elif isinstance(error, requests.exceptions.Timeout):
|
381 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
382 |
-
logger.warning(f"请求超时 →
|
383 |
time.sleep(delay)
|
384 |
return 0, None
|
385 |
|
|
|
321 |
error_message = error_data['error'].get('message', 'Bad Request')
|
322 |
error_type = error_data['error'].get('type', 'invalid_request_error')
|
323 |
logger.warning(f"400 错误请求: {error_message}")
|
324 |
+
return 2, jsonify({'error': {'message': error_message, 'type': error_type}})
|
325 |
except ValueError:
|
326 |
logger.warning("400 错误请求:响应不是有效的JSON格式")
|
327 |
+
return 2, jsonify({'error': {'message': '', 'type': 'invalid_request_error'}})
|
328 |
|
329 |
elif status_code == 429:
|
|
|
330 |
logger.warning(
|
331 |
+
f"{current_api_key[:8]} ... {current_api_key[-3:]} → 429 官方资源耗尽 → 立即重试..."
|
332 |
)
|
333 |
key_manager.blacklist_key(current_api_key)
|
334 |
switch_api_key()
|
|
|
335 |
return 0, None
|
336 |
|
337 |
elif status_code == 403:
|
|
|
343 |
return 0, None
|
344 |
|
345 |
elif status_code == 500:
|
|
|
346 |
logger.warning(
|
347 |
+
f"{current_api_key[:8]} ... {current_api_key[-3:]} → 500 服务器内部错误 → 立即重试..."
|
348 |
)
|
349 |
switch_api_key()
|
|
|
350 |
return 0, None
|
351 |
|
352 |
elif status_code == 503:
|
|
|
353 |
logger.warning(
|
354 |
+
f"{current_api_key[:8]} ... {current_api_key[-3:]} → 503 服务不可用 → 立即重试..."
|
355 |
)
|
356 |
switch_api_key()
|
|
|
357 |
return 0, None
|
358 |
|
359 |
else:
|
|
|
360 |
logger.warning(
|
361 |
+
f"{current_api_key[:8]} ... {current_api_key[-3:]} → {status_code} 未知错误/模型不可用 → 不重试..."
|
362 |
)
|
363 |
switch_api_key()
|
|
|
364 |
return 2, None
|
365 |
|
366 |
elif isinstance(error, requests.exceptions.ConnectionError):
|
367 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
368 |
+
logger.warning(f"连接错误 → 立即重试...")
|
369 |
time.sleep(delay)
|
370 |
return 0, None
|
371 |
|
372 |
elif isinstance(error, requests.exceptions.Timeout):
|
373 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
374 |
+
logger.warning(f"请求超时 → 立即重试...")
|
375 |
time.sleep(delay)
|
376 |
return 0, None
|
377 |
|