Appearance
自动添加 AgenticRagAgent 子智能体功能
📋 功能概述
当 Agent 绑定了知识库时,自动添加 AgenticRagAgent 作为子智能体,避免因用户忘记配置而导致 RAG 检索执行失败。
实现日期: 2026-01-24 状态: ✅ 已完成并编译通过 功能作者: Claude Opus 4.5
🎯 问题背景
用户反馈
用户在测试需要 RAG 的场景时,遇到以下错误:
✗ 未找到 Agentic RAG Agent,请确保已配置 AgenticRagAgent 作为子Agent
✓ 任务完成
结果: 执行失败: 未找到 Agentic RAG Agent,请确保已配置 AgenticRagAgent 作为子Agent问题分析
- ✅ ActionRouterNode 正确识别需要使用
agentic_rag - ✅ 条件路由 正确路由到
AgenticRagNode - ❌ AgenticRagNode 执行失败:Agent 配置中没有 AgenticRagAgent 子智能体
根本原因
用户创建 Agent 时:
- 绑定了知识库 ✅
- 但忘记添加 AgenticRagAgent 作为子智能体 ❌
这导致即使系统正确判断需要 RAG,也无法找到 AgenticRagAgent 来执行检索。
💡 解决方案
核心思想
自动化 + 健壮性:如果 Agent 绑定了知识库,在加载配置时自动添加 AgenticRagAgent 作为子智能体。
实现位置
AutoPlanningAgent.java 的 loadAgentConfig 方法
实现流程
加载 Agent 配置
↓
加载子 Agent 列表 (from database)
↓
加载知识库列表 (from database)
↓
【新增】检查:有知识库 && 没有 AgenticRagAgent?
↓ 是
查询数据库获取 AgenticRagAgent
↓
自动添加到子 Agent 列表
↓
✅ 更新 Agent 配置🔧 实现细节
1. 触发条件
在 loadAgentConfig 方法中,知识库加载完成后:
java
// 提取知识库检索配置供后续使用
if (!datasetList.isEmpty()) {
// ... 配置检索信息 ...
// 🔧 自动添加 AgenticRagAgent(如果有知识库但没有配置)
ensureAgenticRagAgent(config, handler);
}触发条件: datasetList 不为空(Agent 绑定了知识库)
2. ensureAgenticRagAgent 方法
功能实现
java
@SuppressWarnings("unchecked")
private void ensureAgenticRagAgent(Map<String, Object> config, StreamHandler handler) {
try {
// 1. 获取当前子 Agent 列表
List<Map<String, Object>> subAgents = (List<Map<String, Object>>) config.get("subAgents");
if (subAgents == null) {
subAgents = new ArrayList<>();
config.put("subAgents", subAgents);
}
// 2. 检查是否已经包含 AgenticRagAgent
boolean hasAgenticRagAgent = subAgents.stream()
.anyMatch(agent -> {
String name = (String) agent.get("name");
return name != null && name.contains("Agentic") && name.contains("RAG");
});
if (hasAgenticRagAgent) {
log.debug("AgenticRagAgent 已配置,无需自动添加");
return;
}
// 3. 查询数据库获取 AgenticRagAgent
if (aiAgentMapper == null) {
log.warn("AiAgentMapper 未初始化,无法自动添加 AgenticRagAgent");
return;
}
// 使用 MyBatis-Plus QueryWrapper 查询
QueryWrapper<AiAgentEntity> queryWrapper = new QueryWrapper<>();
queryWrapper
.like("name", "Agentic")
.like("name", "RAG")
.eq("publish", true) // 只查询已发布的
.last("LIMIT 1"); // 限制返回1条
AiAgentEntity agenticRagAgent = aiAgentMapper.selectOne(queryWrapper);
if (agenticRagAgent == null) {
log.warn("数据库中未找到 AgenticRagAgent,请确保已创建并发布该智能体");
if (handler != null) {
handler.onThinkingContent("load_config",
"⚠ 未找到 AgenticRagAgent,建议配置以支持智能 RAG 检索\n");
}
return;
}
// 4. 添加到子 Agent 列表
Map<String, Object> agentInfo = new HashMap<>();
agentInfo.put("id", agenticRagAgent.getId());
agentInfo.put("name", agenticRagAgent.getName());
agentInfo.put("description", agenticRagAgent.getDescription());
subAgents.add(agentInfo);
log.info("自动添加 AgenticRagAgent 作为子智能体: id={}, name={}",
agenticRagAgent.getId(), agenticRagAgent.getName());
if (handler != null) {
handler.onThinkingContent("load_config",
String.format("✓ **自动添加子Agent**: %s (ID: %d)\n",
agenticRagAgent.getName(), agenticRagAgent.getId()));
}
} catch (Exception e) {
log.error("自动添加 AgenticRagAgent 失败", e);
// 不抛出异常,避免影响整体流程
}
}关键设计
- 检查重复: 避免多次添加同一个 AgenticRagAgent
- 数据库查询: 使用 MyBatis-Plus QueryWrapper 查询
- 查询条件:
name LIKE '%Agentic%'name LIKE '%RAG%'publish = true(只查询已发布的)LIMIT 1(只取一条)
- 容错处理: 查询失败不抛异常,只记录日志
- 用户反馈: 通过 StreamHandler 实时推送添加信息
📊 执行流程示例
场景:用户创建了 Agent 并绑定知识库,但忘记添加 AgenticRagAgent
修复前
用户: "帮我查看知识库中招标文件中的甲方是谁"
↓
意图识别: needRag=true ✅
↓
任务规划: 生成步骤 "检索招标文件并识别甲方"
↓
ActionRouterNode: 判断 actionType=agentic_rag ✅
↓
AgenticRagNode: 查找 AgenticRagAgent
↓
❌ 未找到 AgenticRagAgent
↓
返回错误: "未找到 Agentic RAG Agent,请确保已配置"修复后
用户: "帮我查看知识库中招标文件中的甲方是谁"
↓
加载配置节点
├─> 加载子 Agent: 0 个
├─> 加载知识库: 1 个
└─> 🔧 自动添加 AgenticRagAgent (ID: 10) ✅
↓
意图识别: needRag=true ✅
↓
任务规划: 生成步骤 "检索招标文件并识别甲方"
↓
ActionRouterNode: 判断 actionType=agentic_rag ✅
↓
AgenticRagNode: 查找 AgenticRagAgent
├─> ✅ 找到 AgenticRagAgent (ID: 10)
└─> 调用执行
↓
✅ 返回检索结果📝 日志输出
成功自动添加
[INFO] AutoPlanningAgent - 加载Agent配置: name=客户服务助手, type=AUTO_PLANNING
[INFO] AutoPlanningAgent - 知识库检索配置已准备: datasetCount=1
[INFO] AutoPlanningAgent - 自动添加 AgenticRagAgent 作为子智能体: id=10, name=Agentic RAG智能检索增强生成智能体用户看到的反馈:
### 📋 配置加载
正在加载智能体的配置...
✓ **Agent名称**: 客户服务助手
✓ **LLM模型**: gpt-4o (openai)
✓ **知识库**: 1 个
✓ **自动添加子Agent**: Agentic RAG智能检索增强生成智能体 (ID: 10)
✓ 配置加载完成已经配置的情况
[DEBUG] AutoPlanningAgent - AgenticRagAgent 已配置,无需自动添加用户看到的反馈:
✓ **子Agent**: 1 个
✓ **知识库**: 1 个数据库中未找到
[WARN] AutoPlanningAgent - 数据库中未找到 AgenticRagAgent(name 包含 'Agentic' 和 'RAG'),请确保已创建并发布该智能体用户看到的反馈:
✓ **知识库**: 1 个
⚠ 未找到 AgenticRagAgent,建议配置以支持智能 RAG 检索🎯 关键优势
1. 用户体验提升
- ✅ 零配置: 用户绑定知识库后无需手动添加 AgenticRagAgent
- ✅ 自动化: 系统智能判断并自动配置
- ✅ 实时反馈: 通过思维链实时告知用户自动添加的操作
2. 健壮性增强
- ✅ 容错处理: 查询失败不影响整体流程
- ✅ 重复检查: 避免多次添加同一个 Agent
- ✅ 降级策略: AgenticRagAgent 不存在时给出友好提示
3. 系统一致性
- ✅ 统一逻辑: 所有绑定知识库的 Agent 都会自动配置 RAG 能力
- ✅ 即时生效: 配置加载时立即添加,无需重启
4. 可维护性
- ✅ 清晰日志: 详细记录自动添加过程
- ✅ 易于调试: 可通过日志追踪自动添加是否成功
- ✅ 灵活查询: 通过名称模糊匹配,适应 AgenticRagAgent 命名变化
🧪 测试场景
场景 1: Agent 绑定知识库,未配置 AgenticRagAgent
初始状态:
- Agent: 客户服务助手
- 知识库: 招标文档知识库 (ID: 1)
- 子 Agent: 无
预期行为:
- 加载配置时自动查询 AgenticRagAgent
- 找到并添加到子 Agent 列表
- 用户看到 "自动添加子Agent" 提示
- 后续执行 RAG 检索时能找到 AgenticRagAgent
验证日志:
[INFO] 自动添加 AgenticRagAgent 作为子智能体: id=10, name=Agentic RAG智能检索增强生成智能体场景 2: Agent 已配置 AgenticRagAgent
初始状态:
- Agent: 智能客服
- 知识库: 产品知识库 (ID: 2)
- 子 Agent: AgenticRagAgent (ID: 10)
预期行为:
- 检测到已配置 AgenticRagAgent
- 不重复添加
- 无额外提示
验证日志:
[DEBUG] AgenticRagAgent 已配置,无需自动添加场景 3: Agent 无知识库绑定
初始状态:
- Agent: 通用助手
- 知识库: 无
- 子 Agent: 无
预期行为:
- 不触发
ensureAgenticRagAgent方法 - 无自动添加操作
验证日志:
无相关日志(因为未进入 ensureAgenticRagAgent 方法)场景 4: 数据库中不存在 AgenticRagAgent
初始状态:
- Agent: 测试助手
- 知识库: 测试知识库 (ID: 3)
- 子 Agent: 无
- 数据库: 无 AgenticRagAgent(未创建或未发布)
预期行为:
- 查询数据库失败
- 记录警告日志
- 向用户显示友好提示
- 不影响整体配置加载流程
验证日志:
[WARN] 数据库中未找到 AgenticRagAgent(name 包含 'Agentic' 和 'RAG'),请确保已创建并发布该智能体用户反馈:
⚠ 未找到 AgenticRagAgent,建议配置以支持智能 RAG 检索🔍 查询逻辑详解
MyBatis-Plus QueryWrapper
java
QueryWrapper<AiAgentEntity> queryWrapper = new QueryWrapper<>();
queryWrapper
.like("name", "Agentic") // name LIKE '%Agentic%'
.like("name", "RAG") // AND name LIKE '%RAG%'
.eq("publish", true) // AND publish = true
.last("LIMIT 1"); // LIMIT 1SQL 等价:
sql
SELECT * FROM ai_agent
WHERE name LIKE '%Agentic%'
AND name LIKE '%RAG%'
AND publish = true
LIMIT 1;查询条件说明
| 条件 | 说明 | 原因 |
|---|---|---|
like("name", "Agentic") | 名称包含 "Agentic" | 匹配 AgenticRagAgent 的特征 |
like("name", "RAG") | 名称包含 "RAG" | 进一步精确匹配 |
eq("publish", true) | 只查询已发布的 | 避免使用未发布的测试版本 |
last("LIMIT 1") | 限制返回1条 | 提高查询效率,避免多余结果 |
为什么使用名称匹配?
灵活性: AgenticRagAgent 的名称可能有多种变体
- "Agentic RAG Agent"
- "Agentic RAG智能检索增强生成智能体"
- "AgenticRag Agent"
向后兼容: 即使 AgentType 枚举变化,仍能通过名称找到
简单可靠: 不依赖 AgentType 字段的具体值
📚 相关代码修改
修改的文件
| 文件 | 修改内容 | 状态 |
|---|---|---|
AutoPlanningAgent.java | 新增 ensureAgenticRagAgent 方法 | ✅ 完成 |
AutoPlanningAgent.java | 在 loadAgentConfig 中调用 | ✅ 完成 |
代码位置
- 调用位置:
AutoPlanningAgent.java:545 - 方法定义:
AutoPlanningAgent.java:755-819
🚀 部署步骤
1. 编译验证
bash
cd muotai-ai/muotai-ai-biz
mvn clean compile -DskipTests预期输出:
[INFO] BUILD SUCCESS2. 重启服务
bash
./restart.sh muotai-ai3. 验证功能
准备工作
- 创建一个测试 Agent
- 绑定知识库(不添加 AgenticRagAgent 子智能体)
- 确保数据库中已创建并发布 AgenticRagAgent
测试请求
json
{
"agentId": 123,
"message": "帮我查看知识库中的招标文件"
}查看日志
bash
tail -f logs/muotai-ai/info.log | grep -E "(自动添加|AgenticRagAgent|ensureAgenticRagAgent)"预期日志:
[INFO] AutoPlanningAgent - 知识库检索配置已准备: datasetCount=1
[INFO] AutoPlanningAgent - 自动添加 AgenticRagAgent 作为子智能体: id=10, name=Agentic RAG智能检索增强生成智能体用户界面反馈
### 📋 配置加载
正在加载智能体的配置...
✓ **Agent名称**: 测试助手
✓ **LLM模型**: gpt-4o (openai)
✓ **知识库**: 1 个
✓ **自动添加子Agent**: Agentic RAG智能检索增强生成智能体 (ID: 10)
✓ 配置加载完成🎓 设计原则
1. 最小惊讶原则
用户绑定知识库后,期望系统能自动处理 RAG 检索,而不需要手动配置 AgenticRagAgent。
2. 防御性编程
- 查询失败不抛异常
- 已配置则跳过
- 提供详细日志便于排查
3. 用户友好
- 实时反馈自动添加操作
- 清晰的日志和提示
- 不中断用户流程
4. 系统一致性
所有绑定知识库的 Agent 都会自动获得 RAG 检索能力。
🔮 未来优化
1. 配置化
允许用户在系统设置中禁用自动添加功能:
java
@Value("${ai.agent.auto-add-rag-agent:true}")
private boolean autoAddRagAgent;2. 版本管理
支持指定 AgenticRagAgent 的版本:
java
queryWrapper
.like("name", "Agentic")
.like("name", "RAG")
.eq("version", "v2.0") // 指定版本
.eq("publish", true);3. 多租户支持
在多租户环境中,查询当前租户的 AgenticRagAgent:
java
queryWrapper
.like("name", "Agentic")
.like("name", "RAG")
.eq("tenant_id", currentTenantId)
.eq("publish", true);创建时间: 2026-01-24 版本: 1.0.0 状态: ✅ 已完成并编译通过 功能作者: Claude Opus 4.5