mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-04-25 11:18:22 +00:00
* feat: show token usage per assistant response * fix: align client models response with token usage * fix: address token usage review feedback * docs: clarify token usage config example --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
134 lines
4.1 KiB
Python
134 lines
4.1 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel, Field
|
|
|
|
from deerflow.config import get_app_config
|
|
|
|
router = APIRouter(prefix="/api", tags=["models"])
|
|
|
|
|
|
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")
|
|
|
|
|
|
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
|
|
|
|
|
|
@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() -> 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
|
|
}
|
|
}
|
|
```
|
|
"""
|
|
config = get_app_config()
|
|
models = [
|
|
ModelResponse(
|
|
name=model.name,
|
|
model=model.model,
|
|
display_name=model.display_name,
|
|
description=model.description,
|
|
supports_thinking=model.supports_thinking,
|
|
supports_reasoning_effort=model.supports_reasoning_effort,
|
|
)
|
|
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) -> 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
|
|
}
|
|
```
|
|
"""
|
|
config = get_app_config()
|
|
model = config.get_model_config(model_name)
|
|
if model is None:
|
|
raise HTTPException(status_code=404, detail=f"Model '{model_name}' not found")
|
|
|
|
return ModelResponse(
|
|
name=model.name,
|
|
model=model.model,
|
|
display_name=model.display_name,
|
|
description=model.description,
|
|
supports_thinking=model.supports_thinking,
|
|
supports_reasoning_effort=model.supports_reasoning_effort,
|
|
)
|