liusuyi
2026-05-18 8a7b666f0bed7c1dc03767166c7ecd602552a5c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.ard.agent.config;
 
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.model.ChatModel;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class AgentConfig {
 
    @Resource(name = "ollamaChatModel")
    private ChatModel chatModel;
    @Resource
    private MonitorTools monitorTools;
    @Resource
    private SystemTools systemTools;
    @Resource
    private KnowledgeTools knowledgeTools;
 
    private static final String UNIFIED_AGENT_PROMPT = """
            你是 ARD 智能业务助手,能使用摄像机管理、系统管理和知识库工具。
 
            ## 路由原则
            - 查询或修改摄像机:使用摄像机工具。
            - 查询人员、部门、组织架构:使用系统工具。
            - 查询说明书、制度、操作指南、故障排查:使用知识库工具。
            - 复合问题可以连续调用多个工具,再综合回答。
 
            ## 约束
            - 业务数据必须来自工具调用结果。
            - 修改类操作要在回答中明确修改对象和结果。
            - 无法确认的数据要直接说明,不要编造。
 
            ## 输出格式
            使用 Markdown,给出清晰结论和下一步建议。
            """;
 
    private final ModelCallLimitHook modelCallLimitHook = ModelCallLimitHook.builder()
            .runLimit(8) // 最多调用8次LLM
            .exitBehavior(ModelCallLimitHook.ExitBehavior.ERROR)  // 超限时抛出异常
            .build();
 
    @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(name)
                .model(chatModel)
                .hooks(modelCallLimitHook)
                .instruction(instruction)
                .tools(ToolCallbacks.from(tools))
                .saver(new MemorySaver())
                .enableLogging(true)
                .build();
    }
}