55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
import logging
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from ...core.dependencies import get_current_user
|
|
from ...db.session import get_session
|
|
from ...schemas.llm_config import LLMConfigCreate, LLMConfigRead
|
|
from ...schemas.user import UserInDB
|
|
from ...services.llm_config_service import LLMConfigService
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter(prefix="/api/llm-config", tags=["LLM Configuration"])
|
|
|
|
|
|
def get_llm_config_service(session: AsyncSession = Depends(get_session)) -> LLMConfigService:
|
|
return LLMConfigService(session)
|
|
|
|
|
|
@router.get("", response_model=LLMConfigRead)
|
|
async def read_llm_config(
|
|
service: LLMConfigService = Depends(get_llm_config_service),
|
|
current_user: UserInDB = Depends(get_current_user),
|
|
) -> LLMConfigRead:
|
|
config = await service.get_config(current_user.id)
|
|
if not config:
|
|
logger.warning("用户 %s 尚未设置 LLM 配置", current_user.id)
|
|
raise HTTPException(status_code=404, detail="尚未设置自定义配置")
|
|
logger.info("用户 %s 获取 LLM 配置", current_user.id)
|
|
return config
|
|
|
|
|
|
@router.put("", response_model=LLMConfigRead)
|
|
async def upsert_llm_config(
|
|
payload: LLMConfigCreate,
|
|
service: LLMConfigService = Depends(get_llm_config_service),
|
|
current_user: UserInDB = Depends(get_current_user),
|
|
) -> LLMConfigRead:
|
|
logger.info("用户 %s 更新 LLM 配置", current_user.id)
|
|
return await service.upsert_config(current_user.id, payload)
|
|
|
|
|
|
@router.delete("", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_llm_config(
|
|
service: LLMConfigService = Depends(get_llm_config_service),
|
|
current_user: UserInDB = Depends(get_current_user),
|
|
) -> None:
|
|
deleted = await service.delete_config(current_user.id)
|
|
if not deleted:
|
|
logger.warning("用户 %s 删除 LLM 配置失败,未找到记录", current_user.id)
|
|
raise HTTPException(status_code=404, detail="未找到配置")
|
|
logger.info("用户 %s 删除 LLM 配置", current_user.id)
|