Spaces:
Build error
Build error
| from typing import Dict, Any, Optional, List | |
| from fastapi import APIRouter, HTTPException | |
| from pydantic import BaseModel | |
| import backend.models.gateway as gateway_module | |
| router = APIRouter(prefix="/v1/models", tags=["Model Gateway"]) | |
| class RegisterProviderRequest(BaseModel): | |
| provider_id: str | |
| provider_type: str | |
| config: Dict[str, Any] | |
| class GenerateRequest(BaseModel): | |
| model: str | |
| prompt: str | |
| providers: Optional[List[str]] = None | |
| kwargs: Optional[Dict[str, Any]] = None | |
| async def register_provider(req: RegisterProviderRequest): | |
| if gateway_module.model_gateway is None: | |
| raise HTTPException(503, "Model gateway not initialized") | |
| try: | |
| gateway_module.model_gateway.register_provider(req.provider_id, req.provider_type, req.config) | |
| return {"status": "registered", "provider_id": req.provider_id} | |
| except ValueError as e: | |
| raise HTTPException(400, str(e)) | |
| async def list_providers(): | |
| if gateway_module.model_gateway is None: | |
| raise HTTPException(503, "Model gateway not initialized") | |
| providers = gateway_module.model_gateway.list_providers() | |
| return {"providers": providers} | |
| async def generate(req: GenerateRequest): | |
| if gateway_module.model_gateway is None: | |
| raise HTTPException(503, "Model gateway not initialized") | |
| try: | |
| result = await gateway_module.model_gateway.generate( | |
| req.model, req.prompt, req.providers, **(req.kwargs or {}) | |
| ) | |
| return result | |
| except Exception as e: | |
| raise HTTPException(500, str(e)) | |
| async def get_call(call_id: str): | |
| if gateway_module.model_gateway is None: | |
| raise HTTPException(503, "Model gateway not initialized") | |
| call = await gateway_module.model_gateway.get_call(call_id) | |
| if not call: | |
| raise HTTPException(404, "Call not found") | |
| return call | |
| async def get_calls(limit: int = 20): | |
| if gateway_module.model_gateway is None: | |
| raise HTTPException(503, "Model gateway not initialized") | |
| calls = await gateway_module.model_gateway.get_calls(limit) | |
| return {"calls": calls} | |