import logging from typing import Literal from fastapi import APIRouter, Depends, HTTPException, Request from pydantic import BaseModel, Field from app.gateway.catalog_cache import cached_catalog from app.gateway.deps import get_config from deerflow.config.app_config import AppConfig from deerflow.config.model_config import ModelConfig router = APIRouter(prefix="/api", tags=["models"]) logger = logging.getLogger(__name__) _PROVIDER_KEYWORDS: list[tuple[str, str]] = [ ("langchain_openai", "openai"), ("langchain_anthropic", "anthropic"), ("langchain_google", "google"), ("langchain_groq", "groq"), ("langchain_mistralai", "mistral"), ("langchain_cohere", "cohere"), ("ollama", "ollama"), ("vllm", "vllm"), ] _LOCAL_PROVIDERS = {"ollama", "vllm"} def _derive_provider(use: str) -> str | None: use_lower = use.lower() for keyword, provider in _PROVIDER_KEYWORDS: if keyword in use_lower: return provider return None def _build_model_response( model: ModelConfig, *, supports_reasoning_effort: bool = False, default_reasoning_effort: str = "xhigh", ) -> "ModelResponse": extra = model.model_extra or {} provider = str(extra["provider"]) if "provider" in extra else _derive_provider(model.use) is_local = bool(extra["is_local"]) if "is_local" in extra else (provider in _LOCAL_PROVIDERS) supports_tool_calling = bool(extra["supports_tool_calling"]) if "supports_tool_calling" in extra else True return ModelResponse( name=model.name, model=model.model, display_name=model.display_name, description=model.description, supports_thinking=model.supports_thinking, supports_reasoning_effort=bool(supports_reasoning_effort and model.supports_thinking), default_reasoning_effort=default_reasoning_effort if default_reasoning_effort in {"low", "medium", "xhigh"} else "xhigh", supports_vision=model.supports_vision, supports_tool_calling=supports_tool_calling, is_local=is_local, provider=provider, ) class ModelResponse(BaseModel): """Response model for model information.""" name: str = Field(..., description="Unique identifier for the model") model: str = Field(..., description="Actual provider model identifier") display_name: str | None = Field(None, description="Human-readable name") description: str | None = Field(None, description="Model description") supports_thinking: bool = Field(default=False, description="Whether model supports thinking mode") supports_reasoning_effort: bool = Field(default=False, description="Whether model supports reasoning effort") default_reasoning_effort: Literal["low", "medium", "xhigh"] = Field(default="xhigh", description="Globally configured default reasoning effort") supports_vision: bool = Field(default=False, description="Whether model supports image inputs") supports_tool_calling: bool = Field(default=True, description="Whether model supports tool/function calling") is_local: bool = Field(default=False, description="Whether this is a locally-hosted model") provider: str | None = Field(None, description="Model provider name (openai, anthropic, ollama, etc.)") class TokenUsageResponse(BaseModel): """Token usage display configuration.""" enabled: bool = Field(default=False, description="Whether token usage display is enabled") class ModelsListResponse(BaseModel): """Response model for listing all models.""" models: list[ModelResponse] token_usage: TokenUsageResponse async def _reasoning_capabilities(request: Request) -> dict[str, dict[str, object]]: store = getattr(request.app.state, "model_capability_store", None) if store is None: return {} async def load() -> dict[str, dict[str, object]]: rows = await store.list_capabilities() return {str(row.get("model_name") or ""): row for row in rows if row.get("model_name")} try: return await cached_catalog(request, "models", "reasoning-capabilities", load) except Exception: # Capability reads fail closed: the selector disappears, while ordinary # model use remains available. logger.warning("Failed to read model capabilities", exc_info=True) return {} @router.get( "/models", response_model=ModelsListResponse, summary="List All Models", description="Retrieve a list of all available AI models configured in the system.", ) async def list_models(request: Request, config: AppConfig = Depends(get_config)) -> ModelsListResponse: """List all available models from configuration. Returns model information suitable for frontend display, excluding sensitive fields like API keys and internal configuration. Returns: A list of all configured models with their metadata and token usage display settings. Example Response: ```json { "models": [ { "name": "gpt-4", "model": "gpt-4", "display_name": "GPT-4", "description": "OpenAI GPT-4 model", "supports_thinking": false, "supports_reasoning_effort": false }, { "name": "claude-3-opus", "model": "claude-3-opus", "display_name": "Claude 3 Opus", "description": "Anthropic Claude 3 Opus model", "supports_thinking": true, "supports_reasoning_effort": false } ], "token_usage": { "enabled": true } } ``` """ capabilities = await _reasoning_capabilities(request) models = [ _build_model_response( model, supports_reasoning_effort=bool(capabilities.get(model.name, {}).get("supports_reasoning_effort", False)), default_reasoning_effort=str(capabilities.get(model.name, {}).get("default_reasoning_effort") or "xhigh"), ) for model in config.models ] return ModelsListResponse( models=models, token_usage=TokenUsageResponse(enabled=config.token_usage.enabled), ) @router.get( "/models/{model_name}", response_model=ModelResponse, summary="Get Model Details", description="Retrieve detailed information about a specific AI model by its name.", ) async def get_model(model_name: str, request: Request, config: AppConfig = Depends(get_config)) -> ModelResponse: """Get a specific model by name. Args: model_name: The unique name of the model to retrieve. Returns: Model information if found. Raises: HTTPException: 404 if model not found. Example Response: ```json { "name": "gpt-4", "display_name": "GPT-4", "description": "OpenAI GPT-4 model", "supports_thinking": false } ``` """ model = config.get_model_config(model_name) if model is None: raise HTTPException(status_code=404, detail=f"Model '{model_name}' not found") capabilities = await _reasoning_capabilities(request) return _build_model_response( model, supports_reasoning_effort=bool(capabilities.get(model.name, {}).get("supports_reasoning_effort", False)), default_reasoning_effort=str(capabilities.get(model.name, {}).get("default_reasoning_effort") or "xhigh"), )