Spaces:
Paused
Paused
File size: 22,165 Bytes
f3bcb28 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 | """
Models Management API.
CRUD operations for LLM models registry.
Models are stored in the database and can be configured via the admin UI.
Default models are loaded from backend/config/default_models.json (no hardcoded values).
"""
from typing import List, Optional
import json
import logging
from pathlib import Path
from fastapi import APIRouter, Depends, HTTPException, status, Query
from pydantic import BaseModel
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from backend.db.session import get_db
from backend.db.models import LLMModel, ProviderType
from backend.core.jwt import get_current_user_id
from backend.application.providers.ollama import ollama_provider
from backend.application.providers.lmstudio import lmstudio_provider
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/models", tags=["Models"])
# === Load default models from external config ===
def _load_default_models() -> list[dict]:
"""Load default models from backend/config/default_models.json."""
config_paths = [
Path(__file__).parent.parent.parent
/ "config"
/ "default_models.json", # backend/config/
Path(
"/app/backend/config/default_models.json"
), # Docker path (backend/config/)
Path.cwd() / "backend" / "config" / "default_models.json",
]
for config_path in config_paths:
if config_path.exists():
try:
with open(config_path, "r") as f:
data = json.load(f)
return data.get("models", [])
except Exception as e:
logger.warning(f"Failed to load default models from {config_path}: {e}")
logger.warning("No default_models.json found, seeding will create empty database")
return []
# === Pydantic Schemas ===
class ModelCreate(BaseModel):
"""Request to create a model."""
model_id: str
name: str
description: Optional[str] = None
provider: (
str # openai, anthropic, ollama, lmstudio, azure_openai, groq, together, custom
)
provider_model_id: str
base_url: Optional[str] = None
api_key_env_var: Optional[str] = None
api_key: Optional[str] = None # Direct API key for cloud providers (OSS)
context_length: int = 4096
supports_vision: bool = False
supports_functions: bool = False
supports_streaming: bool = True
input_cost_per_million: float = 0.0
output_cost_per_million: float = 0.0
is_enabled: bool = True
is_default: bool = False
display_order: int = 100
class ModelUpdate(BaseModel):
"""Request to update a model."""
name: Optional[str] = None
description: Optional[str] = None
base_url: Optional[str] = None
api_key_env_var: Optional[str] = None
api_key: Optional[str] = None # Direct API key for cloud providers (OSS)
context_length: Optional[int] = None
supports_vision: Optional[bool] = None
supports_functions: Optional[bool] = None
supports_streaming: Optional[bool] = None
input_cost_per_million: Optional[float] = None
output_cost_per_million: Optional[float] = None
is_enabled: Optional[bool] = None
is_default: Optional[bool] = None
display_order: Optional[int] = None
class ModelResponse(BaseModel):
"""Model response."""
id: int
model_id: str
name: str
description: Optional[str]
provider: str
provider_model_id: str
base_url: Optional[str]
has_api_key: bool = False # True if api_key is set (never expose the actual key)
context_length: int
supports_vision: bool
supports_functions: bool
supports_streaming: bool
input_cost_per_million: float
output_cost_per_million: float
is_enabled: bool
is_default: bool
display_order: int
class Config:
from_attributes = True
class ModelInfo(BaseModel):
"""Simplified model info for playground."""
id: str
name: str
provider: str
description: Optional[str] = None
available: bool = True
context_length: Optional[int] = None
supports_vision: bool = False
supports_functions: bool = False
class ProviderStatus(BaseModel):
"""Status of a provider."""
name: str
available: bool
base_url: Optional[str] = None
model_count: int = 0
class ModelsListResponse(BaseModel):
"""Response with all available models."""
models: List[ModelInfo]
providers: List[ProviderStatus]
# === Endpoints ===
@router.get("", response_model=ModelsListResponse)
async def list_models(
db: AsyncSession = Depends(get_db),
include_disabled: bool = Query(False, description="Include disabled models"),
):
"""
List all available models.
Returns models from the database, grouped by provider.
Also checks local providers (Ollama, LM Studio) availability.
"""
# Get models from database
query = select(LLMModel).order_by(LLMModel.display_order, LLMModel.name)
if not include_disabled:
query = query.where(LLMModel.is_enabled.is_(True))
result = await db.execute(query)
db_models = result.scalars().all()
# Convert to response format
models: List[ModelInfo] = []
provider_counts: dict = {}
for m in db_models:
provider_name = (
m.provider.value if hasattr(m.provider, "value") else str(m.provider)
)
models.append(
ModelInfo(
id=m.model_id,
name=m.name,
provider=provider_name,
description=m.description,
available=m.is_enabled,
context_length=m.context_length,
supports_vision=m.supports_vision,
supports_functions=m.supports_functions,
)
)
provider_counts[provider_name] = provider_counts.get(provider_name, 0) + 1
# Check local providers
ollama_available = await ollama_provider.is_available()
lmstudio_available = await lmstudio_provider.is_available()
# Get provider base URLs from database
from backend.core.config import settings
provider_urls_result = await db.execute(
select(LLMModel.provider, LLMModel.base_url)
.distinct()
.where(LLMModel.is_enabled.is_(True))
)
provider_urls: dict[str, str] = {}
for provider, base_url in provider_urls_result.all():
provider_name = provider.value if hasattr(provider, "value") else str(provider)
if base_url and provider_name not in provider_urls:
provider_urls[provider_name] = base_url
# Build provider status list (URLs from DB or settings)
providers = [
ProviderStatus(
name="openai",
available=True,
base_url=provider_urls.get("openai", settings.openai_api_url),
model_count=provider_counts.get("openai", 0),
),
ProviderStatus(
name="anthropic",
available=True,
base_url=provider_urls.get("anthropic", settings.anthropic_api_url),
model_count=provider_counts.get("anthropic", 0),
),
ProviderStatus(
name="ollama",
available=ollama_available,
base_url=ollama_provider.base_url,
model_count=provider_counts.get("ollama", 0),
),
ProviderStatus(
name="lmstudio",
available=lmstudio_available,
base_url=lmstudio_provider.base_url,
model_count=provider_counts.get("lmstudio", 0),
),
]
return ModelsListResponse(models=models, providers=providers)
def _model_to_response(model: LLMModel) -> ModelResponse:
"""Convert DB model to response, adding has_api_key flag."""
provider_name = (
model.provider.value
if hasattr(model.provider, "value")
else str(model.provider)
)
return ModelResponse(
id=model.id,
model_id=model.model_id,
name=model.name,
description=model.description,
provider=provider_name,
provider_model_id=model.provider_model_id,
base_url=model.base_url,
has_api_key=bool(model.api_key), # True if api_key is set
context_length=model.context_length,
supports_vision=model.supports_vision,
supports_functions=model.supports_functions,
supports_streaming=model.supports_streaming,
input_cost_per_million=model.input_cost_per_million,
output_cost_per_million=model.output_cost_per_million,
is_enabled=model.is_enabled,
is_default=model.is_default,
display_order=model.display_order,
)
@router.get("/all", response_model=List[ModelResponse])
async def list_all_models(
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""
List all models (admin view with full details).
"""
result = await db.execute(
select(LLMModel).order_by(LLMModel.display_order, LLMModel.name)
)
models = result.scalars().all()
return [_model_to_response(m) for m in models]
@router.post("", response_model=ModelResponse, status_code=status.HTTP_201_CREATED)
async def create_model(
request: ModelCreate,
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""
Create a new model configuration.
"""
# Check if model_id already exists
existing = await db.execute(
select(LLMModel).where(LLMModel.model_id == request.model_id)
)
if existing.scalar_one_or_none():
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"Model with ID '{request.model_id}' already exists",
)
# Validate provider
try:
provider = ProviderType(request.provider)
except ValueError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid provider: {request.provider}. Valid providers: {[p.value for p in ProviderType]}",
)
model = LLMModel(
model_id=request.model_id,
name=request.name,
description=request.description,
provider=provider,
provider_model_id=request.provider_model_id,
base_url=request.base_url,
api_key_env_var=request.api_key_env_var,
api_key=request.api_key, # Direct API key for cloud providers (OSS)
context_length=request.context_length,
supports_vision=request.supports_vision,
supports_functions=request.supports_functions,
supports_streaming=request.supports_streaming,
input_cost_per_million=request.input_cost_per_million,
output_cost_per_million=request.output_cost_per_million,
is_enabled=request.is_enabled,
is_default=request.is_default,
display_order=request.display_order,
)
db.add(model)
await db.commit()
await db.refresh(model)
return _model_to_response(model)
@router.get("/by-id/{model_id:path}", response_model=ModelResponse)
async def get_model(
model_id: str,
db: AsyncSession = Depends(get_db),
):
"""Get a specific model configuration."""
result = await db.execute(select(LLMModel).where(LLMModel.model_id == model_id))
model = result.scalar_one_or_none()
if not model:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Model '{model_id}' not found",
)
return _model_to_response(model)
@router.patch("/by-id/{model_id:path}", response_model=ModelResponse)
async def update_model(
model_id: str,
request: ModelUpdate,
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""Update a model configuration."""
result = await db.execute(select(LLMModel).where(LLMModel.model_id == model_id))
model = result.scalar_one_or_none()
if not model:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Model '{model_id}' not found",
)
# Update fields
update_data = request.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(model, field, value)
await db.commit()
await db.refresh(model)
return _model_to_response(model)
@router.delete("/by-id/{model_id:path}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_model(
model_id: str,
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""Delete a model configuration."""
result = await db.execute(select(LLMModel).where(LLMModel.model_id == model_id))
model = result.scalar_one_or_none()
if not model:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Model '{model_id}' not found",
)
await db.delete(model)
await db.commit()
@router.post("/by-id/{model_id:path}/enable", response_model=ModelResponse)
async def enable_model(
model_id: str,
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""Enable a model."""
result = await db.execute(select(LLMModel).where(LLMModel.model_id == model_id))
model = result.scalar_one_or_none()
if not model:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Model '{model_id}' not found",
)
model.is_enabled = True
await db.commit()
await db.refresh(model)
return _model_to_response(model)
@router.post("/by-id/{model_id:path}/disable", response_model=ModelResponse)
async def disable_model(
model_id: str,
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""Disable a model."""
result = await db.execute(select(LLMModel).where(LLMModel.model_id == model_id))
model = result.scalar_one_or_none()
if not model:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Model '{model_id}' not found",
)
model.is_enabled = False
await db.commit()
await db.refresh(model)
return _model_to_response(model)
@router.post("/seed", response_model=dict)
async def seed_default_models(
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""
Seed the database with default models from config/default_models.json.
This is useful for initial setup or after clearing the models table.
Models are loaded from external config file (no hardcoded values).
"""
# Load models from external config
default_models = _load_default_models()
if not default_models:
return {
"message": "No default models found in backend/config/default_models.json",
"created": 0,
"skipped": 0,
}
created = 0
skipped = 0
for model_data in default_models:
# Check if already exists
existing = await db.execute(
select(LLMModel).where(LLMModel.model_id == model_data["model_id"])
)
if existing.scalar_one_or_none():
skipped += 1
continue
# Create model
try:
provider = ProviderType(model_data["provider"])
except ValueError:
logger.warning(
f"Unknown provider {model_data['provider']} for model {model_data['model_id']}, skipping"
)
skipped += 1
continue
model = LLMModel(
model_id=model_data["model_id"],
name=model_data["name"],
description=model_data.get("description"),
provider=provider,
provider_model_id=model_data["provider_model_id"],
base_url=model_data.get("base_url"),
context_length=model_data.get("context_length", 4096),
supports_vision=model_data.get("supports_vision", False),
supports_functions=model_data.get("supports_functions", False),
input_cost_per_million=model_data.get("input_cost_per_million", 0.0),
output_cost_per_million=model_data.get("output_cost_per_million", 0.0),
display_order=model_data.get("display_order", 100),
is_enabled=model_data.get("is_enabled", True),
)
db.add(model)
created += 1
await db.commit()
return {
"message": f"Seeded {created} models from backend/config/default_models.json, skipped {skipped} existing",
"created": created,
"skipped": skipped,
}
@router.get("/providers/status", response_model=List[ProviderStatus])
async def list_providers(db: AsyncSession = Depends(get_db)):
"""
List all providers and their availability status.
Provider URLs are loaded from database models (no hardcoded URLs).
"""
from backend.core.config import settings
ollama_available = await ollama_provider.is_available()
lmstudio_available = await lmstudio_provider.is_available()
# Get unique providers and their base_urls from database
result = await db.execute(
select(LLMModel.provider, LLMModel.base_url)
.distinct()
.where(LLMModel.is_enabled.is_(True))
)
db_providers = result.all()
# Build provider URLs from DB
provider_urls: dict[str, str] = {}
for provider, base_url in db_providers:
provider_name = provider.value if hasattr(provider, "value") else str(provider)
if base_url and provider_name not in provider_urls:
provider_urls[provider_name] = base_url
# Use settings for OpenAI/Anthropic if not in DB
if "openai" not in provider_urls:
provider_urls["openai"] = settings.openai_api_url
if "anthropic" not in provider_urls:
provider_urls["anthropic"] = settings.anthropic_api_url
return [
ProviderStatus(
name="openai", available=True, base_url=provider_urls.get("openai")
),
ProviderStatus(
name="anthropic", available=True, base_url=provider_urls.get("anthropic")
),
ProviderStatus(
name="ollama", available=ollama_available, base_url=ollama_provider.base_url
),
ProviderStatus(
name="lmstudio",
available=lmstudio_available,
base_url=lmstudio_provider.base_url,
),
ProviderStatus(name="aws_bedrock", available=True),
ProviderStatus(name="groq", available=True, base_url=provider_urls.get("groq")),
ProviderStatus(
name="mistral", available=True, base_url=provider_urls.get("mistral")
),
ProviderStatus(
name="google", available=True, base_url=provider_urls.get("google")
),
ProviderStatus(
name="deepseek", available=True, base_url=provider_urls.get("deepseek")
),
]
@router.post("/discover/ollama", response_model=dict)
async def discover_ollama_models(
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""
Discover and add models from Ollama.
Connects to the local Ollama instance and imports available models.
"""
if not await ollama_provider.is_available():
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Ollama is not running. Start it with 'ollama serve'",
)
ollama_models = await ollama_provider.list_models()
created = 0
skipped = 0
for m in ollama_models:
model_id = f"ollama/{m.name}"
# Check if exists
existing = await db.execute(
select(LLMModel).where(LLMModel.model_id == model_id)
)
if existing.scalar_one_or_none():
skipped += 1
continue
# Create model
model = LLMModel(
model_id=model_id,
name=m.name.replace(":latest", "").replace("-", " ").title(),
description="Local model via Ollama",
provider=ProviderType.OLLAMA,
provider_model_id=m.name,
base_url=ollama_provider.base_url,
context_length=4096, # Default, can be updated
is_enabled=True,
display_order=50,
)
db.add(model)
created += 1
await db.commit()
return {
"message": f"Discovered {created} models from Ollama, skipped {skipped} existing",
"created": created,
"skipped": skipped,
}
@router.post("/discover/lmstudio", response_model=dict)
async def discover_lmstudio_models(
db: AsyncSession = Depends(get_db),
user_id: int = Depends(get_current_user_id),
):
"""
Discover and add models from LM Studio.
Connects to the local LM Studio instance and imports loaded models.
"""
if not await lmstudio_provider.is_available():
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="LM Studio is not running. Start it and enable the local server.",
)
lmstudio_models = await lmstudio_provider.list_models()
created = 0
skipped = 0
for m in lmstudio_models:
model_id = f"lmstudio/{m.id}"
# Check if exists
existing = await db.execute(
select(LLMModel).where(LLMModel.model_id == model_id)
)
if existing.scalar_one_or_none():
skipped += 1
continue
# Create model
display_name = m.id.split("/")[-1] if "/" in m.id else m.id
model = LLMModel(
model_id=model_id,
name=display_name,
description="Local model via LM Studio",
provider=ProviderType.LMSTUDIO,
provider_model_id=m.id,
base_url=lmstudio_provider.base_url,
context_length=4096, # Default, can be updated
is_enabled=True,
display_order=60,
)
db.add(model)
created += 1
await db.commit()
return {
"message": f"Discovered {created} models from LM Studio, skipped {skipped} existing",
"created": created,
"skipped": skipped,
}
|