当前位置: 首页 > news >正文

Spring AI 2.x 全面指南:架构升级、高效的工具调用、多模型生态与实战示例

让 AI 应用开发回归本质:统一接口,自由切换,一次编码,驱动全球大模型。


目录

Spring AI 最新版本概览

⚙️ 技术栈要求

✨ 主要更新亮点

Spring AI 2.x 有哪些重大升级?

1. 底层架构全面升级:拥抱 Java 21

2. ️ Redis “史诗级”增强:从缓存到智能记忆引擎

3. 模型能力与工具调用全面进化

4. ️ API 设计规范与空安全

5. ⚠️ 破坏性变更(迁移注意)

实战示例:Spring AI 2.x + DeepSeek + Tool + Advisor

1. Maven 依赖配置 (pom.xml)

2. 工具调用示例(@Tool 替代 @Function)

定义工具类

Tool注册

Service 调用模型

Controller 使用

3. Advisor 机制详解

✅ 核心价值

内置 Advisor 类型

自定义 Advisor 示例

使用建议

4. 配置文件 (application.yml)

5. 迁移注意事项

Spring AI 2.x 中工具方法的识别机制

识别流程四步走

示例:JSON Schema 生成

Spring AI 对 Function Calling 的支持演进

版本组合建议

Spring AI 2.x:构建多模型智能应用的终极框架

一、国际主流商业大模型

二、国产大模型(重点支持)

️ 三、开源与本地部署模型

四、多模态与专用能力

选型建议

⚙️ 开发体验:一行配置,无缝切换

✅ 总结



Spring AI 最新版本概览

截至 2026 年 2 月,Spring AI 的最新里程碑版本为:

2.0.0-M2(发布于 2026 年 1 月)

⚙️ 技术栈要求

  • Java 21(强制要求,不再支持 Java 17)
  • Spring Boot 4.0.1
  • Spring Framework 7.0

重要提示:由于底层架构重构,Spring AI 2.x 与 Spring Boot 3.x 不兼容,不可混合使用。

✨ 主要更新亮点

  • API 空安全:引入 JSpecify 规范,提升 API 健壮性。
  • 存储扩展:新增对 Amazon S3Infinispan 等向量存储的支持。
  • 缓存增强:支持基于 Redis 的语义缓存,显著降低重复查询成本。
  • 模型能力升级
    • Mistral AI 与 Ollama 支持 结构化输出(JSON Schema)
    • 工具调用(Tool Calling)机制全面增强

生产建议:当前仍推荐使用 1.1.0 GA 版本,待 2.0 正式版(GA)发布后再迁移。


Spring AI 2.x 有哪些重大升级?

Spring AI 2.x 不是渐进式迭代,而是一次面向 AI 原生时代的架构重构,旨在解决企业级 AI 应用的性能、成本与复杂度问题。

1. 底层架构全面升级:拥抱 Java 21

项目说明
最低 JDKJava 21(虚拟线程、Pattern Matching 等特性可用)
生态基座Spring Boot 4.0 + Spring Framework 7.0
性能红利利用 虚拟线程(Virtual Threads) 提升高并发 LLM 调用效率;支持 AOT 编译 加速启动

2. ️ Redis “史诗级”增强:从缓存到智能记忆引擎

  • Redis Chat Memory:跨会话上下文持久化,支持全文检索。
  • Redis Vector Store
    • 新增 文本搜索 + 范围查询
    • 暴露 HNSW 索引参数(如 M, efConstruction),可精细调优召回率 vs 延迟
  • 语义缓存顾问:自动识别语义相似请求,复用结果,节省 API 调用成本

✅ Redis 在 Spring AI 2.x 中已可部分替代专用向量数据库。

3. 模型能力与工具调用全面进化

厂商升级亮点
OpenAI集成官方 Java SDK;默认模型升级为 gpt-5-mini
Anthropic Claude支持 4.5 版本:
• Citations API(回答可溯源)
• Files API(生成可下载文件)
Google Gemini新增 ThinkingConfig,动态调节“思考深度”
Mistral / Ollama原生支持 结构化 JSON 输出,确保类型安全

工具调用增强

  • 支持 运行时动态修改工具 Schema
  • 新增 conversationHistoryEnabled 选项,精细控制上下文逻辑

4. ️ API 设计规范与空安全

  • 全面采用 JSpecify 空安全规范
  • 编译期即可检测空指针风险
  • Kotlin 开发者受益:获得真正的可空/非空类型支持

5. ⚠️ 破坏性变更(迁移注意)

变更项说明
temperature 配置移除默认值,必须显式配置,否则报错
默认模型OpenAI 从 gpt-4gpt-5-mini,行为可能变化
注解体系@Function@Tool(见下文)

实战示例:Spring AI 2.x + DeepSeek + Tool + Advisor

示例仓库:
https://github.com/SuniaW/ai-learn.git

1. Maven 依赖配置 (pom.xml)


4.0.0org.springframework.bootspring-boot-starter-parent4.0.0com.wx.ai.learnai-learn0.0.1-SNAPSHOTai-learn212.0.0-M2org.springframework.bootspring-boot-starter-weborg.springframework.aispring-ai-starter-model-openaiorg.springframework.aispring-ai-bom${spring-ai.version}pomimportspring-milestonesSpring Milestoneshttps://repo.spring.io/milestonefalse

2. 工具调用示例(@Tool 替代 @Function

定义工具类
/** Copyright (c) 2026 the original author or authors. All rights reserved.** @author wangxu* @since 2026*/
package com.wx.ai.learn.tool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;
import java.util.Random;
/*** @author wangxu* @version 1.0* @date 2026/2/12* @description 天气工具类*/
@Component
public class WeatherTool {private static final Logger LOGGER = LoggerFactory.getLogger(WeatherTool.class);final int[] temperatures = {-125, 15, -255};private final Random random = new Random();/*** 获取天气信息** @param location 位置* @return 答案*/@Tool(description = "Get the current weather for a given location")public String weather(String location) {LOGGER.info("WeatherTool called with location: {}", location);int temperature = temperatures[random.nextInt(temperatures.length)];System.out.println(">>> Tool Call responseTemp: " + temperature);return "The current weather in " + location + " is sunny with a temperature of " + temperature + "°C.";}
}
Tool注册
/** Copyright (c) 2026 the original author or authors. All rights reserved.** @author wangxu* @since 2026*/
package com.wx.ai.learn.config;
import com.wx.ai.learn.advisor.MyLoggingAdvisor;
import com.wx.ai.learn.advisor.SelfRefineEvaluationAdvisor;
import com.wx.ai.learn.tool.WeatherTool;
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;
import org.springframework.context.annotation.Primary;
@Configuration
public class AiConfig {@Bean@Primary  // 设置为默认,不加 Qualifier 时优先选这个public ChatClient chatClient(ChatClient.Builder builder) {return builder.defaultSystem("You are a helpful assistant.").defaultAdvisors().build();}@Beanpublic ChatClient weatherChatClient(ChatClient.Builder builder, // 注入 Spring Boot 自动配置的 Builder 基础实例ChatModel chatModel) {// 在此处集中配置通用的工具、顾问和模型return builder.defaultSystem("你是一个专业的气象助手。").defaultTools(new WeatherTool()) // 注册工具类.defaultAdvisors(// 1. 自我修正评估顾问(使用本地 Ollama 模型进行评估)SelfRefineEvaluationAdvisor.builder().chatClientBuilder(ChatClient.builder(chatModel)).maxRepeatAttempts(15).successRating(4).order(0).build(),// 2. 自定义日志顾问new MyLoggingAdvisor(2)).build();}
}
Service 调用模型
/** Copyright (c) 2026 the original author or authors. All rights reserved.** @author wangxu* @since 2026*/
package com.wx.ai.learn.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class WeatherService {private static final Logger LOGGER = LoggerFactory.getLogger(WeatherService.class);private final ChatClient genericClient;private final ChatClient weatherClient;// 通过 @Qualifier 指定 Bean 的名称public WeatherService(@Qualifier("chatClient") ChatClient genericClient,@Qualifier("weatherChatClient") ChatClient weatherClient) {this.genericClient = genericClient;this.weatherClient = weatherClient;}public String doWork(String city) {//没有使用工具String common = genericClient.prompt("你好").call().content();//使用工具String weather = this.weatherClient.prompt("请查询 " + city + " 的天气,并友好地回复用户。").call().content();LOGGER.info("common response: {}", common);LOGGER.info("weather response: {}", weather);return common + weather;}
}
Controller 使用
/** Copyright (c) 2026 the original author or authors. All rights reserved.** @author wangxu* @since 2026*/
package com.wx.ai.learn.cotroller;
import com.wx.ai.learn.service.AiService;
import com.wx.ai.learn.service.WeatherService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/ai")
public class AiController {private static final Logger LOGGER = LoggerFactory.getLogger(AiController.class);@Resourceprivate AiService aiService;@Resourceprivate WeatherService weatherService;private final OpenAiChatModel chatModel;@Autowiredpublic AiController(OpenAiChatModel chatModel) {this.chatModel = chatModel;}@GetMapping("/weather")public String weather(@RequestParam(value = "city", defaultValue = "Paris") String city) {return weatherService.doWork(city);}@GetMapping(value = "/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {return chatModel.stream(new Prompt(new UserMessage(message)));}
}

3. Advisor 机制详解

Advisor(顾问) 是 Spring AI 的核心增强能力,借鉴 AOP 思想,专为 LLM 交互设计。

✅ 核心价值
  • 无侵入性:业务代码无需感知
  • 横切关注点解耦:日志、记忆、RAG、安全等
  • 责任链模式:可插拔、可排序
内置 Advisor 类型

表格

类型说明
MessageChatMemoryAdvisor保留完整消息结构,适合多轮对话
VectorStoreChatMemoryAdvisor超长历史对话支持(突破 token 限制)
QuestionAnswerAdvisor自动 RAG 检索增强
SimpleLoggerAdvisor请求/响应日志记录
SelfRefineEvaluationAdvisorLLM-as-a-Judge 闭环评估
自定义 Advisor 示例
/** Copyright (c) 2026 the original author or authors. All rights reserved.** @author wangxu* @since 2026*/
package com.wx.ai.learn.advisor;
import org.springframework.ai.chat.client.ChatClientRequest;
import org.springframework.ai.chat.client.ChatClientResponse;
import org.springframework.ai.chat.client.advisor.api.AdvisorChain;
import org.springframework.ai.chat.client.advisor.api.BaseAdvisor;
import org.springframework.ai.model.ModelOptionsUtils;
public class MyLoggingAdvisor implements BaseAdvisor {private final int order;public MyLoggingAdvisor(int order) {this.order = order;}@Overridepublic int getOrder() {return this.order;}@Overridepublic ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChain advisorChain) {print("REQUEST", chatClientRequest.prompt().getInstructions());return chatClientRequest;}@Overridepublic ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorChain advisorChain) {print("RESPONSE", chatClientResponse.chatResponse().getResults());return chatClientResponse;}private void print(String label, Object object) {System.out.println(label + ":" + ModelOptionsUtils.toJsonString(object) + "\n");}
}
使用建议
  • 合理设置 order:安全校验优先,日志最后
  • 避免过度堆叠(≤5 个)
  • 流式响应中慎用阻塞操作
  • 生产环境使用 Redis/DB 替代 InMemoryChatMemory

4. 配置文件 (application.yml)

spring:application:name: ai-learnai:openai:api-key: ${OPENAI_API_KEY}base-url: https://api.deepseek.com  #  关键!代理地址chat:options:model: deepseek-chattemperature: 0.7

5. 迁移注意事项

旧版 (1.x)新版 (2.x)
@Function@Tool
FunctionCallback→ 废弃,直接使用注解方法
配置前缀统一至 spring.ai.*

Spring AI 2.x 中工具方法的识别机制

Spring AI 2.x 采用 “声明 + 自动发现” 模式,实现零注册、开箱即用的工具集成。

识别流程四步走

  1. 声明身份
    使用 @Tool(description = "...") 标记方法,定义其名称、参数、用途。

  2. 纳入容器
    工具类需标注 @Component / @Service,成为 Spring Bean。

  3. 自动注册
    ChatClient 初始化时,扫描所有 Bean,提取 @Tool 方法并转换为 JSON Schema

  4. 运行时调用
    LLM 返回工具调用指令 → Spring AI 解析 → 反射调用对应 Java 方法。

示例:JSON Schema 生成

{"name": "getWeather","description": "获取指定城市的天气预报","parameters": {"type": "object","properties": {"location": { "type": "string", "description": "城市名称" }},"required": ["location"]}
}

核心思想:开发者只需关注业务逻辑,框架自动完成“Java ↔ LLM”桥接。


Spring AI 对 Function Calling 的支持演进

版本阶段支持方式状态
旧版
(≤ 1.0.0-M5)
原生 FunctionCallbackdefaultFunctions()✅ 稳定,但已过时
过渡版
(1.0.0-M6 ~ 1.1.x)
Function Calling 标记为 @Deprecated⚠️ 兼容但不推荐
新版
(≥ 2.0.0-M1)
全面采用 Tool Calling@Tool✅ 官方推荐

版本组合建议

场景推荐组合
旧项目维护Spring Boot 3.2.x + Spring AI 0.8.1
新项目开发Spring Boot 4.0.x + Spring AI 2.0.0-M2

Spring AI 2.x:构建多模型智能应用的终极框架

统一接口,自由切换 —— 一套代码,驱动全球大模型。

一、国际主流商业大模型

厂商模型示例核心增强
OpenAIgpt-5-mini, gpt-4-turbo官方 SDK + 流式 + 工具调用
AnthropicClaude 4.5 Opus/HaikuCitations API + Files API
GoogleGemini 1.5 Pro/FlashThinkingConfig + 多模态
MistralMixtral, Mistral Large结构化 JSON 输出
AzureAzure OpenAI企业合规 + 私有网络支持

二、国产大模型(重点支持)

厂商模型接入方式
阿里云Qwen-Max/Plus/VL灵积平台 API
智谱 AIGLM-4, GLM-3-Turbo原生 API
百度ERNIE-Bot 4.0千帆平台
月之暗面Moonshot超长上下文(128K+)
DeepSeekDeepSeek-V2/CoderOpenAI 兼容代理

️ 三、开源与本地部署模型

  • Ollama:本地运行 Llama 3、Qwen、Mistral,通过 OpenAI 兼容 API 对接
  • Hugging Face:调用 HF Inference Endpoints
  • 高性能平台:Groq(低延迟)、NVIDIA NIM(GPU 加速)

四、多模态与专用能力

能力支持服务接口
文生图DALL·E 3, Stable DiffusionImageModel
语音合成(TTS)ElevenLabsAudioModel
语音识别(ASR)Google Gemini音频输入支持
向量存储(RAG)Redis, Milvus, PineconeVectorStore + EmbeddingModel

选型建议

场景推荐方案
快速原型OpenAI (gpt-5-mini) 或 Ollama (Llama 3)
中文生产Qwen-Max / GLM-4
长文本处理Claude 4.5 / Moonshot
低成本 + 隐私Ollama + Qwen-7B(本地部署)

⚙️ 开发体验:一行配置,无缝切换

spring:ai:openai:api-key: ${API_KEY}chat:options:model: qwen-max  # ← 仅改此处,即可切换模型!

无需修改业务代码,真正实现“模型即插即用”。


✅ 总结

Spring AI 2.x 是面向未来的 AI 应用基础设施,它通过:

  • 统一抽象接口ChatClient, EmbeddingModel...)
  • 深度模型集成(国内外 + 开源 + 多模态)
  • 企业级能力增强(Redis 记忆、语义缓存、结构化输出)

帮助开发者:

  • 专注业务逻辑,而非模型细节
  • 自由选择模型,避免厂商锁定
  • 平滑演进架构,从原型到生产无缝过渡
http://www.jsqmd.com/news/508011/

相关文章:

  • SPIRAN ART SUMMONER效果展示:风格迁移对比实验
  • Qwen3-32B-Chat百度开发者能力认证:部署/调优/安全/扩展四大模块考核大纲
  • GD32 Flash擦写异常排查:EXMC配置陷阱与pgerr的深层解析
  • 基于高保真UI伪造与反沙箱机制的加密货币钓鱼攻击研究
  • BLE广播包中的公司ID:如何快速查询和修改(附最新Company-Identifiers表)
  • 丹青识画入门必看:从部署到生成,完整体验AI艺术创作流程
  • 信捷XD5与威纶触摸屏编写机械手六轴程序:成熟可靠,高借鉴价值,附详细注释
  • BAAI/bge-m3性能优化:CPU环境下如何实现毫秒级向量计算
  • VSCode 1.109 Chat UX 大升级!
  • 2026年TQM系统排名:10款实用TQM系统助力质量提升
  • LobeChat实战体验:一键部署多模态聊天机器人,效果惊艳实测
  • BMP180气压传感器驱动开发与海拔计算实战
  • 抖音直播数据实时抓取终极指南:5个实战技巧打造智能监控系统
  • Java+大模型工程化落地:AIGS范式重构企业级服务新内核
  • 又整新活, IDEA 2025.3.3发布:骚操作,跟不上了!
  • Vite 7.0 性能优化指南:Rolldown 集成与 advancedChunks 配置详解
  • Java使用zip4j压缩工具时如何避免‘Zip headers not found‘错误:实战排查与解决方案
  • 文墨共鸣模型API开发入门:使用Node.js构建简易聊天机器人
  • 通义千问2.5-7B-Instruct部署指南:解决CUDA显存不足,新手避坑大全
  • 从模型选择到出图:Anything V5快速入门与实战技巧分享
  • IDEA开发者必看:SkyWalking Agent 9.0.0在Java应用中的埋点实战(含JVM参数详解)
  • CasRel模型Java八股文知识抽取:构建面试题库关系网络
  • Beyond Compare 5 密钥生成技术深度解析与完整部署指南
  • 5分钟搞定!用Kuboard管理k3s上的ASP.NET Core应用(2024最新版)
  • 【目标跟踪算法】Strong SORT与Deep SORT对比:优化点解析与性能提升实战
  • GPT模型实战:从零开始搭建一个简单的文本生成器(附Python代码)
  • 避坑指南:ExternalProject_Add的5个隐藏陷阱与解决方案(基于CMake 3.25)
  • Qwen3-32B私有部署保姆级教程:RTX4090D+550.90.07驱动兼容性验证
  • PP-DocLayoutV3模型更新与维护:如何安全升级到新版本
  • Mac上Charles抓包工具保姆级安装教程(含HTTPS证书配置)