from fastapi import APIRouter, HTTPException, Depends from pydantic import BaseModel from typing import Optional, List import time router = APIRouter() # In-memory / per-user AI provider configurations storage dictionary USER_AI_CONFIGS = {} class AIProviderSetting(BaseModel): id: str name: str provider_type: str # 'openai', 'openai_compatible', 'anthropic', 'gemini' api_base_url: Optional[str] = "https://api.openai.com/v1" api_key: Optional[str] = "" model_name: Optional[str] = "gpt-4o" temperature: float = 0.7 is_active: bool = True class SaveAIConfigRequest(BaseModel): providers: List[AIProviderSetting] @router.get("/config/ai") async def get_user_ai_config(): """Fetch user's AI provider configurations.""" if "default_user" not in USER_AI_CONFIGS: USER_AI_CONFIGS["default_user"] = [ { "id": "openai_default", "name": "OpenAI Official", "provider_type": "openai", "api_base_url": "https://api.openai.com/v1", "api_key": "", "model_name": "gpt-4o", "temperature": 0.7, "is_active": True }, { "id": "openai_compat_default", "name": "OpenAI Compatible (Ollama/LocalAI/DeepSeek)", "provider_type": "openai_compatible", "api_base_url": "http://localhost:11434/v1", "api_key": "ollama", "model_name": "deepseek-r1", "temperature": 0.7, "is_active": False }, { "id": "anthropic_default", "name": "Anthropic Claude", "provider_type": "anthropic", "api_base_url": "https://api.anthropic.com/v1", "api_key": "", "model_name": "claude-3-5-sonnet", "temperature": 0.7, "is_active": False }, { "id": "gemini_default", "name": "Google Gemini", "provider_type": "gemini", "api_base_url": "https://generativelanguage.googleapis.com", "api_key": "", "model_name": "gemini-1.5-pro", "temperature": 0.7, "is_active": False } ] return { "success": True, "providers": USER_AI_CONFIGS["default_user"] } @router.post("/config/ai") async def save_user_ai_config(req: SaveAIConfigRequest): """Save user's AI provider configurations.""" USER_AI_CONFIGS["default_user"] = [p.dict() for p in req.providers] return { "success": True, "message": "Đã lưu cấu hình AI Providers thành công!" }