models.py (1338B)
1 """Model routes — proxy to Hermes API for model listing.""" 2 3 from __future__ import annotations 4 5 import logging 6 7 from fastapi import APIRouter, HTTPException 8 9 from app.config import get_config 10 from app.hermes_client import HermesClient, _normalize_model 11 12 logger = logging.getLogger(__name__) 13 router = APIRouter(prefix="/api/models", tags=["models"]) 14 15 16 def get_hermes_client() -> HermesClient: # pragma: no mutate: block 17 """Build a Hermes client from the current config.""" 18 cfg = get_config() 19 return HermesClient(base_url=cfg.hermes_base_url, api_key=cfg.hermes_api_key) 20 21 22 @router.get("") 23 async def list_models() -> dict: # pragma: no mutate: block 24 """List available models from Hermes.""" 25 client = get_hermes_client() 26 try: 27 data = await client.list_models() 28 except Exception as exc: 29 logger.exception("Hermes API error") 30 raise HTTPException(status_code=502, detail=f"Hermes: {exc}") from exc 31 if data is None: 32 return {"models": []} 33 # Hermes returns {"object": "list", "data": [...]} 34 raw_list = ( 35 data.get("data", data.get("models", [])) if isinstance(data, dict) 36 else data if isinstance(data, list) 37 else [] 38 ) 39 if isinstance(raw_list, list): 40 return {"models": [_normalize_model(m) for m in raw_list]} 41 return {"models": []}