mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-22 13:05:18 +00:00
- 重构工作流模块目录结构,迁移代码文件 - 修复类型断言空值安全问题,添加 ! 操作符 - 优化样式类名,替换 flex-cc 为标准 flex 工具类 - 更新路由标签简化文案,移除冗余注释 - 调整 ruff 配置,放宽行长度限制 - 更新 README 与多语言文案,优化项目描述 - 修复表单、图表组件的类型与样式问题 - 简化搜索表单、数据卡片的布局代码
182 lines
5.1 KiB
Python
182 lines
5.1 KiB
Python
from typing import Annotated, Any
|
|
|
|
from fastapi import APIRouter, Depends, Path
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.common.response import ResponseSchema, SuccessResponse
|
|
from app.core.base_params import PaginationQueryParam
|
|
from app.core.base_schema import AuthSchema
|
|
from app.core.dependencies import AuthPermission
|
|
from app.core.router_class import OperationLogRoute
|
|
|
|
from .schema import (
|
|
AiChatRequestSchema,
|
|
AiChatResponseSchema,
|
|
ChatSessionCreateSchema,
|
|
ChatSessionQueryParam,
|
|
ChatSessionUpdateSchema,
|
|
)
|
|
from .service import ChatService
|
|
|
|
ChatRouter = APIRouter(route_class=OperationLogRoute, prefix="/chat", tags=["AI对话"])
|
|
|
|
|
|
@ChatRouter.get(
|
|
"/detail/{session_id}",
|
|
summary="获取会话详情",
|
|
response_model=ResponseSchema[dict[str, Any]],
|
|
)
|
|
async def get_session_detail_controller(
|
|
session_id: Annotated[str, Path(description="会话ID")],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_ai:chat:detail"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
获取会话详情
|
|
|
|
参数:
|
|
- session_id (str): 会话ID
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含会话详情的JSON响应
|
|
"""
|
|
result = await ChatService.get_session_service(auth=auth, session_id=session_id)
|
|
return SuccessResponse(data=result, msg="获取会话详情成功")
|
|
|
|
|
|
@ChatRouter.get(
|
|
"/list",
|
|
summary="查询会话列表",
|
|
response_model=ResponseSchema[dict],
|
|
)
|
|
async def get_session_list_controller(
|
|
page: Annotated[PaginationQueryParam, Depends()],
|
|
search: Annotated[ChatSessionQueryParam, Depends()],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_ai:chat:query"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
查询会话列表
|
|
|
|
参数:
|
|
- page (PaginationQueryParam): 分页查询参数
|
|
- search (ChatSessionQueryParam): 查询参数
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含会话列表分页信息的JSON响应
|
|
"""
|
|
result_dict = await ChatService.page_service(
|
|
auth=auth,
|
|
page_no=page.page_no,
|
|
page_size=page.page_size,
|
|
search=search,
|
|
order_by=page.order_by,
|
|
)
|
|
return SuccessResponse(data=result_dict, msg="查询会话列表成功")
|
|
|
|
|
|
@ChatRouter.post(
|
|
"/create",
|
|
summary="创建会话",
|
|
response_model=ResponseSchema[dict[str, Any]],
|
|
)
|
|
async def create_session_controller(
|
|
data: ChatSessionCreateSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_ai:chat:create"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
创建会话
|
|
|
|
参数:
|
|
- data (ChatSessionCreateSchema): 会话创建模型
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含创建会话详情的JSON响应
|
|
"""
|
|
result = await ChatService.create_service(auth=auth, data=data)
|
|
return SuccessResponse(data=result, msg="创建会话成功")
|
|
|
|
|
|
@ChatRouter.put(
|
|
"/update/{session_id}",
|
|
summary="更新会话",
|
|
response_model=ResponseSchema[None],
|
|
)
|
|
async def update_session_controller(
|
|
session_id: Annotated[str, Path(description="会话ID")],
|
|
data: ChatSessionUpdateSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_ai:chat:update"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
更新会话
|
|
|
|
参数:
|
|
- session_id (str): 会话ID
|
|
- data (ChatSessionUpdateSchema): 会话更新模型
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含更新会话详情的JSON响应
|
|
"""
|
|
await ChatService.update_service(auth=auth, session_id=session_id, data=data)
|
|
return SuccessResponse(data=None, msg="更新会话成功")
|
|
|
|
|
|
@ChatRouter.delete(
|
|
"/delete",
|
|
summary="删除会话",
|
|
response_model=ResponseSchema[None],
|
|
)
|
|
async def delete_session_controller(
|
|
session_ids: list[str],
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_ai:chat:delete"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
删除会话
|
|
|
|
参数:
|
|
- session_ids (list[str]): 会话ID列表
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含删除结果的JSON响应
|
|
"""
|
|
await ChatService.delete_service(auth=auth, session_ids=session_ids)
|
|
return SuccessResponse(data=None, msg="删除会话成功")
|
|
|
|
|
|
@ChatRouter.post(
|
|
"/ai-chat",
|
|
summary="AI 对话(非流式)",
|
|
response_model=ResponseSchema[AiChatResponseSchema],
|
|
)
|
|
async def ai_chat_controller(
|
|
data: AiChatRequestSchema,
|
|
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_ai:chat:query"]))],
|
|
) -> JSONResponse:
|
|
"""
|
|
AI 对话(非流式)
|
|
|
|
参数:
|
|
- data (AiChatRequestSchema): 对话请求数据
|
|
- auth (AuthSchema): 认证信息模型
|
|
|
|
返回:
|
|
- JSONResponse: 包含 AI 回复、会话ID和函数调用信息的JSON响应
|
|
"""
|
|
result = await ChatService.chat_non_stream(
|
|
message=data.message,
|
|
session_id=data.session_id,
|
|
auth=auth,
|
|
)
|
|
return SuccessResponse(
|
|
data=AiChatResponseSchema(
|
|
response=result["response"],
|
|
session_id=result["session_id"],
|
|
function_calls=result.get("function_calls"),
|
|
action=result.get("action"),
|
|
),
|
|
msg="对话成功",
|
|
)
|