Spaces:
Paused
Paused
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -320,11 +320,11 @@ def handle_api_error(error, attempt, current_api_key):
|
|
| 320 |
return 0, None
|
| 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
|
| 324 |
return 1, jsonify({'error': {'message': error_message, 'type': error_type}})
|
| 325 |
except ValueError:
|
| 326 |
-
logger.warning("400
|
| 327 |
-
return 1, jsonify({'error': {'message': '
|
| 328 |
|
| 329 |
elif status_code == 429:
|
| 330 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
|
@@ -479,7 +479,7 @@ def chat_completions():
|
|
| 479 |
parts = content['parts']
|
| 480 |
if is_thinking and not show_thoughts:
|
| 481 |
parts = [part for part in parts if not part.get('thought')]
|
| 482 |
-
if parts:
|
| 483 |
text = parts[0].get('text', '')
|
| 484 |
finish_reason = candidate.get('finishReason')
|
| 485 |
|
|
@@ -496,17 +496,47 @@ def chat_completions():
|
|
| 496 |
}
|
| 497 |
yield f"data: {json.dumps(data)}\n\n"
|
| 498 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
except json.JSONDecodeError:
|
| 500 |
-
logger.debug(f"
|
| 501 |
continue
|
| 502 |
|
| 503 |
except Exception as e:
|
| 504 |
-
logger.error(f"
|
| 505 |
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
| 506 |
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
|
|
|
| 510 |
except Exception as e:
|
| 511 |
logger.error(f"流式处理错误↙\n{e}")
|
| 512 |
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
|
|
|
| 320 |
return 0, None
|
| 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 1, jsonify({'error': {'message': error_message, 'type': error_type}})
|
| 325 |
except ValueError:
|
| 326 |
+
logger.warning("400 错误请求:响应不是有效的JSON格式")
|
| 327 |
+
return 1, jsonify({'error': {'message': '', 'type': 'invalid_request_error'}})
|
| 328 |
|
| 329 |
elif status_code == 429:
|
| 330 |
delay = min(RETRY_DELAY * (2 ** attempt), MAX_RETRY_DELAY)
|
|
|
|
| 479 |
parts = content['parts']
|
| 480 |
if is_thinking and not show_thoughts:
|
| 481 |
parts = [part for part in parts if not part.get('thought')]
|
| 482 |
+
if parts:
|
| 483 |
text = parts[0].get('text', '')
|
| 484 |
finish_reason = candidate.get('finishReason')
|
| 485 |
|
|
|
|
| 496 |
}
|
| 497 |
yield f"data: {json.dumps(data)}\n\n"
|
| 498 |
|
| 499 |
+
if candidate.get("finishReason") and candidate.get("finishReason") != "STOP":
|
| 500 |
+
error_message = {
|
| 501 |
+
"error": {
|
| 502 |
+
"code": "content_filter",
|
| 503 |
+
"message": "模型的响应因违反内容政策而被标记",
|
| 504 |
+
"status": candidate.get("finishReason"),
|
| 505 |
+
"details": []
|
| 506 |
+
}
|
| 507 |
+
}
|
| 508 |
+
yield f"data: {json.dumps(error_message)}\n\n"
|
| 509 |
+
break
|
| 510 |
+
|
| 511 |
+
if 'safetyRatings' in candidate:
|
| 512 |
+
for rating in candidate['safetyRatings']:
|
| 513 |
+
if rating['probability'] == 'HIGH':
|
| 514 |
+
error_message = {
|
| 515 |
+
"error": {
|
| 516 |
+
"code": "content_filter",
|
| 517 |
+
"message": f"模型的响应因高概率被标记为 {rating['category']}",
|
| 518 |
+
"status": "SAFETY_RATING_HIGH",
|
| 519 |
+
"details": [rating]
|
| 520 |
+
}
|
| 521 |
+
}
|
| 522 |
+
yield f"data: {json.dumps(error_message)}\n\n"
|
| 523 |
+
break
|
| 524 |
+
else:
|
| 525 |
+
continue
|
| 526 |
+
break
|
| 527 |
+
|
| 528 |
except json.JSONDecodeError:
|
| 529 |
+
logger.debug(f"JSON解析错误, 当前缓冲区内容: {buffer}")
|
| 530 |
continue
|
| 531 |
|
| 532 |
except Exception as e:
|
| 533 |
+
logger.error(f"流式处理期间发生错误: {e}, 原始数据行: {line}")
|
| 534 |
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
| 535 |
|
| 536 |
+
else:
|
| 537 |
+
yield f"data: {json.dumps({'choices': [{'delta': {}, 'finish_reason': 'stop', 'index': 0}]})}\n\n"
|
| 538 |
+
logger.info(f"流式结束 ←")
|
| 539 |
+
logger.info(f"200!")
|
| 540 |
except Exception as e:
|
| 541 |
logger.error(f"流式处理错误↙\n{e}")
|
| 542 |
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|