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(); } }