Skip to content

Commit

Permalink
🐛 Bug: 1. Fix the bug of httpx timeout being too long after hot reloa…
Browse files Browse the repository at this point in the history
…ding the configuration file.

2. Fix the bug where v1/models cannot obtain the API key channel model.
  • Loading branch information
yym68686 committed Nov 26, 2024
1 parent c5104a1 commit 96efe10
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
7 changes: 4 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ async def ensure_config(request: Request, call_next):
}
# 发送GET请求获取模型列表
base_url = "http://127.0.0.1:8000/v1/models"
async with app.state.client_manager.get_client(100, base_url) as client:
async with app.state.client_manager.get_client(1, base_url) as client:
response = await client.get(
base_url,
headers=headers
Expand All @@ -786,7 +786,8 @@ async def ensure_config(request: Request, call_next):
for model in models_data.get("data", []):
models_list.append(model["id"])
except Exception as e:
logger.error(f"获取模型列表失败: {str(e)}")
if str(e):
logger.error(f"获取模型列表失败: {str(e)}")
app.state.models_list[provider_name] = models_list

return await call_next(request)
Expand Down Expand Up @@ -1280,7 +1281,7 @@ async def options_handler():

@app.get("/v1/models")
async def list_models(api_index: int = Depends(verify_api_key)):
models = post_all_models(api_index, app.state.config)
models = post_all_models(api_index, app.state.config, app.state.api_list, app.state.models_list)
return JSONResponse(content={
"object": "list",
"data": models
Expand Down
68 changes: 47 additions & 21 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ async def new_generator():
except StopAsyncIteration:
raise HTTPException(status_code=400, detail="data: {'error': 'No data returned'}")

def post_all_models(api_index, config):
def post_all_models(api_index, config, api_list, models_list):
all_models = []
unique_models = set()

Expand All @@ -493,36 +493,62 @@ def post_all_models(api_index, config):
provider = model.split("/")[0]
model = model.split("/")[1]
if model == "*":
for provider_item in config["providers"]:
if provider_item['provider'] != provider:
continue
model_dict = get_model_dict(provider_item)
for model_item in model_dict.keys():
if provider.startswith("sk-") and provider in api_list:
for model_item in models_list[provider]:
if model_item not in unique_models:
unique_models.add(model_item)
model_info = {
"id": model_item,
"object": "model",
"created": 1720524448858,
"owned_by": "uni-api"
# "owned_by": provider_item['provider']
}
all_models.append(model_info)
else:
for provider_item in config["providers"]:
if provider_item['provider'] != provider:
continue
model_dict = get_model_dict(provider_item)
for model_item in model_dict.keys():
if model_item not in unique_models:
unique_models.add(model_item)
model_info = {
"id": model_item,
"object": "model",
"created": 1720524448858,
"owned_by": "uni-api"
# "owned_by": provider_item['provider']
}
all_models.append(model_info)
else:
for provider_item in config["providers"]:
if provider_item['provider'] != provider:
continue
model_dict = get_model_dict(provider_item)
for model_item in model_dict.keys() :
if model_item not in unique_models and model_item == model:
unique_models.add(model_item)
model_info = {
"id": model_item,
"object": "model",
"created": 1720524448858,
"owned_by": "uni-api"
}
all_models.append(model_info)
if provider.startswith("sk-") and provider in api_list:
if model in models_list[provider] and model not in unique_models:
unique_models.add(model)
model_info = {
"id": model,
"object": "model",
"created": 1720524448858,
"owned_by": "uni-api"
}
all_models.append(model_info)
else:
for provider_item in config["providers"]:
if provider_item['provider'] != provider:
continue
model_dict = get_model_dict(provider_item)
for model_item in model_dict.keys():
if model_item not in unique_models and model_item == model:
unique_models.add(model_item)
model_info = {
"id": model_item,
"object": "model",
"created": 1720524448858,
"owned_by": "uni-api"
}
all_models.append(model_info)
continue

if model.startswith("sk-") and model in api_list:
continue

if model not in unique_models:
Expand Down

0 comments on commit 96efe10

Please sign in to comment.