文章目录Spring AI Alibaba 工具调用实战Tool Calling版本一、依赖引入二、yml 配置三、代码案例声明式工具Tool1. 日期工具2. 天气工具普通业务方法 Tool四、注册到 ChatClientdefaultTools五、Controller 调用1. 查当前时间2. 查天气六、调用流程Spring AI Alibaba 工具调用实战Tool Calling让大模型在对话中调用你写好的 Java 方法查时间、查天气等。版本Spring Boot3.4.5spring-ai-alibaba-starter-dashscope1.1.2.1Java17Spring-ai更新速度快,请以官方为准可参考网址:Spring-ai官网Spring-ai阿里巴巴阿里百炼模型广场开发者指南(API/SDK)一、依赖引入dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdcom.alibaba.cloud.ai/groupIdartifactIdspring-ai-alibaba-starter-dashscope/artifactIdversion1.1.2.1/version/dependency!-- dashscope-sdk 可能带旧版 jsonschema-generator与 spring-ai-model 冲突时需对齐 --dependencyManagementdependenciesdependencygroupIdcom.github.victools/groupIdartifactIdjsonschema-generator/artifactIdversion4.38.0/version/dependency/dependencies/dependencyManagementdependencygroupIdcom.alibaba/groupIdartifactIddashscope-sdk-java/artifactIdversion2.22.18/versionexclusions!-- 避免与 Boot 自带的 logback 双绑 SLF4J --exclusiongroupIdorg.slf4j/groupIdartifactIdslf4j-simple/artifactId/exclusion/exclusions/dependency二、yml 配置spring:application:name:spring-tool-demoai:dashscope:api-key:${DASHSCOPE_API_KEY}chat:options:# 需支持 Tool Calling 的对话模型model:qwen-maxtemperature:0.7三、代码案例声明式工具Tool1. 日期工具Component// 交给 Spring 管理便于注入到 ChatClientpublicclassDateTool{/** * description 很重要模型靠它判断「什么时候该调这个工具」。 * 工具名默认是方法名 getCurrentDateTime。 */Tool(descriptionGet the current date and time in the users timezone)publicStringgetCurrentDateTime(){// 返回给模型的真实数据模型再组织成自然语言回复用户returnDateFormatUtil.now();// 例如 yyyy-MM-dd HH:mm:ss}}2. 天气工具普通业务方法 ToolComponentpublicclassWeatherTool{/** * 查询指定城市天气。 * district 建议用拼音如 beijing / shanghai方便模型稳定传参。 */Tool(description查询指定城市的天气情况)publicStringgetWeather(Stringdistrict){// 这里用 switch 模拟业务真实项目可调第三方天气 APIreturnswitch(district){casebeijing-天气清凉;caseshanghai-天气炎热;caseguangzhou-天气闷热;default-未知地区;};}}四、注册到 ChatClientdefaultToolsConfigurationpublicclassClientConfig{/** * 构建带默认工具的 ChatClient。 * defaultTools该 Client 每次对话都可用这些工具。 */Bean(nametoolClient)publicChatClienttoolClient(DashScopeChatModelchatModel,DateTooldateTool,WeatherToolweatherTool){returnChatClient.builder(chatModel)// 传入带 Tool 方法的对象实例即可框架会扫注解并生成 Schema.defaultTools(dateTool,weatherTool).build();}}注意若已在defaultTools注册请求里不要再写.tools(new DateTool())否则会报Multiple tools with the same name (getCurrentDateTime) founddefaultTools与单次.tools()二选一或确保工具名不重复。五、Controller 调用1. 查当前时间RestControllerpublicclassTestController{Resource(nametoolClient)privateChatClienttoolClient;/** * GET /get/currenttime * 用户说「要当前时间」→ 模型决定调用 getCurrentDateTime → 把结果组织成回答 */GetMapping(/get/currenttime)publicStringgetCurrentTime(){returntoolClient.prompt().system(You are a helpful assistant.).user(Get the current date and time in the users timezone)// 不要再 .tools(...)工具已在 defaultTools 里.call().content();}}2. 查天气RestControllerpublicclassWeatherController{Resource(nametoolClient)privateChatClienttoolClient;/** * GET /get/weather?district上海 * system 提示把中文城市转成拼音参数和 WeatherTool 的 case 对齐 */GetMapping(/get/weather)publicStringgetWeather(RequestParamStringdistrict){returntoolClient.prompt().system(你可以通过工具获取天气情况,中文请转换成拼音作为调用工具的参数,例如上海对应shanghai).user(查一下district的天气).call().content();}}六、调用流程