|
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-" |
|
PAY_PREFIX = "[PAY]" |
|
|
|
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 original_model_id in sorted(list(all_model_ids)): |
|
current_display_prefix = "" |
|
if has_sa_creds and not has_express_key and EXPERIMENTAL_MARKER not in original_model_id: |
|
current_display_prefix = PAY_PREFIX |
|
|
|
base_display_id = f"{current_display_prefix}{original_model_id}" |
|
|
|
dynamic_models_data.append({ |
|
"id": base_display_id, "object": "model", "created": current_time, "owned_by": "google", |
|
"permission": [], "root": original_model_id, "parent": None |
|
}) |
|
|
|
|
|
if not original_model_id.startswith("gemini-2.0"): |
|
standard_suffixes = ["-search", "-encrypt", "-encrypt-full", "-auto"] |
|
for suffix in standard_suffixes: |
|
|
|
suffixed_model_part = f"{original_model_id}{suffix}" |
|
|
|
final_suffixed_display_id = f"{current_display_prefix}{suffixed_model_part}" |
|
|
|
|
|
if final_suffixed_display_id not in all_model_ids and not any(m['id'] == final_suffixed_display_id for m in dynamic_models_data): |
|
dynamic_models_data.append({ |
|
"id": final_suffixed_display_id, "object": "model", "created": current_time, "owned_by": "google", |
|
"permission": [], "root": original_model_id, "parent": None |
|
}) |
|
|
|
|
|
if original_model_id.startswith("gemini-2.5-flash"): |
|
special_flash_suffixes = ["-nothinking", "-max"] |
|
for special_suffix in special_flash_suffixes: |
|
suffixed_model_part = f"{original_model_id}{special_suffix}" |
|
final_special_suffixed_display_id = f"{current_display_prefix}{suffixed_model_part}" |
|
|
|
if final_special_suffixed_display_id not in all_model_ids and not any(m['id'] == final_special_suffixed_display_id for m in dynamic_models_data): |
|
dynamic_models_data.append({ |
|
"id": final_special_suffixed_display_id, "object": "model", "created": current_time, "owned_by": "google", |
|
"permission": [], "root": original_model_id, "parent": None |
|
}) |
|
|
|
|
|
|
|
if has_sa_creds: |
|
|
|
|
|
|
|
|
|
|
|
|
|
for base_model_id_for_openai in raw_vertex_models: |
|
display_model_id = "" |
|
if EXPERIMENTAL_MARKER in base_model_id_for_openai: |
|
display_model_id = f"{base_model_id_for_openai}{OPENAI_DIRECT_SUFFIX}" |
|
else: |
|
display_model_id = f"{PAY_PREFIX}{base_model_id_for_openai}{OPENAI_DIRECT_SUFFIX}" |
|
|
|
|
|
if display_model_id and not any(m['id'] == display_model_id for m in dynamic_models_data): |
|
dynamic_models_data.append({ |
|
"id": display_model_id, "object": "model", "created": current_time, "owned_by": "google", |
|
"permission": [], "root": base_model_id_for_openai, "parent": None |
|
}) |
|
|
|
|
|
|
|
|
|
return {"object": "list", "data": sorted(dynamic_models_data, key=lambda x: x['id'])} |