|
import time |
|
from fastapi import APIRouter, Depends, Request |
|
from typing import List, Dict, Any |
|
from auth import get_api_key |
|
from model_loader import get_vertex_models, get_vertex_express_models, refresh_models_config_cache |
|
import config as app_config |
|
from credentials_manager import CredentialManager |
|
|
|
router = APIRouter() |
|
|
|
@router.get("/v1/models") |
|
async def list_models(fastapi_request: Request, api_key: str = Depends(get_api_key)): |
|
await refresh_models_config_cache() |
|
|
|
OPENAI_DIRECT_SUFFIX = "-openai" |
|
EXPERIMENTAL_MARKER = "-exp-" |
|
|
|
credential_manager_instance: CredentialManager = fastapi_request.app.state.credential_manager |
|
|
|
has_sa_creds = credential_manager_instance.get_total_credentials() > 0 |
|
has_express_key = bool(app_config.VERTEX_EXPRESS_API_KEY_VAL) |
|
|
|
raw_vertex_models = await get_vertex_models() |
|
raw_express_models = await get_vertex_express_models() |
|
|
|
candidate_model_ids = set() |
|
|
|
if has_express_key: |
|
candidate_model_ids.update(raw_express_models) |
|
|
|
|
|
|
|
if not has_sa_creds: |
|
|
|
|
|
all_model_ids = set(raw_express_models) |
|
else: |
|
|
|
all_model_ids = set(raw_vertex_models + raw_express_models) |
|
elif has_sa_creds: |
|
|
|
all_model_ids = set(raw_vertex_models) |
|
else: |
|
|
|
all_model_ids = set() |
|
|
|
|
|
|
|
|
|
|
|
|
|
dynamic_models_data: List[Dict[str, Any]] = [] |
|
current_time = int(time.time()) |
|
|
|
|
|
for model_id in sorted(list(all_model_ids)): |
|
dynamic_models_data.append({ |
|
"id": model_id, "object": "model", "created": current_time, "owned_by": "google", |
|
"permission": [], "root": model_id, "parent": None |
|
}) |
|
|
|
|
|
if not model_id.startswith("gemini-2.0"): |
|
standard_suffixes = ["-search", "-encrypt", "-encrypt-full", "-auto"] |
|
for suffix in standard_suffixes: |
|
suffixed_id = f"{model_id}{suffix}" |
|
|
|
if suffixed_id not in all_model_ids and not any(m['id'] == suffixed_id for m in dynamic_models_data): |
|
dynamic_models_data.append({ |
|
"id": suffixed_id, "object": "model", "created": current_time, "owned_by": "google", |
|
"permission": [], "root": model_id, "parent": None |
|
}) |
|
|
|
|
|
if model_id.startswith("gemini-2.5-flash"): |
|
special_flash_suffixes = ["-nothinking", "-max"] |
|
for special_suffix in special_flash_suffixes: |
|
suffixed_id = f"{model_id}{special_suffix}" |
|
if suffixed_id not in all_model_ids and not any(m['id'] == suffixed_id for m in dynamic_models_data): |
|
dynamic_models_data.append({ |
|
"id": suffixed_id, "object": "model", "created": current_time, "owned_by": "google", |
|
"permission": [], "root": model_id, "parent": None |
|
}) |
|
|
|
|
|
|
|
if has_sa_creds: |
|
|
|
|
|
for model_id in raw_vertex_models: |
|
if EXPERIMENTAL_MARKER in model_id: |
|
suffixed_id = f"{model_id}{OPENAI_DIRECT_SUFFIX}" |
|
|
|
if not any(m['id'] == suffixed_id for m in dynamic_models_data): |
|
dynamic_models_data.append({ |
|
"id": suffixed_id, "object": "model", "created": current_time, "owned_by": "google", |
|
"permission": [], "root": model_id, "parent": None |
|
}) |
|
|
|
|
|
|
|
|
|
return {"object": "list", "data": sorted(dynamic_models_data, key=lambda x: x['id'])} |