agentscope-java
agentscope-java copied to clipboard
[Questions] Agent as Tool not work
Hello,
I have created a Master Agent and registered several Sub-Agents following the documentation. I intended for the Master Agent to successfully invoke the Sub-Agents during execution. However, after testing, the Sub-Agents failed to be invoked.
I am not sure if there is an error in my code implementation or if version 1.0.3 is outdated. If the issue lies with my code, could you please provide a usage example? Below is the code I wrote; please review and correct it. Thank you!
package com.example.agentscope.test.mutiAgent;
import io.agentscope.core.ReActAgent;
import io.agentscope.core.message.Msg;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.message.TextBlock;
import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.tool.Tool;
import io.agentscope.core.tool.ToolParam;
import io.agentscope.core.tool.Toolkit;
/**
* 大模型接口连通性测试
*/
public class MutiAgentDemo {
public static void main(String[] args) {
String apiKey = "xxx";
DashScopeChatModel model = DashScopeChatModel.builder()
.apiKey(apiKey)
.modelName("qwen3-max")
.build();
// 创建子智能体的 Provider(工厂)
// 注意:必须使用 lambda 表达式,确保每次调用创建新实例
Toolkit mainAgentToolkit = new Toolkit();
mainAgentToolkit.registration()
.subAgent(() -> createCalculatorAgent(model))
.subAgent(() -> createWeatherAgent(model))
.apply();
// 创建主智能体,配置工具
ReActAgent mainAgent = ReActAgent.builder()
.name("Coordinator")
.sysPrompt("你是一个协调员。当用户咨询问题时,你需要调用 CalculatorAgent或WeatherAgent两个智能体工具解决问题。")
.model(model)
.toolkit(mainAgentToolkit)
.build();
// 调用智能体
Msg msg = Msg.builder()
.role(MsgRole.USER)
.content(TextBlock.builder()
.text("杭州今天的天气怎么样")
.build())
.build();
Msg response = mainAgent.call(msg).block();
System.out.println(response.getTextContent());
}
private static ReActAgent createCalculatorAgent(DashScopeChatModel model) {
Toolkit toolkit = new Toolkit();
toolkit.registerTool(new WeatherService());
return ReActAgent.builder()
.name("CalculatorAgent")
.sysPrompt("你是一个计算专家,负责回答两数相加以及两数相乘的问题。")
.model(model)
.build();
}
private static ReActAgent createWeatherAgent(DashScopeChatModel model) {
Toolkit toolkit = new Toolkit();
toolkit.registerTool(new Calculator());
return ReActAgent.builder()
.name("WeatherAgent")
.sysPrompt("你是一个天气预报专家,负责回答天气相关问题。")
.model(model)
.build();
}
}
/**
* 天气服务工具
*/
class WeatherService {
@Tool(description = "获取城市的当前天气")
public String getWeather(
@ToolParam(name = "city", description = "城市名称")
String city) {
System.out.println("tool invoke: 获取城市的当前天气");
// 模拟 API 调用
return String.format("%s 的天气:晴天,25°C", city);
}
@Tool(description = "获取未来 N 天的天气预报")
public String getForecast(
@ToolParam(name = "city", description = "城市名称")
String city,
@ToolParam(name = "days", description = "天数")
int days) {
System.out.println("tool invoke: 获取未来 N 天的天气预报");
return String.format("%s 的 %d 天预报:大部分晴天", city, days);
}
}
/**
* 计算器工具
*/
class Calculator {
@Tool(description = "两数相加")
public double add(
@ToolParam(name = "a", description = "第一个数") double a,
@ToolParam(name = "b", description = "第二个数") double b) {
System.out.println("tool invoke: 两数相加");
return a + b;
}
@Tool(description = "两数相乘")
public double multiply(
@ToolParam(name = "a", description = "第一个数") double a,
@ToolParam(name = "b", description = "第二个数") double b) {
System.out.println("tool invoke: 两数相乘");
return a * b;
}
}
The modified code is as follows:
public class BasicChatExample {
public static void main(String[] args) throws Exception {
// Get API key (from environment or interactive input)
String apiKey = ExampleUtils.getDashScopeApiKey();
DashScopeChatModel model = DashScopeChatModel.builder()
.apiKey(apiKey)
.modelName("qwen3-max")
.build();
// 创建子智能体的 Provider(工厂)
// 注意:必须使用 lambda 表达式,确保每次调用创建新实例
Toolkit mainAgentToolkit = new Toolkit();
mainAgentToolkit.registration()
.subAgent(() -> createCalculatorAgent(model))
.apply();
mainAgentToolkit.registration()
.subAgent(() -> createWeatherAgent(model))
.apply();
// 创建主智能体,配置工具
ReActAgent mainAgent = ReActAgent.builder()
.name("Coordinator")
.sysPrompt("你是一个协调员。当用户咨询问题时,你需要调用 CalculatorAgent或WeatherAgent两个智能体工具解决问题。")
.model(model)
.toolkit(mainAgentToolkit)
.build();
// 调用智能体
Msg msg = Msg.builder()
.role(MsgRole.USER)
.content(TextBlock.builder()
.text("杭州今天的天气怎么样")
.build())
.build();
Msg response = mainAgent.call(msg).block();
System.out.println(response.getTextContent());
}
private static ReActAgent createCalculatorAgent(DashScopeChatModel model) {
Toolkit toolkit = new Toolkit();
toolkit.registerTool(new Calculator());
return ReActAgent.builder()
.name("CalculatorAgent")
.sysPrompt("你是一个计算专家,负责回答两数相加以及两数相乘的问题。")
.description("你是一个计算专家,负责回答两数相加以及两数相乘的问题。")
.toolkit(toolkit)
.model(model)
.build();
}
private static ReActAgent createWeatherAgent(DashScopeChatModel model) {
Toolkit toolkit = new Toolkit();
toolkit.registerTool(new WeatherService());
return ReActAgent.builder()
.name("WeatherAgent")
.sysPrompt("你是一个天气预报专家,负责回答天气相关问题。")
.description("你是一个天气预报专家,负责回答天气相关问题。")
.toolkit(toolkit)
.model(model)
.build();
}
static class WeatherService {
@Tool(description = "获取城市的当前天气")
public String getWeather(
@ToolParam(name = "city", description = "城市名称")
String city) {
System.out.println("tool invoke: 获取城市的当前天气");
// 模拟 API 调用
return String.format("%s 的天气:晴天,25°C", city);
}
@Tool(description = "获取未来 N 天的天气预报")
public String getForecast(
@ToolParam(name = "city", description = "城市名称")
String city,
@ToolParam(name = "days", description = "天数")
int days) {
System.out.println("tool invoke: 获取未来 N 天的天气预报");
return String.format("%s 的 %d 天预报:大部分晴天", city, days);
}
}
/**
* 计算器工具
*/
static class Calculator {
@Tool(description = "两数相加")
public double add(
@ToolParam(name = "a", description = "第一个数") double a,
@ToolParam(name = "b", description = "第二个数") double b) {
System.out.println("tool invoke: 两数相加");
return a + b;
}
@Tool(description = "两数相乘")
public double multiply(
@ToolParam(name = "a", description = "第一个数") double a,
@ToolParam(name = "b", description = "第二个数") double b) {
System.out.println("tool invoke: 两数相乘");
return a * b;
}
}
}
There are several issues with the original code:
- Toolkit Registration: The toolkit was only registered once, but since there are two Agents, registration needs to be performed twice (separately for each).
- Missing Description: You need to specify an
agent descriptioninsidecreateCalculatorAgent. This is required for the parent Agent to understand the child Agent's capabilities. - Missing Registration in Function: The toolkit inside
createCalculatorAgentwas not registered to theReActAgent. - Incorrect Tools: The tools assigned to the toolkit in
createCalculatorAgentare incorrect (weather tools were used by mistake). - Similar Issues: The
createWeatherAgentfunction suffers from similar issues ascreateCalculatorAgent.
可以了,非常感谢~