AI项目博客
记录 AI 工程实践与大模型应用

Spring AI Alibaba 55|智能体作为工具 · 工具调用(Tool Calling):控制子Agent的输出

塑造主Agent从子Agent接收的内容的常见策略:

  • 修改提示词——优化子Agent的提示以指定应返回的确切内容。

    当输出不完整、过于冗长或缺少关键细节时很有用。
    常见的失败模式是子Agent执行工具调用或推理但不在最终消息中包含结果。提醒它控制器(和用户)只看到最终输出,因此必须在那里包含所有相关信息。

  • 自定义输出格式——使用 outputSchema 或 outputType 定义结构化输出格式。

使用 outputSchema

使用 BeanOutputConverter 生成输出 Schema,提供类型安全和自动 schema 生成:

import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import com.alibaba.cloud.ai.graph.agent.AgentTool;
import org.springframework.ai.converter.BeanOutputConverter;

// 定义输出类型
public static class ArticleOutput {
  private String title;
  private String content;
  private int characterCount;

  // Getters and Setters
  public String getTitle() { return title; }
  public void setTitle(String title) { this.title = title; }
  public String getContent() { return content; }
  public void setContent(String content) { this.content = content; }
  public int getCharacterCount() { return characterCount; }
  public void setCharacterCount(int characterCount) { this.characterCount = characterCount; }
}

// 使用 BeanOutputConverter 生成 outputSchema
BeanOutputConverter<ArticleOutput> outputConverter = new BeanOutputConverter<>(ArticleOutput.class);
String format = outputConverter.getFormat();

ReactAgent writerAgent = ReactAgent.builder()
  .name("writer_with_output_schema")
  .model(chatModel)
  .description("写文章并返回结构化输出")
  .instruction("你是一个专业作家。请创作文章并严格按照指定的JSON格式返回结果。")
  .outputSchema(format) 
  .build();

ReactAgent coordinatorAgent = ReactAgent.builder()
  .name("coordinator_output_schema")
  .model(chatModel)
  .instruction("调用写作工具完成用户请求,工具会返回结构化的文章数据。")
  .tools(AgentTool.getFunctionToolCallback(writerAgent))
  .build();

Optional<OverAllState> result = coordinatorAgent.invoke("写一篇关于冬天的短文");

使用 outputType

使用 Java 类型定义输出,框架会自动生成输出 schema:

import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import com.alibaba.cloud.ai.graph.agent.AgentTool;
import org.springframework.ai.converter.BeanOutputConverter;

// 定义输出类型
public class ArticleOutput {
  private String title;
  private String content;
  private int characterCount;

  // getters and setters
}

ReactAgent writerAgent = ReactAgent.builder()
  .name("writer_with_output_type")
  .model(chatModel)
  .description("写文章并返回类型化输出")
  .instruction("你是一个专业作家。请创作文章并返回包含 title、content 和 characterCount 的结构化结果。")
  .outputType(ArticleOutput.class) 
  .build();

ReactAgent coordinatorAgent = ReactAgent.builder()
  .name("coordinator_output_type")
  .model(chatModel)
  .instruction("调用写作工具完成用户请求。")
  .tools(AgentTool.getFunctionToolCallback(writerAgent))
  .build();

Optional<OverAllState> result = coordinatorAgent.invoke("写一篇关于夏天的小诗");

同时使用 inputType 和 outputType 进行完整的类型化Agent工具调用:

import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import com.alibaba.cloud.ai.graph.agent.AgentTool;

// 定义输入和输出类型
public record ArticleRequest(String topic, int wordCount, String style) {}

public class ArticleOutput {
  private String title;
  private String content;
  private int characterCount;
  // getters and setters
}

public class ReviewOutput {
  private String comment;
  private boolean approved;
  private List<String> suggestions;
  // getters and setters
}

// 创建完整类型化的Agent
ReactAgent writerAgent = ReactAgent.builder()
  .name("full_typed_writer")
  .model(chatModel)
  .description("完整类型化的写作工具")
  .instruction("根据结构化输入(topic、wordCount、style)创作文章,并返回结构化输出(title、content、characterCount)。")
  .inputType(ArticleRequest.class) 
  .outputType(ArticleOutput.class) 
  .build();

ReactAgent reviewerAgent = ReactAgent.builder()
  .name("typed_reviewer")
  .model(chatModel)
  .description("完整类型化的评审工具")
  .instruction("对文章进行评审,返回评审意见(comment、approved、suggestions)。")
  .outputType(ReviewOutput.class) 
  .build();

ReactAgent orchestratorAgent = ReactAgent.builder()
  .name("orchestrator")
  .model(chatModel)
  .instruction("协调写作和评审流程。先调用写作工具创作文章,然后调用评审工具进行评审。")
  .tools(
      AgentTool.getFunctionToolCallback(writerAgent),
      AgentTool.getFunctionToolCallback(reviewerAgent)
  )
  .build();

Optional<OverAllState> result = orchestratorAgent.invoke("请写一篇关于友谊的散文,约200字,需要评审");

在实际应用中,主Agent通常需要访问多个不同的子Agent工具,根据任务需求选择合适的工具进行调用。这种模式允许你构建更灵活、更强大的多Agent系统。

import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import com.alibaba.cloud.ai.graph.agent.AgentTool;

// 创建写作Agent
ReactAgent writerAgent = ReactAgent.builder()
  .name("writer_agent")
  .model(chatModel)
  .description("专门负责创作文章和内容生成")
  .instruction("你是一个专业作家,擅长各类文章创作。")
  .build();

// 创建翻译Agent
ReactAgent translatorAgent = ReactAgent.builder()
  .name("translator_agent")
  .model(chatModel)
  .description("专门负责文本翻译工作")
  .instruction("你是一个专业翻译,能够准确翻译多种语言。")
  .build();

// 创建总结Agent
ReactAgent summarizerAgent = ReactAgent.builder()
  .name("summarizer_agent")
  .model(chatModel)
  .description("专门负责内容总结和提炼")
  .instruction("你是一个内容总结专家,擅长提炼关键信息。")
  .build();

// 创建主Agent,集成多个工具
ReactAgent multiToolAgent = ReactAgent.builder()
  .name("multi_tool_coordinator")
  .model(chatModel)
  .instruction("你可以访问多个专业工具:写作、翻译和总结。" +
          "根据用户需求选择合适的工具来完成任务。")
  .tools(
      AgentTool.getFunctionToolCallback(writerAgent),      
      AgentTool.getFunctionToolCallback(translatorAgent),  
      AgentTool.getFunctionToolCallback(summarizerAgent)   
  )
  .build();

// 使用 - 主Agent会根据需求自动选择合适的工具
Optional<OverAllState> result = multiToolAgent.invoke(
  "请写一篇关于AI的文章,然后翻译成英文,最后给出摘要");

在这种模式中:

  1. 专业化分工:每个子Agent专注于特定领域(写作、翻译、总结等)
  2. 灵活组合:主Agent可以根据任务需求调用一个或多个工具
  3. 智能路由:主Agent根据工具的描述和用户需求,自动选择合适的工具
  4. 顺序执行:主Agent可以按顺序调用多个工具,实现复杂的工作流

提示:为每个子Agent提供清晰、准确的 description 非常重要,这直接影响主Agent如何选择合适的工具。描述应该简洁地说明Agent的职责和能力。

本文整理自 Spring AI Alibaba 官方文档《智能体作为工具》:https://java2ai.com/docs/frameworks/agent-framework/advanced/agent-tool

赞(0)
未经允许不得转载:AI项目博客 » Spring AI Alibaba 55|智能体作为工具 · 工具调用(Tool Calling):控制子Agent的输出

评论 抢沙发

AI项目博客

专注 AI 工程化落地 —— 覆盖智能体、大模型、Java AI 与 Python 的学习笔记,以及 AI 平台的项目实战记录。

关于本站文章归档

登录

找回密码

注册