‹ 博客列表

Spring AI Alibaba 16|Tools 工具 · 在 ReactAgent 中使用工具:基础使用示例

在 ReactAgent 中使用工具非常简单: import com.alibaba.cloud.ai.graph.agent.ReactAgent; import org.springframework.ai.tool.ToolCallb

在 ReactAgent 中使用工具非常简单:

import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.function.FunctionToolCallback;

// 创建工具
ToolCallback weatherTool = FunctionToolCallback
  .builder("get_weather", new WeatherFunction())
  .description("Get weather for a given city")
  .inputType(String.class)
  .build();

ToolCallback searchTool = FunctionToolCallback
  .builder("search", new SearchFunction())
  .description("Search for information")
  .inputType(String.class)
  .build();

// 创建带有工具的 Agent
ReactAgent agent = ReactAgent.builder()
  .name("my_agent")
  .model(chatModel)
  .tools(weatherTool, searchTool)
  .systemPrompt("You are a helpful assistant with access to weather and search tools.")
  .saver(new MemorySaver())
  .build();

// 使用 Agent
AssistantMessage response = agent.call("What's the weather like in San Francisco?");
System.out.println(response.getText());

本文整理自 Spring AI Alibaba 官方文档《Tools 工具》:https://java2ai.com/docs/frameworks/agent-framework/tutorials/tools

本文来自 https://aimock.cn/blog,站内重新编排。
内容仅供学习交流,转载请注明出处。