场景Spring AI 整合 Ollama 实现工具调用从入门到实战全解https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/161009153在上面调用tool的基础上实现工具链的使用示例。Spring AI 提供了强大的 Tool Calling 能力允许大模型在对话中自主决定调用哪些外部工具并自动组合它们的执行顺序。这种机制让智能体能够完成复杂的多步骤任务例如“查询北京天气并用英文回答”——模型会先调用天气工具获取数据再调用翻译工具输出英文。核心概念概念说明Tool Calling大模型根据用户问题生成函数调用请求含函数名和参数Spring AI 拦截后执行对应的 Java 方法将结果返回模型。Tool 注解标记在方法上Spring AI 自动解析方法签名和注释生成 JSON Schema 供模型理解。多工具注册通过defaultTools(...)同时注册多个工具模型可自主选择调用顺序和组合。链式调用模型在一次响应中先后调用多个工具前一个的输出作为后一个的输入形成“链”。注博客https://blog.csdn.net/badao_liumang_qizhi实现pom.xmlparent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.3.3/version !-- 降级为稳定版解决冲突 -- /parent groupIdcom.example/groupId artifactIdspring-ai-ollama-tool-chain/artifactId version1.0/version properties java.version17/java.version spring-ai.version1.1.2/spring-ai.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !-- Spring AI Ollama 核心 -- dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-model-ollama/artifactId version${spring-ai.version}/version /dependency /dependenciesapplication.yml​ server: port: 886 spring: ai: ollama: base-url: http://localhost:11434 chat: model: qwen2.5:7b-instruct options: temperature: 0.7 num-ctx: 4096 # 上下文窗口大小 logging: level: org.springframework.ai.chat.client: DEBUG # 查看工具调用详情 ​天气工具package com.badao.ai.tools; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; import org.springframework.stereotype.Component; Component public class WeatherTool { Tool(name get_weather, description 查询指定城市的实时天气) public String getWeather(ToolParam(description 城市名称) String city) { System.out.println(调用了天气工具); // 模拟天气数据 return String.format(%s当前天气晴温度22℃湿度45%%。, city); } }翻译工具package com.badao.ai.tools; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; import org.springframework.stereotype.Component; Component public class TranslateTool { Tool(name translate_to_english, description 将中文文本翻译成英文) public String translate(ToolParam(description 待翻译的中文文本) String text) { System.out.println(调用了翻译工具); // 模拟翻译实际可接入翻译API return Translated: text (This is the English version.); } }工具注册配置类package com.badao.ai.config; import com.badao.ai.tools.WeatherTool; import com.badao.ai.tools.TranslateTool; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.model.ChatModel; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class ToolConfig { Bean public ChatClient chatClient(ChatModel chatModel, WeatherTool weatherTool, TranslateTool translateTool) { return ChatClient.builder(chatModel) .defaultTools(weatherTool, translateTool) // 注册天气和翻译工具 .build(); } }Agent 服务层package com.badao.ai.service; import org.springframework.ai.chat.client.ChatClient; import org.springframework.stereotype.Service; Service public class AgentService { private final ChatClient chatClient; public AgentService(ChatClient chatClient) { this.chatClient chatClient; } public String ask(String question) { return chatClient.prompt() .user(question 请先用天气工具再用翻译工具) .call() .content(); } }控制器package com.badao.ai.controller; import com.badao.ai.service.AgentService; import org.springframework.web.bind.annotation.*; RestController RequestMapping(/api) public class AgentController { private final AgentService agentService; public AgentController(AgentService agentService) { this.agentService agentService; } PostMapping(/agent) public String ask(RequestBody String question) { return agentService.ask(question); } }测试测试单工具测试工具链