mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-11 20:05:44 +00:00
feat(ai): 重构AI聊天模块,新增会话管理和文件上传功能
- 重构前端聊天界面布局,新增侧边栏管理会话 - 实现会话创建、切换和消息历史加载功能 - 新增文件上传和消息附件支持 - 优化WebSocket连接处理,支持token认证 - 后端新增会话和消息模型,重构WebSocket处理逻辑 - 移除旧的知识库和智能体配置相关代码 - 优化UI样式和交互体验
This commit is contained in:
@@ -1,489 +1,330 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, Path
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.api.v1.module_system.auth.schema import AuthSchema
|
||||
from app.common.request import PaginationService
|
||||
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
|
||||
from app.common.response import ResponseSchema, SuccessResponse
|
||||
from app.core.base_params import PaginationQueryParam
|
||||
from app.core.dependencies import AuthPermission
|
||||
from app.core.logger import log
|
||||
from app.core.router_class import OperationLogRoute
|
||||
|
||||
from .schema import (
|
||||
AgentConfigCreateSchema,
|
||||
AgentConfigOutSchema,
|
||||
AgentConfigQueryParam,
|
||||
AgentConfigUpdateSchema,
|
||||
ChatQuerySchema,
|
||||
KnowledgeCreateSchema,
|
||||
KnowledgeDocumentCreateSchema,
|
||||
KnowledgeDocumentOutSchema,
|
||||
KnowledgeDocumentQueryParam,
|
||||
KnowledgeDocumentUpdateSchema,
|
||||
KnowledgeOutSchema,
|
||||
KnowledgeQueryParam,
|
||||
KnowledgeUpdateSchema,
|
||||
ChatMessageCreateSchema,
|
||||
ChatMessageOutSchema,
|
||||
ChatMessageQueryParam,
|
||||
ChatMessageUpdateSchema,
|
||||
ChatSessionCreateSchema,
|
||||
ChatSessionOutSchema,
|
||||
ChatSessionQueryParam,
|
||||
ChatSessionUpdateSchema,
|
||||
)
|
||||
from .service import AgentConfigService, AgentService, KnowledgeService
|
||||
from .service import ChatMessageService, ChatSessionService
|
||||
|
||||
AIRouter = APIRouter(route_class=OperationLogRoute, prefix="/ai", tags=["AI模块"])
|
||||
|
||||
AIRouter = APIRouter(route_class=OperationLogRoute, prefix="/ai", tags=["智能助手"])
|
||||
|
||||
|
||||
@AIRouter.get(
|
||||
"/agent-config/detail/{id}",
|
||||
summary="获取智能体配置详情",
|
||||
description="获取智能体配置详情",
|
||||
response_model=ResponseSchema[AgentConfigOutSchema],
|
||||
"/session/detail/{id}",
|
||||
summary="获取聊天会话详情",
|
||||
description="获取聊天会话详情",
|
||||
response_model=ResponseSchema[ChatSessionOutSchema],
|
||||
)
|
||||
async def agent_config_detail_controller(
|
||||
id: Annotated[int, Path(description="智能体配置ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:agent-config:detail"]))],
|
||||
) -> SuccessResponse:
|
||||
async def get_session_detail_controller(
|
||||
id: Annotated[int, Path(description="会话ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_session:detail"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取智能体配置详情接口
|
||||
|
||||
参数:
|
||||
- id (int): 智能体配置ID
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含智能体配置详情的响应
|
||||
"""
|
||||
result_dict = await AgentConfigService.get_by_id_service(auth=auth, id=id)
|
||||
log.info(f"获取智能体配置详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取智能体配置详情成功")
|
||||
|
||||
|
||||
@AIRouter.get(
|
||||
"/agent-config/default",
|
||||
summary="获取默认智能体配置",
|
||||
description="获取默认智能体配置",
|
||||
response_model=ResponseSchema[AgentConfigOutSchema],
|
||||
)
|
||||
async def agent_config_default_controller(
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:agent-config:query"]))],
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
获取默认智能体配置接口
|
||||
获取聊天会话详情
|
||||
|
||||
参数:
|
||||
- id (str): 会话ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含默认智能体配置的响应
|
||||
- JSONResponse: 包含会话详情的JSON响应
|
||||
"""
|
||||
result_dict = await AgentConfigService.get_default_service(auth=auth)
|
||||
log.info("获取默认智能体配置成功")
|
||||
return SuccessResponse(data=result_dict, msg="获取默认智能体配置成功")
|
||||
result_dict = await ChatSessionService.detail_service(id=id, auth=auth)
|
||||
log.info(f"获取聊天会话详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取聊天会话详情成功")
|
||||
|
||||
|
||||
@AIRouter.get(
|
||||
"/agent-config/list",
|
||||
summary="查询智能体配置列表",
|
||||
description="查询智能体配置列表",
|
||||
response_model=ResponseSchema[list[AgentConfigOutSchema]],
|
||||
"/session/list",
|
||||
summary="查询聊天会话列表",
|
||||
description="查询聊天会话列表",
|
||||
response_model=ResponseSchema[list[ChatSessionOutSchema]],
|
||||
)
|
||||
async def agent_config_list_controller(
|
||||
async def get_session_list_controller(
|
||||
page: Annotated[PaginationQueryParam, Depends()],
|
||||
search: Annotated[AgentConfigQueryParam, Depends()],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:agent-config:query"]))],
|
||||
) -> SuccessResponse:
|
||||
search: Annotated[ChatSessionQueryParam, Depends()],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_session:query"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询智能体配置列表接口
|
||||
查询聊天会话列表
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数模型
|
||||
- search (AgentConfigQueryParam): 查询参数模型
|
||||
- page (PaginationQueryParam): 分页查询参数
|
||||
- search (ChatSessionQueryParam): 查询参数
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含智能体配置列表的响应
|
||||
- JSONResponse: 包含会话列表分页信息的JSON响应
|
||||
"""
|
||||
result_dict = await AgentConfigService.get_list_service(auth=auth, query_params=search)
|
||||
result_dict = await PaginationService.paginate(
|
||||
data_list=result_dict["data"],
|
||||
result_dict = await ChatSessionService.page_service(
|
||||
auth=auth,
|
||||
page_no=page.page_no,
|
||||
page_size=page.page_size,
|
||||
search=search,
|
||||
order_by=page.order_by,
|
||||
)
|
||||
log.info("查询智能体配置列表成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询智能体配置列表成功")
|
||||
log.info("查询聊天会话列表成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询聊天会话列表成功")
|
||||
|
||||
|
||||
@AIRouter.post(
|
||||
"/agent-config/create",
|
||||
summary="创建智能体配置",
|
||||
description="创建智能体配置",
|
||||
response_model=ResponseSchema[AgentConfigOutSchema],
|
||||
"/session/create",
|
||||
summary="创建聊天会话",
|
||||
description="创建聊天会话",
|
||||
response_model=ResponseSchema[ChatSessionOutSchema],
|
||||
)
|
||||
async def agent_config_create_controller(
|
||||
data: AgentConfigCreateSchema,
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:agent-config:create"]))],
|
||||
) -> SuccessResponse:
|
||||
async def create_session_controller(
|
||||
data: ChatSessionCreateSchema,
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_session:create"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建智能体配置接口
|
||||
创建聊天会话
|
||||
|
||||
参数:
|
||||
- data (AgentConfigCreateSchema): 创建智能体配置模型
|
||||
- data (ChatSessionCreateSchema): 会话创建模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含创建智能体配置结果的响应
|
||||
- JSONResponse: 包含创建会话详情的JSON响应
|
||||
"""
|
||||
result_dict = await AgentConfigService.create_service(auth=auth, data=data)
|
||||
log.info(f"创建智能体配置成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建智能体配置成功")
|
||||
result_dict = await ChatSessionService.create_service(auth=auth, data=data)
|
||||
log.info(f"创建聊天会话成功: {result_dict.get('title')}")
|
||||
return SuccessResponse(data=result_dict, msg="创建聊天会话成功")
|
||||
|
||||
|
||||
@AIRouter.put(
|
||||
"/agent-config/update/{id}",
|
||||
summary="修改智能体配置",
|
||||
description="修改智能体配置",
|
||||
response_model=ResponseSchema[AgentConfigOutSchema],
|
||||
"/session/update/{id}",
|
||||
summary="修改聊天会话",
|
||||
description="修改聊天会话",
|
||||
response_model=ResponseSchema[ChatSessionOutSchema],
|
||||
)
|
||||
async def agent_config_update_controller(
|
||||
data: AgentConfigUpdateSchema,
|
||||
id: Annotated[int, Path(description="智能体配置ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:agent-config:update"]))],
|
||||
) -> SuccessResponse:
|
||||
async def update_session_controller(
|
||||
data: ChatSessionUpdateSchema,
|
||||
id: Annotated[int, Path(description="会话ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_session:update"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改智能体配置接口
|
||||
修改聊天会话
|
||||
|
||||
参数:
|
||||
- data (AgentConfigUpdateSchema): 修改智能体配置模型
|
||||
- id (int): 智能体配置ID
|
||||
- data (ChatSessionUpdateSchema): 会话更新模型
|
||||
- id (int): 会话ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含修改智能体配置结果的响应
|
||||
- JSONResponse: 包含修改会话详情的JSON响应
|
||||
"""
|
||||
result_dict = await AgentConfigService.update_service(auth=auth, id=id, data=data)
|
||||
log.info(f"修改智能体配置成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改智能体配置成功")
|
||||
result_dict = await ChatSessionService.update_service(auth=auth, id=id, data=data)
|
||||
log.info(f"修改聊天会话成功: {result_dict.get('title')}")
|
||||
return SuccessResponse(data=result_dict, msg="修改聊天会话成功")
|
||||
|
||||
|
||||
@AIRouter.delete(
|
||||
"/agent-config/delete",
|
||||
summary="删除智能体配置",
|
||||
description="删除智能体配置",
|
||||
"/session/delete",
|
||||
summary="删除聊天会话",
|
||||
description="删除聊天会话",
|
||||
response_model=ResponseSchema[None],
|
||||
)
|
||||
async def agent_config_delete_controller(
|
||||
ids: Annotated[list[int], Body(description="ID列表")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:agent-config:delete"]))],
|
||||
) -> SuccessResponse:
|
||||
async def delete_session_controller(
|
||||
ids: Annotated[list[int], Body(description="会话ID列表")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_session:delete"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除智能体配置接口
|
||||
删除聊天会话
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 智能体配置ID列表
|
||||
- ids (list[int]): 会话ID列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含删除智能体配置结果的响应
|
||||
- JSONResponse: 包含删除会话详情的JSON响应
|
||||
"""
|
||||
await AgentConfigService.delete_service(auth=auth, ids=ids)
|
||||
log.info(f"删除智能体配置成功: {ids}")
|
||||
return SuccessResponse(msg="删除智能体配置成功")
|
||||
|
||||
|
||||
@AIRouter.post(
|
||||
"/chat",
|
||||
summary="智能对话",
|
||||
description="与智能助手进行对话",
|
||||
)
|
||||
async def chat_controller(
|
||||
query: ChatQuerySchema,
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai:chat"]))],
|
||||
) -> StreamResponse:
|
||||
"""
|
||||
智能对话接口
|
||||
|
||||
参数:
|
||||
- query (ChatQuerySchema): 聊天查询模型
|
||||
|
||||
返回:
|
||||
- StreamingResponse: 流式响应,每次返回一个聊天响应
|
||||
"""
|
||||
user_name = auth.user.name if auth.user else "未知用户"
|
||||
log.info(f"用户 {user_name} 发起智能对话: {query.message[:50]}...")
|
||||
|
||||
async def generate_response():
|
||||
try:
|
||||
async for chunk in AgentService.chat_query(query=query):
|
||||
if chunk:
|
||||
yield (chunk.encode("utf-8") if isinstance(chunk, str) else chunk)
|
||||
except Exception as e:
|
||||
log.error(f"流式响应出错: {e!s}")
|
||||
yield f"抱歉,处理您的请求时出现了错误: {e!s}".encode()
|
||||
|
||||
return StreamResponse(generate_response(), media_type="text/plain; charset=utf-8")
|
||||
await ChatSessionService.delete_service(auth=auth, ids=ids)
|
||||
log.info(f"删除聊天会话成功: {ids}")
|
||||
return SuccessResponse(msg="删除聊天会话成功")
|
||||
|
||||
|
||||
@AIRouter.get(
|
||||
"/knowledge/detail/{id}",
|
||||
summary="获取知识库详情",
|
||||
description="获取知识库详情",
|
||||
response_model=ResponseSchema[KnowledgeOutSchema],
|
||||
"/message/detail/{id}",
|
||||
summary="获取聊天消息详情",
|
||||
description="获取聊天消息详情",
|
||||
response_model=ResponseSchema[ChatMessageOutSchema],
|
||||
)
|
||||
async def knowledge_detail_controller(
|
||||
id: Annotated[int, Path(description="知识库ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:knowledge:query"]))],
|
||||
) -> SuccessResponse:
|
||||
async def get_message_detail_controller(
|
||||
id: Annotated[int, Path(description="消息ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_message:detail"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
获取知识库详情接口
|
||||
获取聊天消息详情
|
||||
|
||||
参数:
|
||||
- id (int): 知识库ID
|
||||
- id (int): 消息ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含知识库详情的响应
|
||||
- JSONResponse: 包含消息详情的JSON响应
|
||||
"""
|
||||
result_dict = await KnowledgeService.detail_service(auth=auth, id=id)
|
||||
log.info(f"获取知识库详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取知识库详情成功")
|
||||
result_dict = await ChatMessageService.detail_service(id=id, auth=auth)
|
||||
log.info(f"获取聊天消息详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取聊天消息详情成功")
|
||||
|
||||
|
||||
@AIRouter.get(
|
||||
"/knowledge/list",
|
||||
summary="查询知识库列表",
|
||||
description="查询知识库列表",
|
||||
response_model=ResponseSchema[list[KnowledgeOutSchema]],
|
||||
"/message/list",
|
||||
summary="查询聊天消息列表",
|
||||
description="查询聊天消息列表",
|
||||
response_model=ResponseSchema[list[ChatMessageOutSchema]],
|
||||
)
|
||||
async def knowledge_list_controller(
|
||||
async def get_message_list_controller(
|
||||
page: Annotated[PaginationQueryParam, Depends()],
|
||||
search: Annotated[KnowledgeQueryParam, Depends()],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:knowledge:query"]))],
|
||||
) -> SuccessResponse:
|
||||
search: Annotated[ChatMessageQueryParam, Depends()],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_message:query"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询知识库列表接口
|
||||
查询聊天消息列表
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数模型
|
||||
- search (KnowledgeQueryParam): 查询参数模型
|
||||
- page (PaginationQueryParam): 分页查询参数
|
||||
- search (ChatMessageQueryParam): 查询参数
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含知识库列表的响应
|
||||
- JSONResponse: 包含消息列表分页信息的JSON响应
|
||||
"""
|
||||
result_dict_list = await KnowledgeService.list_service(
|
||||
auth=auth, search=search, order_by=page.order_by
|
||||
result_dict = await ChatMessageService.page_service(
|
||||
auth=auth,
|
||||
page_no=page.page_no,
|
||||
page_size=page.page_size,
|
||||
search=search,
|
||||
order_by=page.order_by,
|
||||
)
|
||||
result_dict = await PaginationService.paginate(
|
||||
data_list=result_dict_list,
|
||||
log.info("查询聊天消息列表成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询聊天消息列表成功")
|
||||
|
||||
|
||||
@AIRouter.get(
|
||||
"/message/session/{session_id}",
|
||||
summary="按会话ID获取聊天消息",
|
||||
description="按会话ID获取聊天消息",
|
||||
response_model=ResponseSchema[list[ChatMessageOutSchema]],
|
||||
)
|
||||
async def get_message_by_session_id_controller(
|
||||
session_id: Annotated[int, Path(description="会话ID")],
|
||||
page: Annotated[PaginationQueryParam, Depends()],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_message:query"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
按会话ID获取聊天消息
|
||||
|
||||
参数:
|
||||
- session_id (int): 会话ID
|
||||
- page (PaginationQueryParam): 分页查询参数
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含消息列表分页信息的JSON响应
|
||||
"""
|
||||
result_dict = await ChatMessageService.get_by_session_id_service(
|
||||
auth=auth,
|
||||
session_id=session_id,
|
||||
page_no=page.page_no,
|
||||
page_size=page.page_size,
|
||||
)
|
||||
log.info("查询知识库列表成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询知识库列表成功")
|
||||
log.info(f"按会话ID获取聊天消息成功: {session_id}")
|
||||
return SuccessResponse(data=result_dict, msg="按会话ID获取聊天消息成功")
|
||||
|
||||
|
||||
@AIRouter.post(
|
||||
"/knowledge/create",
|
||||
summary="创建知识库",
|
||||
description="创建知识库",
|
||||
response_model=ResponseSchema[KnowledgeOutSchema],
|
||||
"/message/create",
|
||||
summary="创建聊天消息",
|
||||
description="创建聊天消息",
|
||||
response_model=ResponseSchema[ChatMessageOutSchema],
|
||||
)
|
||||
async def knowledge_create_controller(
|
||||
data: KnowledgeCreateSchema,
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:knowledge:create"]))],
|
||||
) -> SuccessResponse:
|
||||
async def create_message_controller(
|
||||
data: ChatMessageCreateSchema,
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_message:create"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建知识库接口
|
||||
创建聊天消息
|
||||
|
||||
参数:
|
||||
- data (KnowledgeCreateSchema): 创建知识库模型
|
||||
- data (ChatMessageCreateSchema): 消息创建模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含创建知识库结果的响应
|
||||
- JSONResponse: 包含创建消息详情的JSON响应
|
||||
"""
|
||||
result_dict = await KnowledgeService.create_service(auth=auth, data=data)
|
||||
log.info(f"创建知识库成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建知识库成功")
|
||||
result_dict = await ChatMessageService.create_service(auth=auth, data=data)
|
||||
content = result_dict.get('content', '')[:50] if result_dict.get('content') else ''
|
||||
log.info(f"创建聊天消息成功: {content}...")
|
||||
return SuccessResponse(data=result_dict, msg="创建聊天消息成功")
|
||||
|
||||
|
||||
@AIRouter.put(
|
||||
"/knowledge/update/{id}",
|
||||
summary="修改知识库",
|
||||
description="修改知识库",
|
||||
response_model=ResponseSchema[KnowledgeOutSchema],
|
||||
"/message/update/{id}",
|
||||
summary="修改聊天消息",
|
||||
description="修改聊天消息",
|
||||
response_model=ResponseSchema[ChatMessageOutSchema],
|
||||
)
|
||||
async def knowledge_update_controller(
|
||||
data: KnowledgeUpdateSchema,
|
||||
id: Annotated[int, Path(description="知识库ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:knowledge:update"]))],
|
||||
) -> SuccessResponse:
|
||||
async def update_message_controller(
|
||||
data: ChatMessageUpdateSchema,
|
||||
id: Annotated[int, Path(description="消息ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_message:update"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改知识库接口
|
||||
修改聊天消息
|
||||
|
||||
参数:
|
||||
- data (KnowledgeUpdateSchema): 修改知识库模型
|
||||
- id (int): 知识库ID
|
||||
- data (ChatMessageUpdateSchema): 消息更新模型
|
||||
- id (int): 消息ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含修改知识库结果的响应
|
||||
- JSONResponse: 包含修改消息详情的JSON响应
|
||||
"""
|
||||
result_dict = await KnowledgeService.update_service(auth=auth, id=id, data=data)
|
||||
log.info(f"修改知识库成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改知识库成功")
|
||||
result_dict = await ChatMessageService.update_service(auth=auth, id=id, data=data)
|
||||
content = result_dict.get('content', '')[:50] if result_dict.get('content') else ''
|
||||
log.info(f"修改聊天消息成功: {content}...")
|
||||
return SuccessResponse(data=result_dict, msg="修改聊天消息成功")
|
||||
|
||||
|
||||
@AIRouter.delete(
|
||||
"/knowledge/delete",
|
||||
summary="删除知识库",
|
||||
description="删除知识库",
|
||||
"/message/delete",
|
||||
summary="删除聊天消息",
|
||||
description="删除聊天消息",
|
||||
response_model=ResponseSchema[None],
|
||||
)
|
||||
async def knowledge_delete_controller(
|
||||
ids: Annotated[list[int], Body(description="ID列表")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:knowledge:delete"]))],
|
||||
) -> SuccessResponse:
|
||||
async def delete_message_controller(
|
||||
ids: Annotated[list[int], Body(description="消息ID列表")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:ai_message:delete"]))],
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除知识库接口
|
||||
删除聊天消息
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 知识库ID列表
|
||||
- ids (list[int]): 消息ID列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含删除知识库结果的响应
|
||||
- JSONResponse: 包含删除消息详情的JSON响应
|
||||
"""
|
||||
await KnowledgeService.delete_service(auth=auth, ids=ids)
|
||||
log.info(f"删除知识库成功: {ids}")
|
||||
return SuccessResponse(msg="删除知识库成功")
|
||||
|
||||
|
||||
@AIRouter.get(
|
||||
"/document/detail/{id}",
|
||||
summary="获取知识库文档详情",
|
||||
description="获取知识库文档详情",
|
||||
response_model=ResponseSchema[KnowledgeDocumentOutSchema],
|
||||
)
|
||||
async def document_detail_controller(
|
||||
id: Annotated[int, Path(description="文档ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:document:query"]))],
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
获取知识库文档详情接口
|
||||
|
||||
参数:
|
||||
- id (int): 文档ID
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含文档详情的响应
|
||||
"""
|
||||
result_dict = await KnowledgeService.document_detail_service(auth=auth, id=id)
|
||||
log.info(f"获取知识库文档详情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取知识库文档详情成功")
|
||||
|
||||
|
||||
@AIRouter.get(
|
||||
"/document/list",
|
||||
summary="查询知识库文档列表",
|
||||
description="查询知识库文档列表",
|
||||
response_model=ResponseSchema[list[KnowledgeDocumentOutSchema]],
|
||||
)
|
||||
async def document_list_controller(
|
||||
page: Annotated[PaginationQueryParam, Depends()],
|
||||
search: Annotated[KnowledgeDocumentQueryParam, Depends()],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:document:query"]))],
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
查询知识库文档列表接口
|
||||
|
||||
参数:
|
||||
- page (PaginationQueryParam): 分页查询参数模型
|
||||
- search (KnowledgeDocumentQueryParam): 查询参数模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含文档列表的响应
|
||||
"""
|
||||
result_dict_list = await KnowledgeService.document_list_service(
|
||||
auth=auth, search=search, order_by=page.order_by
|
||||
)
|
||||
result_dict = await PaginationService.paginate(
|
||||
data_list=result_dict_list,
|
||||
page_no=page.page_no,
|
||||
page_size=page.page_size,
|
||||
)
|
||||
log.info("查询知识库文档列表成功")
|
||||
return SuccessResponse(data=result_dict, msg="查询知识库文档列表成功")
|
||||
|
||||
|
||||
@AIRouter.post(
|
||||
"/document/create",
|
||||
summary="创建知识库文档",
|
||||
description="创建知识库文档",
|
||||
response_model=ResponseSchema[KnowledgeDocumentOutSchema],
|
||||
)
|
||||
async def document_create_controller(
|
||||
data: KnowledgeDocumentCreateSchema,
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:document:create"]))],
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
创建知识库文档接口
|
||||
|
||||
参数:
|
||||
- data (KnowledgeDocumentCreateSchema): 创建文档模型
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含创建文档结果的响应
|
||||
"""
|
||||
result_dict = await KnowledgeService.document_create_service(auth=auth, data=data)
|
||||
log.info(f"创建知识库文档成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建知识库文档成功")
|
||||
|
||||
|
||||
@AIRouter.put(
|
||||
"/document/update/{id}",
|
||||
summary="修改知识库文档",
|
||||
description="修改知识库文档",
|
||||
response_model=ResponseSchema[KnowledgeDocumentOutSchema],
|
||||
)
|
||||
async def document_update_controller(
|
||||
data: KnowledgeDocumentUpdateSchema,
|
||||
id: Annotated[int, Path(description="文档ID")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:document:update"]))],
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
修改知识库文档接口
|
||||
|
||||
参数:
|
||||
- data (KnowledgeDocumentUpdateSchema): 修改文档模型
|
||||
- id (int): 文档ID
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含修改文档结果的响应
|
||||
"""
|
||||
result_dict = await KnowledgeService.document_update_service(auth=auth, id=id, data=data)
|
||||
log.info(f"修改知识库文档成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改知识库文档成功")
|
||||
|
||||
|
||||
@AIRouter.delete(
|
||||
"/document/delete",
|
||||
summary="删除知识库文档",
|
||||
description="删除知识库文档",
|
||||
response_model=ResponseSchema[None],
|
||||
)
|
||||
async def document_delete_controller(
|
||||
ids: Annotated[list[int], Body(description="ID列表")],
|
||||
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_application:document:delete"]))],
|
||||
) -> SuccessResponse:
|
||||
"""
|
||||
删除知识库文档接口
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 文档ID列表
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
|
||||
返回:
|
||||
- SuccessResponse: 包含删除文档结果的响应
|
||||
"""
|
||||
await KnowledgeService.document_delete_service(auth=auth, ids=ids)
|
||||
log.info(f"删除知识库文档成功: {ids}")
|
||||
return SuccessResponse(msg="删除知识库文档成功")
|
||||
await ChatMessageService.delete_service(auth=auth, ids=ids)
|
||||
log.info(f"删除聊天消息成功: {ids}")
|
||||
return SuccessResponse(msg="删除聊天消息成功")
|
||||
|
||||
Reference in New Issue
Block a user