Spaces:
Sleeping
Sleeping
Upload main.py
Browse files
main.py
CHANGED
|
@@ -874,7 +874,10 @@ async def track_uptime_middleware(request: Request, call_next):
|
|
| 874 |
return response
|
| 875 |
|
| 876 |
except Exception as e:
|
| 877 |
-
# 请求失败
|
|
|
|
|
|
|
|
|
|
| 878 |
uptime_tracker.record_request("api_service", False)
|
| 879 |
if model and model in uptime_tracker.SUPPORTED_MODELS:
|
| 880 |
uptime_tracker.record_request(model, False)
|
|
@@ -1491,8 +1494,8 @@ async def chat(
|
|
| 1491 |
)
|
| 1492 |
is_new_conversation = True
|
| 1493 |
logger.info(f"[CHAT] [{account_manager.config.account_id}] [req_{request_id}] 新会话创建并绑定账户")
|
| 1494 |
-
# 记录
|
| 1495 |
-
uptime_tracker.record_request("
|
| 1496 |
break
|
| 1497 |
except Exception as e:
|
| 1498 |
last_error = e
|
|
@@ -1500,10 +1503,10 @@ async def chat(
|
|
| 1500 |
# 安全获取账户ID
|
| 1501 |
account_id = account_manager.config.account_id if 'account_manager' in locals() and account_manager else 'unknown'
|
| 1502 |
logger.error(f"[CHAT] [req_{request_id}] 账户 {account_id} 创建会话失败 (尝试 {attempt + 1}/{max_account_tries}) - {error_type}: {str(e)}")
|
|
|
|
|
|
|
| 1503 |
if attempt == max_account_tries - 1:
|
| 1504 |
logger.error(f"[CHAT] [req_{request_id}] 所有账户均不可用")
|
| 1505 |
-
# 记录服务状态(账户不可用)
|
| 1506 |
-
uptime_tracker.record_request("service_status", False)
|
| 1507 |
raise HTTPException(503, f"All accounts unavailable: {str(last_error)[:100]}")
|
| 1508 |
# 继续尝试下一个账户
|
| 1509 |
|
|
@@ -1611,6 +1614,9 @@ async def chat(
|
|
| 1611 |
account_manager.error_count = 0
|
| 1612 |
account_manager.conversation_count += 1 # 增加对话次数
|
| 1613 |
|
|
|
|
|
|
|
|
|
|
| 1614 |
# 保存对话次数到统计数据
|
| 1615 |
async with stats_lock:
|
| 1616 |
if "account_conversations" not in global_stats:
|
|
@@ -1624,6 +1630,9 @@ async def chat(
|
|
| 1624 |
# 记录当前失败的账户
|
| 1625 |
failed_accounts.add(account_manager.config.account_id)
|
| 1626 |
|
|
|
|
|
|
|
|
|
|
| 1627 |
# 检查是否为429错误(Rate Limit)
|
| 1628 |
is_rate_limit = isinstance(e, HTTPException) and e.status_code == 429
|
| 1629 |
|
|
@@ -1697,6 +1706,8 @@ async def chat(
|
|
| 1697 |
except Exception as create_err:
|
| 1698 |
error_type = type(create_err).__name__
|
| 1699 |
logger.error(f"[CHAT] [req_{request_id}] 账户切换失败 ({error_type}): {str(create_err)}")
|
|
|
|
|
|
|
| 1700 |
if req.stream: yield f"data: {json.dumps({'error': {'message': 'Account Failover Failed'}})}\n\n"
|
| 1701 |
return
|
| 1702 |
else:
|
|
|
|
| 874 |
return response
|
| 875 |
|
| 876 |
except Exception as e:
|
| 877 |
+
# 请求失败 - 尝试提取模型信息(可能在异常前已设置)
|
| 878 |
+
if hasattr(request.state, "model"):
|
| 879 |
+
model = request.state.model
|
| 880 |
+
|
| 881 |
uptime_tracker.record_request("api_service", False)
|
| 882 |
if model and model in uptime_tracker.SUPPORTED_MODELS:
|
| 883 |
uptime_tracker.record_request(model, False)
|
|
|
|
| 1494 |
)
|
| 1495 |
is_new_conversation = True
|
| 1496 |
logger.info(f"[CHAT] [{account_manager.config.account_id}] [req_{request_id}] 新会话创建并绑定账户")
|
| 1497 |
+
# 记录账号池状态(账户可用)
|
| 1498 |
+
uptime_tracker.record_request("account_pool", True)
|
| 1499 |
break
|
| 1500 |
except Exception as e:
|
| 1501 |
last_error = e
|
|
|
|
| 1503 |
# 安全获取账户ID
|
| 1504 |
account_id = account_manager.config.account_id if 'account_manager' in locals() and account_manager else 'unknown'
|
| 1505 |
logger.error(f"[CHAT] [req_{request_id}] 账户 {account_id} 创建会话失败 (尝试 {attempt + 1}/{max_account_tries}) - {error_type}: {str(e)}")
|
| 1506 |
+
# 记录账号池状态(单个账户失败)
|
| 1507 |
+
uptime_tracker.record_request("account_pool", False)
|
| 1508 |
if attempt == max_account_tries - 1:
|
| 1509 |
logger.error(f"[CHAT] [req_{request_id}] 所有账户均不可用")
|
|
|
|
|
|
|
| 1510 |
raise HTTPException(503, f"All accounts unavailable: {str(last_error)[:100]}")
|
| 1511 |
# 继续尝试下一个账户
|
| 1512 |
|
|
|
|
| 1614 |
account_manager.error_count = 0
|
| 1615 |
account_manager.conversation_count += 1 # 增加对话次数
|
| 1616 |
|
| 1617 |
+
# 记录账号池状态(请求成功)
|
| 1618 |
+
uptime_tracker.record_request("account_pool", True)
|
| 1619 |
+
|
| 1620 |
# 保存对话次数到统计数据
|
| 1621 |
async with stats_lock:
|
| 1622 |
if "account_conversations" not in global_stats:
|
|
|
|
| 1630 |
# 记录当前失败的账户
|
| 1631 |
failed_accounts.add(account_manager.config.account_id)
|
| 1632 |
|
| 1633 |
+
# 记录账号池状态(请求失败)
|
| 1634 |
+
uptime_tracker.record_request("account_pool", False)
|
| 1635 |
+
|
| 1636 |
# 检查是否为429错误(Rate Limit)
|
| 1637 |
is_rate_limit = isinstance(e, HTTPException) and e.status_code == 429
|
| 1638 |
|
|
|
|
| 1706 |
except Exception as create_err:
|
| 1707 |
error_type = type(create_err).__name__
|
| 1708 |
logger.error(f"[CHAT] [req_{request_id}] 账户切换失败 ({error_type}): {str(create_err)}")
|
| 1709 |
+
# 记录账号池状态(账户切换失败)
|
| 1710 |
+
uptime_tracker.record_request("account_pool", False)
|
| 1711 |
if req.stream: yield f"data: {json.dumps({'error': {'message': 'Account Failover Failed'}})}\n\n"
|
| 1712 |
return
|
| 1713 |
else:
|