109 lines
3.7 KiB
Python
109 lines
3.7 KiB
Python
import json
|
|
from fastapi import APIRouter, HTTPException, Header, Depends
|
|
from pydantic import BaseModel
|
|
from typing import Optional, List, Dict, Any
|
|
import time
|
|
from app.core.auth import decode_token
|
|
|
|
router = APIRouter()
|
|
|
|
def _get_user_id(authorization):
|
|
if not authorization or not authorization.startswith("Bearer "):
|
|
return "anonymous"
|
|
token = authorization.split(" ")[1]
|
|
payload = decode_token(token)
|
|
if not payload:
|
|
return "anonymous"
|
|
return payload.get("user_id", "anonymous")
|
|
|
|
# In-memory / per-user AI provider configurations storage dictionary
|
|
USER_AI_CONFIGS = {}
|
|
USER_PREFERENCES = {}
|
|
|
|
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]
|
|
|
|
class SavePreferencesRequest(BaseModel):
|
|
preferences: Dict[str, Any]
|
|
|
|
@router.get("/preferences")
|
|
async def get_user_preferences(authorization: Optional[str] = Header(None)):
|
|
uid = _get_user_id(authorization)
|
|
return {"success": True, "preferences": USER_PREFERENCES.get(uid, {})}
|
|
|
|
@router.post("/preferences")
|
|
async def save_user_preferences(req: SavePreferencesRequest, authorization: Optional[str] = Header(None)):
|
|
uid = _get_user_id(authorization)
|
|
USER_PREFERENCES[uid] = req.preferences
|
|
return {"success": True, "message": "Đã lưu cấu hình người dùng."}
|
|
|
|
@router.get("/config/ai")
|
|
async def get_user_ai_config(authorization: Optional[str] = Header(None)):
|
|
uid = _get_user_id(authorization)
|
|
if uid not in USER_AI_CONFIGS:
|
|
USER_AI_CONFIGS[uid] = [
|
|
{
|
|
"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[uid]
|
|
}
|
|
|
|
@router.post("/config/ai")
|
|
async def save_user_ai_config(req: SaveAIConfigRequest, authorization: Optional[str] = Header(None)):
|
|
uid = _get_user_id(authorization)
|
|
USER_AI_CONFIGS[uid] = [p.dict() for p in req.providers]
|
|
return {
|
|
"success": True,
|
|
"message": "Đã lưu cấu hình AI Providers thành công!"
|
|
}
|