Skip to content

Commit aed1c5d

Browse files
authored
feat(example): add RoutingByToolCallsExample (#177)
1 parent 72ee62f commit aed1c5d

1 file changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.examples.advanced;
17+
18+
import io.agentscope.core.ReActAgent;
19+
import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter;
20+
import io.agentscope.core.memory.InMemoryMemory;
21+
import io.agentscope.core.message.Msg;
22+
import io.agentscope.core.message.MsgRole;
23+
import io.agentscope.core.message.TextBlock;
24+
import io.agentscope.core.model.DashScopeChatModel;
25+
import io.agentscope.core.model.GenerateOptions;
26+
import io.agentscope.core.tool.Tool;
27+
import io.agentscope.core.tool.Toolkit;
28+
import io.agentscope.examples.advanced.util.MsgUtils;
29+
30+
public class RoutingByToolCallsExample {
31+
32+
public static void main(String[] args) {
33+
Toolkit toolkit = new Toolkit();
34+
toolkit.registerTool(new SimpleTools());
35+
ReActAgent routerImplicit =
36+
ReActAgent.builder()
37+
.name("RouterImplicit")
38+
.sysPrompt(
39+
"You're a routing agent. Your target is to route the user query to"
40+
+ " the right follow-up task.")
41+
.model(
42+
DashScopeChatModel.builder()
43+
.apiKey(ExampleUtils.getDashScopeApiKey())
44+
.modelName("qwen-max")
45+
.stream(true)
46+
.enableThinking(true)
47+
.formatter(new DashScopeChatFormatter())
48+
.defaultOptions(
49+
GenerateOptions.builder()
50+
.thinkingBudget(512)
51+
.build())
52+
.build())
53+
.memory(new InMemoryMemory())
54+
.toolkit(toolkit)
55+
.build();
56+
57+
// Example of implicit routing with tool calls.
58+
Msg userMsg =
59+
Msg.builder()
60+
.role(MsgRole.USER)
61+
.content(
62+
TextBlock.builder()
63+
.text("Help me to generate a quick sort function in Python")
64+
.build())
65+
.build();
66+
try {
67+
Msg response = routerImplicit.call(userMsg).block();
68+
if (response != null) {
69+
System.out.println("Agent> " + MsgUtils.getTextContent(response) + "\n");
70+
} else {
71+
System.out.println("Agent> [No response]\n");
72+
}
73+
} catch (Exception e) {
74+
System.err.println("Error: " + e.getMessage());
75+
e.printStackTrace();
76+
}
77+
}
78+
79+
public static class SimpleTools {
80+
/**
81+
* Generate Python code based on the demand.
82+
*
83+
* @param demand The demand for the Python code.
84+
*/
85+
@Tool(
86+
name = "generate_Python_code",
87+
description = "Generate Python code based on the demand")
88+
public Msg generatePython(String demand) {
89+
System.out.println("I am PythonAgent,now generating Python code for demand: " + demand);
90+
String apiKey = ExampleUtils.getDashScopeApiKey();
91+
ReActAgent agent =
92+
ReActAgent.builder()
93+
.name("PythonAgent")
94+
.sysPrompt(
95+
"You're a Python expert, your target is to generate Python code"
96+
+ " based on the demand.")
97+
.model(
98+
DashScopeChatModel.builder()
99+
.apiKey(apiKey)
100+
.modelName("qwen-max")
101+
.stream(true)
102+
.enableThinking(true)
103+
.formatter(new DashScopeChatFormatter())
104+
.defaultOptions(
105+
GenerateOptions.builder()
106+
.thinkingBudget(1024)
107+
.build())
108+
.build())
109+
.memory(new InMemoryMemory())
110+
.toolkit(new Toolkit())
111+
.build();
112+
113+
Msg userMsg =
114+
Msg.builder()
115+
.role(MsgRole.USER)
116+
.content(TextBlock.builder().text(demand).build())
117+
.build();
118+
return agent.call(userMsg).block();
119+
}
120+
121+
/**
122+
* Generate a poem based on the demand.
123+
*
124+
* @param demand The demand for the poem.
125+
*/
126+
@Tool(name = "generate_poem", description = "Generate a poem based on the demand")
127+
public Msg generatePoem(String demand) {
128+
System.out.println("I am PoemAgent,now generating a poem for demand: " + demand);
129+
String apiKey = ExampleUtils.getDashScopeApiKey();
130+
ReActAgent agent =
131+
ReActAgent.builder()
132+
.name("PoemAgent")
133+
.sysPrompt(
134+
"You're a poet, your target is to generate poems based on the"
135+
+ " demand.")
136+
.model(
137+
DashScopeChatModel.builder()
138+
.apiKey(apiKey)
139+
.modelName("qwen-max")
140+
.stream(true)
141+
.enableThinking(true)
142+
.formatter(new DashScopeChatFormatter())
143+
.defaultOptions(
144+
GenerateOptions.builder()
145+
.thinkingBudget(1024)
146+
.build())
147+
.build())
148+
.memory(new InMemoryMemory())
149+
.toolkit(new Toolkit())
150+
.build();
151+
152+
Msg userMsg =
153+
Msg.builder()
154+
.role(MsgRole.USER)
155+
.content(TextBlock.builder().text(demand).build())
156+
.build();
157+
return agent.call(userMsg).block();
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)