官方文檔
gitee的demo
1、前言
2025年5月,SpringAI 1.0.0終于正式發布。這不僅是另一個普通的庫,更是將Java和Spring推向AI革命前沿的戰略性舉措。給Java生態帶來了強大且全面的AI工程解決方案。眾多企業級應用在SpringBoot上運行關鍵業務,而SpringAI 1.0.0的發布,將賦予開發者將應用程序與前沿AI模型無縫連接的能力!
官方文檔中已提供了眾多能力的說明,旨在簡化大模型的應用程序的開發。
另外,開源大模型的選擇(如deepseekR1(0528版)),不同蒸餾模型的選擇,可參考github上的開源大模型排行榜
2、運行環境
SpringAI基于spingboot3.x版本,需要JDK17以上。
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.5</version><relativePath/> <!-- lookup parent from repository --></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>1.0.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
3、Api-Key申請
SpringAI提供多種AI提供商的便攜式Model,包括各類多模態:圖像識別、語音識別、視頻識別,以及最基本的LLM文本對話,例如:Claude、OpenAI、DeepSeek、ZhiPu等。
本文使用智譜AI的大模型演示,新用戶可獲得有期限的免費次數
也可以使用本地安裝大模型:https://ollama.com/。通過ollama,就不再需要環境(大模型很多都是依賴Python環境)
4、完整pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>spring-ai-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-ai-demo</name><description>Demo project for Spring Boot</description><properties><java.version>17</java.version><fastjson.version>2.0.53</fastjson.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 智譜 --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-zhipuai</artifactId></dependency><!-- deepseek --><!--<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-deepseek</artifactId></dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>${fastjson.version}</version></dependency><!--mcp server--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-server</artifactId></dependency><!--即支持sse,也支持stdio--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-client-webflux</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>1.0.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>16</source><target>16</target></configuration></plugin></plugins></build></project>
阿里的大模型pom=sping-ai-alibaba-starter-dashscope
5、application.properties文件
server:servlet:context-path: /aispring:application:name: spring-ai-demoai:chat:client:# 禁用默認chat clientenabled: falsezhipuai:# 從環境變量取api-key: ${API_KEY_ZHIPUAI}chat:options:model: glm-4-plustemperature: 0.7data:redis:host: localhostport: 6379password: 123123!lettuce:pool:min-idle: 0max-idle: 8max-active: 8max-wait: -1ms
6、特性與Demo
6.1、最簡單的對話
配置ChatClient
/*** 默認client*/@Beanpublic ChatClient zhiPuAiChatClient(ZhiPuAiChatModel chatModel) {return ChatClient.create(chatModel);}
定義接口
/*** 最簡單的chat** @author stone* @date 2025/6/26 16:11*/
@RestController
@RequestMapping("/case1")
@Slf4j
public class Case1Controller {@Resource@Qualifier("zhiPuAiChatClient")private ChatClient chatClient;/*** 直接獲取結果*/@GetMapping("/chat")public String chat(@RequestParam("input") String input) {// input=講個笑話return this.chatClient.prompt().user(input).call().content();}/*** 轉化實體*/@GetMapping("/entity")public List<ActFilm> entity(@RequestParam("input") String input) {// input=生成劉德華和劉亦菲的10部電影return this.chatClient.prompt().user(input).call().entity(new ParameterizedTypeReference<List<ActFilm>>() {});}/*** 流式響應*/@GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<String> flux(@RequestParam("input") String input) {// input=講個笑話return this.chatClient.prompt().user(input).stream().content();}/*** 動態輸入*/@GetMapping(value = "/fluxDynamic", produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<String> fluxDynamic(@RequestParam("input") String input, @RequestParam("name") String name) {return this.chatClient.prompt().user(promptUserSpec -> promptUserSpec.text("告訴我中國有多少叫{name}的人").param("name", name)).stream().content();}}
.user,也就是用戶提示詞
.call,同步方式響應,也就是一整個結果返回
.stream,流式響應,調整為sse方式(text/event-stream)
6.2、默認系統文本
預定義chatClient,設置系統提示詞
/*** 參數-占位符的默認系統文本*/@Beanpublic ChatClient paramTextChatClient(ZhiPuAiChatModel chatModel) {return ChatClient.builder(chatModel).defaultSystem("你是一個智能聊天機器人,用 {role} 的角度回答問題").build();}/*** 默認系統文本*/@Beanpublic ChatClient defaultTextChatClient(ZhiPuAiChatModel chatModel) {return ChatClient.builder(chatModel).defaultSystem("你是一個智能聊天機器人,用邪惡女巫的角度回答問題").build();}
定義接口
/*** 默認系統文本** @author stone* @date 2025/6/30 15:11*/
@RestController
@RequestMapping("/case2")
@Slf4j
public class Case2Controller {@Resource@Qualifier("defaultTextChatClient")private ChatClient defaultTextChatClient;/*** 默認系統文本*/@GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<String> flux(@RequestParam("input") String input) {// input=講個笑話return this.defaultTextChatClient.prompt().user(input).stream().content();}@Resource@Qualifier("paramTextChatClient")private ChatClient paramTextChatClient;/*** 動態系統文本*/@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<String> chat(@RequestParam("input") String input, @RequestParam("role") String role) {// input=聊一聊圓明園的故事吧,500字以內// role=數學老師/邪惡女巫return this.paramTextChatClient.prompt().system(promptSystemSpec -> promptSystemSpec.param("role", role)).user(input).stream().content();}}
消息類型
提示詞的不同部分,在交互中扮演著獨特和定義明確的角色。
6.3、advisors
提供了強大靈活的攔截式AI交互驅動(配置多個advisor時,前一個做出的更改會傳遞給下一個)
/*** @author stone* @date 2025/6/30 15:41*/
@RestController
@RequestMapping("/case3")
@Slf4j
public class Case3Controller {@Resource@Qualifier("paramTextChatClient")private ChatClient chatClient;/*** 最簡單的advisor=日志* <p>* org.springframework.ai.chat.client.advisor=debug*/@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<String> chat(@RequestParam("input") String input, @RequestParam("role") String role) {// input=聊一聊圓明園的故事吧,500字以內// role=邪惡女巫return this.chatClient.prompt().system(promptSystemSpec -> promptSystemSpec.param("role", role)).advisors(new SimpleLoggerAdvisor()).user(input).stream().content();}/*** 自定義打印內容*/@GetMapping(value = "/chat2",produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<String> chat2(@RequestParam("input") String input, @RequestParam("role") String role) {// input=聊一聊圓明園的故事吧,500字以內// role=邪惡女巫return this.chatClient.prompt().system(promptSystemSpec -> promptSystemSpec.param("role", role)).advisors(SimpleLoggerAdvisor.builder().requestToString(req -> "請求參數:" + req.prompt().getUserMessage().getText()).responseToString(resp -> "響應參數:" + resp.getResult().getOutput().getText()).build()).user(input).stream().content();}/*** 定義子類,自定義打印的*/@GetMapping(value = "/chat3",produces = MediaType.TEXT_EVENT_STREAM_VALUE)public Flux<String> chat3(@RequestParam("input") String input, @RequestParam("role") String role) {// input=聊一聊圓明園的故事吧,500字以內// role=邪惡女巫return this.chatClient.prompt().system(promptSystemSpec -> promptSystemSpec.param("role", role)).advisors(new SimpleLogAdvisor()).user(input).stream().content();}/*** 自定義日志打印*/public static class SimpleLogAdvisor extends SimpleLoggerAdvisor {public ChatClientResponse adviseCall(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain) {log.info("自定義日志,請求參數:{}", chatClientRequest.prompt().getUserMessage().getText());ChatClientResponse chatClientResponse = super.adviseCall(chatClientRequest, callAdvisorChain);log.info("自定義日志,響應結果:{}", chatClientResponse.chatResponse().getResult().getOutput().getText());return chatClientResponse;}public Flux<ChatClientResponse> adviseStream(ChatClientRequest chatClientRequest, StreamAdvisorChain streamAdvisorChain) {log.info("自定義日志,請求參數:{}", chatClientRequest.prompt().getUserMessage().getText());Flux<ChatClientResponse> chatClientResponses = streamAdvisorChain.nextStream(chatClientRequest);return (new ChatClientMessageAggregator()).aggregateChatClientResponse(chatClientResponses, this::logResponse);}private void logResponse(ChatClientResponse chatClientResponse) {log.info("自定義日志,響應結果:{}", chatClientResponse.chatResponse().getResult().getOutput().getText());}}
}
6.4、對話記憶功能
通過多輪對話,實現聊天內存功能,通過實現交互信息的持久化存儲與動態檢索機制
public Case4Controller(ZhiPuAiChatModel chatModel,ChatMemory chatMemory) {this.chatClient = ChatClient.builder(chatModel).defaultAdvisors(PromptChatMemoryAdvisor.builder(chatMemory).build()).build();}private ChatClient chatClient;
基于JVM內存;設置唯一信息,通過常量區分不同的用戶對話
/*** 對話記憶功能-基于內存* <p>* MessageWindowChatMemory:默認最大20* InMemoryChatMemoryRepository:使用map*/
// @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)@GetMapping(value = "/chat")public Flux<String> chat(@RequestParam("input") String input) {// input=我叫什么return this.chatClient.prompt().user(input).stream().content();}/*** 對話記憶功能-區分不同用戶*/@GetMapping(value = "/chat2")public Flux<String> chat2(@RequestParam("input") String input,@RequestParam("userId") String userId) {return this.chatClient.prompt().user(input).advisors(a -> a.param(ChatMemory.CONVERSATION_ID, userId)).stream().content();}
基于Redis的多輪對話記憶功能
/*** @author stone* @date 2025/7/14 10:20*/
@Data
public class ChatBO implements Serializable {/*** 用戶對話唯一標識*/private String chatId;/*** 對話類型*/private String type;/*** 對話內容*/private String text;
}/*** @author stone* @date 2025/7/14 10:19*/
@Slf4j
@Component
public class ChatRedisMemory implements ChatMemory {private static final String KEY_PREFIX = "chat:history:";private final RedisTemplate<String, Object> redisTemplate;public ChatRedisMemory(RedisTemplate<String, Object> redisTemplate) {this.redisTemplate = redisTemplate;}@Overridepublic void add(String conversationId, List<Message> messages) {String key = KEY_PREFIX + conversationId;List<String> list = new ArrayList<>();for (Message msg : messages) {String[] strs = msg.getText().split("</think>");String text = strs.length == 2 ? strs[1] : strs[0];// 轉化ChatBO bo = new ChatBO();bo.setChatId(conversationId);bo.setType(msg.getMessageType().getValue());bo.setText(text);list.add(JSON.toJSONString(bo));}redisTemplate.opsForList().rightPushAll(key, list.toArray());redisTemplate.expire(key, 30, TimeUnit.MINUTES);}@Overridepublic List<Message> get(String conversationId) {String key = KEY_PREFIX + conversationId;Long size = redisTemplate.opsForList().size(key);if (size == null || size == 0) {return Collections.emptyList();}List<Object> listTmp = redisTemplate.opsForList().range(key, 0, -1);List<Message> result = new ArrayList<>();for (Object obj : listTmp) {ChatBO chat = JSON.parseObject(obj.toString(), ChatBO.class);if (MessageType.USER.getValue().equals(chat.getType())) {result.add(new UserMessage(chat.getText()));} else if (MessageType.ASSISTANT.getValue().equals(chat.getType())) {result.add(new AssistantMessage(chat.getText()));} else if (MessageType.SYSTEM.getValue().equals(chat.getType())) {result.add(new SystemMessage(chat.getText()));}}return result;}@Overridepublic void clear(String conversationId) {redisTemplate.delete(KEY_PREFIX + conversationId);}
}
/*** 基于redis的對話記憶*/@Beanpublic ChatMemory chatMemory(RedisTemplate<String, Object> redisTemplate) {return new ChatRedisMemory(redisTemplate);}
public Case4Controller(ZhiPuAiChatModel chatModel,ChatMemory chatMemory) {this.chatClient = ChatClient.builder(chatModel).defaultAdvisors(PromptChatMemoryAdvisor.builder(chatMemory).build()).build();}/*** 對話記憶功能-基于redis*/@GetMapping(value = "/chat3")public Flux<String> chat3(@RequestParam("input") String input,@RequestParam("userId") String userId) {return this.chatClient.prompt().user(input).advisors(a -> a.param(ChatMemory.CONVERSATION_ID, userId)).stream().content();}
多層記憶機構,模仿人類,做到近期(清晰),中期(模糊),長期(關鍵點)。這里就引入了向量數據庫和RAG。
6.5、@Tools使用
聲明式Function Calling,將方法轉化為工具。提前告訴大模型,提供了什么tools。太多的tools,可以放到向量數據庫。
第三方提供的tools,比如百度天氣、高德位置,不可能各對接系統去做解析。因此MCP(model content protol)協議,通過JSON-rpc2.0方式(json數據格式),統一格式解析。
/*** tools工具** @author stone* @date 2025/7/4 10:42*/
@Component
@Slf4j
public class OrderTools {/*** 比如在退訂、取消訂單*/@Tool(description = "退訂、取消訂單")public String cancelOrder(@ToolParam(description = "訂單號") String orderNum,@ToolParam(description = "賬號") String userAccount) {log.info("訂單號:{},用戶賬號:{}", orderNum, userAccount);// 執行業務邏輯log.info("處理數據庫...");return "操作成功";}
}
/*** @author stone* @date 2025/7/4 10:40*/
@RestController
@RequestMapping("/case5")
@Slf4j
public class Case5Controller {public Case5Controller(ZhiPuAiChatModel chatModel,ChatMemory chatMemory,OrderTools orderTools) {this.chatClient = ChatClient.builder(chatModel).defaultAdvisors(PromptChatMemoryAdvisor.builder(chatMemory).build()).defaultTools(orderTools).build();}private ChatClient chatClient;/*** tools使用*/@GetMapping("/chat")public String chat(@RequestParam("input") String input) {// input=我要退訂// input=賬號是101,訂單號是XXX1111return this.chatClient.prompt()// 直接方法使用
// .tools().user(input).call().content();}
}
6.6、調用外部MCP-server
TODO...
6.7、向量數據庫與RAG(檢索增強生成)
TODO...