新进行任务的规划,规划后有多个任务,然后分别完成任务
public class PlanExecuteDemo {
static Map<String, String> files = new HashMap<>();
@Tool(name = "write_file", description = "Write content to a file")
public Mono<String> write(@ToolParam(name = "name") String n,
@ToolParam(name = "content") String c) {
files.put(n, c);
return Mono.just("Saved " + n);
}
@Tool(name = "read_file", description = "Read content from a file")
public Mono<String> read(@ToolParam(name = "name") String n) {
return Mono.just(files.getOrDefault(n, "(not found)"));
}
@Tool(name = "calc", description = "Evaluate a simple math expression like 10*5")
public Mono<String> calc(@ToolParam(name = "expr") String e) {
String[] parts = e.split("\\*");
return Mono.just(e + " = " + (Integer.parseInt(parts[0].trim())
* Integer.parseInt(parts[1].trim())));
}
public static void main(String[] args) {
String apiKey = "sk-8980de1b287d490eb259d2f70eebeab9";
Toolkit toolkit = new Toolkit();
toolkit.registerTool(new PlanExecuteDemo());
PlanNotebook nb = PlanNotebook.builder()
.maxSubtasks(8)
.needUserConfirm(false) // demo 直接执行,不等 "yes"
.build();
nb.addChangeHook("log", (notebook, plan) -> {
if (plan != null) {
System.out.println("\n>>> Plan changed 111:\n" + plan.toMarkdown(false));
}
});
ReActAgent agent = ReActAgent.builder()
.name("Planner")
.sysPrompt("You break down complex tasks into a plan and execute step by step.")
.model(DashScopeChatModel.builder()
.apiKey(apiKey)
.modelName("qwen-plus")
.stream(true)
.formatter(new DashScopeChatFormatter()).build())
.toolkit(toolkit)
.memory(new InMemoryMemory())
.maxIters(50)
.planNotebook(nb)
.build();
Msg user = Msg.builder().role(MsgRole.USER).content(TextBlock.builder().text(
"Calculate 10*5, save the result to result.txt, then read it back to verify. " +
"This is a multi-step task, please plan first."
).build()).build();
Msg resp = agent.call(user).block();
System.out.println("\nFinal: " + resp.getTextContent());
}
}
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!
浙公网安备 33010602011771号