fix: 修复以第一个key获取模型列表,如果key失效将无法获取模型的问题

This commit is contained in:
snaily
2025-07-25 17:07:15 +08:00
parent ae775760dd
commit 6f714649a7
6 changed files with 25 additions and 5 deletions

View File

@@ -50,7 +50,7 @@ async def list_models(
logger.info("Handling Gemini models list request")
try:
api_key = await key_manager.get_first_valid_key()
api_key = await key_manager.get_random_valid_key()
if not api_key:
raise HTTPException(status_code=503, detail="No valid API keys available to fetch models.")
logger.info(f"Using API key: {redact_key_for_logging(api_key)}")

View File

@@ -46,7 +46,7 @@ async def list_models(
operation_name = "list_models"
async with handle_route_errors(logger, operation_name):
logger.info("Handling models list request")
api_key = await key_manager.get_first_valid_key()
api_key = await key_manager.get_random_valid_key()
logger.info(f"Using API key: {redact_key_for_logging(api_key)}")
return await openai_service.get_models(api_key)

View File

@@ -60,7 +60,7 @@ async def list_models(
operation_name = "list_models"
async with handle_route_errors(logger, operation_name):
logger.info("Handling models list request")
api_key = await key_manager.get_first_valid_key()
api_key = await key_manager.get_random_valid_key()
logger.info(f"Using API key: {redact_key_for_logging(api_key)}")
return await model_service.get_gemini_openai_models(api_key)

View File

@@ -46,7 +46,7 @@ async def list_models(
logger.info("Handling Gemini models list request")
try:
api_key = await key_manager.get_first_valid_key()
api_key = await key_manager.get_random_valid_key()
if not api_key:
raise HTTPException(status_code=503, detail="No valid API keys available to fetch models.")
logger.info(f"Using API key: {redact_key_for_logging(api_key)}")

View File

@@ -230,7 +230,7 @@ class ConfigService:
key_manager = await get_key_manager_instance()
model_service = ModelService()
api_key = await key_manager.get_first_valid_key()
api_key = await key_manager.get_random_valid_key()
if not api_key:
logger.error("No valid API keys available to fetch model list for UI.")
raise HTTPException(

View File

@@ -1,4 +1,5 @@
import asyncio
import random
from itertools import cycle
from typing import Dict, Union
@@ -195,6 +196,25 @@ class KeyManager:
return ""
return self.api_keys[0]
async def get_random_valid_key(self) -> str:
"""获取随机的有效API key"""
valid_keys = []
async with self.failure_count_lock:
for key in self.key_failure_counts:
if self.key_failure_counts[key] < self.MAX_FAILURES:
valid_keys.append(key)
if valid_keys:
return random.choice(valid_keys)
# 如果没有有效的key返回第一个key作为fallback
if self.api_keys:
logger.warning("No valid keys available, returning first key as fallback.")
return self.api_keys[0]
logger.warning("API key list is empty, cannot get random valid key.")
return ""
_singleton_instance = None
_singleton_lock = asyncio.Lock()