feat: 接入FastGPT知识库RAG检索,AI回答前先查医学资料
- 新增 FastGptKnowledgeClient:调用 searchTest 接口检索知识库 - ai_chat_endpoints: 调 DeepSeek 之前先检索,把相关片段拼到 system prompt - 5个智能体(default/consultation/diet/report/unified)启用 RAG - 3个纯工具调用智能体(health/medication/exercise)保持原样 - .env: 新增 FastGPT 配置(API Key、datasetId、相似度等) - api_client: 更新默认 baseUrl 适配新网络环境
This commit is contained in:
@@ -33,6 +33,7 @@ public static class AiChatEndpoints
|
||||
HttpContext http,
|
||||
DeepSeekClient llmClient,
|
||||
PromptManager promptManager,
|
||||
FastGptKnowledgeClient knowledgeClient,
|
||||
IAiToolExecutionService toolExecution,
|
||||
IAiWriteConfirmationStore confirmations,
|
||||
IAiConversationService conversations,
|
||||
@@ -110,9 +111,24 @@ public static class AiChatEndpoints
|
||||
var systemPrompt = promptManager.GetSystemPrompt(parsedType);
|
||||
var patientContext = await patientContexts.BuildAsync(userId.Value, ct);
|
||||
|
||||
// ── RAG 知识库检索 ──
|
||||
// 仅对需要医学知识的 Agent 启用检索;记数据/管用药/排运动这些纯工具调用类跳过。
|
||||
// 失败/空结果不阻塞流程,仅作为 prompt 增强。
|
||||
var knowledgeSnippet = "";
|
||||
if (NeedsKnowledgeBase(parsedType) && knowledgeClient.IsEnabled)
|
||||
{
|
||||
knowledgeSnippet = await knowledgeClient.SearchAsync(message, ct);
|
||||
}
|
||||
|
||||
var enhancedSystem = systemPrompt + "\n\n当前患者信息:\n" + patientContext;
|
||||
if (!string.IsNullOrWhiteSpace(knowledgeSnippet))
|
||||
{
|
||||
enhancedSystem += "\n\n知识库参考资料(如与用户问题相关请优先采用):\n" + knowledgeSnippet;
|
||||
}
|
||||
|
||||
var messages = new List<ChatMessage>
|
||||
{
|
||||
new() { Role = "system", Content = systemPrompt + "\n\n当前患者信息:\n" + patientContext },
|
||||
new() { Role = "system", Content = enhancedSystem },
|
||||
};
|
||||
|
||||
// 加载历史对话(最近 10 条)
|
||||
@@ -303,6 +319,20 @@ public static class AiChatEndpoints
|
||||
|
||||
// ── Agent / Tool 调度 ──
|
||||
|
||||
/// <summary>
|
||||
/// 哪些 Agent 需要走 FastGPT 知识库 RAG。
|
||||
/// 纯工具调用类(Health/Medication/Exercise)跳过 RAG,避免拖慢响应。
|
||||
/// </summary>
|
||||
private static bool NeedsKnowledgeBase(AgentType agentType) => agentType switch
|
||||
{
|
||||
AgentType.Default => true,
|
||||
AgentType.Consultation => true,
|
||||
AgentType.Diet => true,
|
||||
AgentType.Report => true,
|
||||
AgentType.Unified => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
private static List<ToolDefinition> GetToolsForAgent(AgentType agentType) => agentType switch
|
||||
{
|
||||
AgentType.Health => HealthDataAgentHandler.Tools,
|
||||
|
||||
Reference in New Issue
Block a user