diff --git a/apps/models/llm.py b/apps/models/llm.py index 4396347a2c31126df93a3093bb38a5ffa776ece2..058c3e4b74cf02ad5ca39c0ded55f427c5a70b51 100644 --- a/apps/models/llm.py +++ b/apps/models/llm.py @@ -65,9 +65,9 @@ class LLMData(Base): """大模型API类型""" llmDescription: Mapped[str] = mapped_column(String(2000), default="", nullable=False) # noqa: N815 """大模型描述""" - createdAt: Mapped[DateTime] = mapped_column( # noqa: N815 - DateTime, - default_factory=lambda: datetime.now(tz=UTC), + createdAt: Mapped[datetime] = mapped_column( # noqa: N815 + DateTime(timezone=True), + default_factory=lambda: datetime.now(UTC), init=False, nullable=False, ) diff --git a/apps/routers/llm.py b/apps/routers/llm.py index 7990f0aca00e05d0c6de1cd30b629685a10e5cd9..2e36347316b6803a0d630cf09254896b0069f521 100644 --- a/apps/routers/llm.py +++ b/apps/routers/llm.py @@ -1,6 +1,7 @@ # Copyright (c) Huawei Technologies Co., Ltd. 2023-2025. All rights reserved. """FastAPI 大模型相关接口""" +import logging from typing import cast from fastapi import APIRouter, Depends, Request, status @@ -37,6 +38,7 @@ admin_router = APIRouter( Depends(verify_admin), ], ) +_logger = logging.getLogger(__name__) @router.get("/provider", response_model=ListLLMRsp, @@ -141,6 +143,7 @@ async def change_global_llm(request: Request, req: UpdateSpecialLlmReq) -> JSONR ), ) except ValueError as e: + _logger.exception("[llm] 更改全局LLM设置失败") return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content=jsonable_encoder( @@ -161,6 +164,7 @@ async def create_llm(req: UpdateLLMReq) -> JSONResponse: try: await LLMManager.update_llm(req.llm_id, req) except ValueError as e: + _logger.exception("[llm] 创建或更新大模型配置失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=jsonable_encoder( @@ -191,6 +195,7 @@ async def delete_llm(llmId: str) -> JSONResponse: # noqa: N803 try: await LLMManager.delete_llm(llmId) except ValueError as e: + _logger.exception("[llm] 删除大模型配置失败") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content=jsonable_encoder( diff --git a/apps/schemas/request_data.py b/apps/schemas/request_data.py index 28df1ab204e4db64243cb93af788d3b3074789ff..687b1cded4e3b8a2bb2101ef508a294eeab2ecd1 100644 --- a/apps/schemas/request_data.py +++ b/apps/schemas/request_data.py @@ -50,7 +50,7 @@ class PutFlowReq(BaseModel): class UpdateLLMReq(BaseModel): """更新大模型请求体""" - llm_id: str | None = Field(default=None, description="大模型ID", alias="id") + llm_id: str = Field(description="大模型ID", alias="id") base_url: str = Field(default="", description="OpenAI API Base URL", alias="baseUrl") api_key: str = Field(default="", description="OpenAI API Key", alias="apiKey") model_name: str | None = Field(default=None, description="模型名称", alias="modelName") diff --git a/apps/services/llm.py b/apps/services/llm.py index a3774f0022e3d00fffc2a95df0fd81d1f3b738df..925a24e48b54993982bed9e54d381fb75a6d2a8f 100644 --- a/apps/services/llm.py +++ b/apps/services/llm.py @@ -99,23 +99,23 @@ class LLMManager: @staticmethod - async def update_llm(llm_id: str | None, req: UpdateLLMReq) -> str: + async def update_llm(llm_id: str, req: UpdateLLMReq) -> str: """ - 创建大模型 + 创建或更新大模型 - :param req: 创建大模型请求体 - :return: 大模型对象 + :param llm_id: 大模型ID + :param req: 创建或更新大模型请求体 + :return: 大模型ID """ async with postgres.session() as session: - if llm_id is not None: - llm = (await session.scalars( - select(LLMData).where( - LLMData.id == llm_id, - ), - )).one_or_none() - if not llm: - err = f"[LLMManager] LLM {llm_id} 不存在" - raise ValueError(err) + llm = (await session.scalars( + select(LLMData).where( + LLMData.id == llm_id, + ), + )).one_or_none() + + if llm: + # 更新现有条目 llm.baseUrl = req.base_url llm.apiKey = req.api_key llm.modelName = req.model_name @@ -126,8 +126,8 @@ class LLMManager: if req.llm_type is not None: llm.llmType = req.llm_type llm.extraConfig = req.extra_data or {} - await session.commit() else: + # 创建新条目 llm_data = { "id": llm_id, "baseUrl": req.base_url, @@ -142,7 +142,8 @@ class LLMManager: } llm = LLMData(**llm_data) session.add(llm) - await session.commit() + + await session.commit() return llm.id