liusuyi
2026-06-02 f652169cc5d6501511eece5df57b7b396fd7a7ab
ard-modules/ard-modules-agent/src/main/java/com/ard/agent/config/AgentConfig.java
@@ -3,73 +3,64 @@
import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import com.alibaba.cloud.ai.graph.agent.hook.modelcalllimit.ModelCallLimitHook;
import com.alibaba.cloud.ai.graph.checkpoint.savers.MemorySaver;
import com.ard.agent.tool.KnowledgeTools;
import com.ard.agent.tool.MonitorTools;
import com.ard.agent.tool.SystemTools;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AgentConfig {
    @Resource
    @Qualifier("ollamaChatModel")
    @Resource(name = "ollamaChatModel")
    private ChatModel chatModel;
    @Resource
    private MonitorTools monitorTools;
    @Resource
    private ChatMemory chatMemory;  // 注入聊天记忆
    private SystemTools systemTools;
    @Resource
    private KnowledgeTools knowledgeTools;
    private static final String UNIFIED_AGENT_PROMPT = """
            你是 ARD 智能业务助手,能使用摄像机管理、系统管理和知识库工具。
    // 增强版的系统提示词,专门为 ReAct 模式优化
    private static final String SYSTEM_PROMPT = """
            你是摄像头管理助手,拥有调用工具和查询知识库的能力。
            ## 工作方式
            你需要通过「思考 → 行动 → 观察」的循环来完成任务:
            1. **思考 (Thought)**:分析用户需求,判断下一步该做什么
            2. **行动 (Action)**:决定调用哪个工具
               - getCameraDetail: 查询单个摄像机详情
               - searchCameras: 按条件搜索摄像机
               - updateCameraInfo: 修改摄像机信息
               - 知识库查询: 获取操作指南/故障排查手册
            3. **观察 (Observation)**:分析工具返回的结果
            4. **重复**:直到获得足够信息回答用户
            ## 工具使用原则
            - 涉及摄像机实时状态、修改操作 → 必须调用工具
            - 询问操作指南、故障排查 → 使用知识库
            - 复杂任务需要多个步骤时,自动规划并依次调用工具
            ## 路由原则
            - 查询或修改摄像机:使用摄像机工具。
            - 查询人员、部门、组织架构:使用系统工具。
            - 查询说明书、制度、操作指南、故障排查:使用知识库工具。
            - 复合问题可以连续调用多个工具,再综合回答。
            ## 约束
            - 业务数据必须来自工具调用结果。
            - 修改类操作要在回答中明确修改对象和结果。
            - 无法确认的数据要直接说明,不要编造。
            ## 输出格式
            使用 Markdown 格式输出:
            - ## 标题
            - - 列表项
            - **加粗** 强调
            - 表格用于数据展示
            ## 重要提醒
            不要编造信息,所有数据必须来自工具调用或知识库。
            使用 Markdown,给出清晰结论和下一步建议。
            """;
    ModelCallLimitHook hook = ModelCallLimitHook.builder()
            .runLimit(5)  // 限制最多调用 5 次
            .exitBehavior(ModelCallLimitHook.ExitBehavior.ERROR)  // 超出限制时抛出异常
    private final ModelCallLimitHook modelCallLimitHook = ModelCallLimitHook.builder()
            .runLimit(8) // 最多调用8次LLM
            .exitBehavior(ModelCallLimitHook.ExitBehavior.ERROR)  // 超限时抛出异常
            .build();
    @Bean
    public ReactAgent cameraAgent() {
    @Bean("unifiedAgent")
    public ReactAgent unifiedAgent() {
        return buildAgent("unifiedAgent", UNIFIED_AGENT_PROMPT, monitorTools, systemTools, knowledgeTools);
    }
    private ReactAgent buildAgent(String name, String instruction, Object... tools) {
        return ReactAgent.builder()
                .name("cameraAgent") // Agent 的名称
                .model(chatModel)      // 设置底层的大语言模型
                .hooks(hook)
                .instruction(SYSTEM_PROMPT) // 系统提示词,定义角色和行为
                .tools(ToolCallbacks.from(monitorTools))       // 注册可用的工具
                .saver(new MemorySaver())      // 2. 核心:用 MemorySaver 实现记忆
                .enableLogging(true)   // 开启日志,方便调试观察思考过程
                .name(name)
                .model(chatModel)
                .hooks(modelCallLimitHook)
                .instruction(instruction)
                .tools(ToolCallbacks.from(tools))
                .saver(new MemorySaver())
                .enableLogging(true)
                .build();
    }
}
}