rag-backend/src/RAG.Api/Endpoints/RAG/RAGQueryEndpoint.cs
向宁 dd9c2c85bb feat(validation): 添加请求参数校验器并优化依赖注入配置
- 为 AssignPermissionsEndpoint 添加请求校验器
- 为 AssignRolesEndpoint 添加请求校验器
- 为分片上传相关端点添加请求校验器
- 为聊天功能相关端点添加请求校验器
- 为知识库管理相关端点添加请求校验器
- 为用户和角色管理相关端点添加请求校验器
- 为嵌入向量化相关端点添加请求校验器
- 为认证授权相关端点添加请求校验器
- 为通知系统相关端点添加请求校验器
- 为 Obsidian 同步功能添加请求校验器
- 移除不必要的 using 语句和依赖注入配置
- 统一参数校验失败响应格式为 ValidationErrorResponse
2026-06-14 15:59:57 +08:00

41 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FastEndpoints;
using MediatR;
using RAG.Application.RagQA.Commands;
using RAG.Application.RagQA.DTOs;
namespace RAG.Api.Endpoints.RAG;
/// <summary>
/// RAG 一次性问答端点(非流式)。返回完整答案 + 命中的来源片段,
/// 适合轻量查询或非交互场景;交互式前端优先用 /rag/stream 获得流式体验。
/// </summary>
public class RAGQueryEndpoint(IMediator mediator) : Endpoint<RAGQueryRequest, RAGQueryResponse>
{
public override void Configure()
{
Post("/rag/query");
AllowAnonymous();
}
public override async Task HandleAsync(RAGQueryRequest req, CancellationToken ct)
{
var result = await mediator.Send(new RAGQueryCommand(req.KnowledgeBaseId, req.Question), ct);
await Send.OkAsync(result, ct);
}
}
/// <summary>
/// RAG 问答请求校验。/rag/query 与 /rag/stream 共用同一请求 DTO故此校验对两个端点同时生效。
/// </summary>
public class RAGQueryRequestValidator : Validator<RAGQueryRequest>
{
public RAGQueryRequestValidator()
{
RuleFor(x => x.KnowledgeBaseId).NotEmpty().WithMessage("知识库 ID 不能为空");
RuleFor(x => x.Question)
.NotEmpty().WithMessage("问题不能为空")
.MaximumLength(1000).WithMessage("问题不能超过 1000 个字符");
}
}