SpringAI 实现了代码不变,只改配置和引入pom,就能更换对接的模型。
官方网站:Introduction :: Spring AI Reference。
参考官网注意文档版本,点击页面中的 ··· 选择版本。

完成聊天功能可以参考:Chat Model API :: Spring AI Reference
目前内容已经更新到2.0.2,但是发现国内模型只支持deepseek。所以目前采用最新的1.1.8来讲解所有示例。

创建项目
项目采用一个整体的Spring AI项目,各个模块介绍对应的功能。
引入父级pom
XML
<?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 http://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.5.16</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.erbao</groupId>
<artifactId>SpringAI</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>ChatModels</module>
<module>EmbeddingModels</module>
<module>ImageModels</module>
<module>AudioModels</module>
<module>Ollama</module>
<module>ChatClient</module>
<module>ChatMemory</module>
<module>ToolCalling</module>
<module>MCPServer</module>
<module>MCPClient</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.1.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<name>Central Portal Snapshots</name>
<id>central-portal-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
引入module:pom
XML
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.erbao</groupId>
<artifactId>SpringAI</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>ChatModels</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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-deepseek</artifactId>
</dependency>
</dependencies>
</project>
配置文件
application.properties
XML
spring.application.name=SpringAIChat
server.port=8080
#配置 Deepseek的基础URL、密钥和使用模型。参考网站:https://api-docs.deepseek.com/zh-cn/
spring.ai.deepseek.base-url=https://api.deepseek.com
spring.ai.deepseek.api-key=sk-403bdd......0b68855207
spring.ai.deepseek.chat.options.model=deepseek-flash
配置的key都有什么可以参考Spring AI官网,chat model下的deepseek,官网导航路径:
Spring AI 》Reference 》Models 》Chat Models 》DeepSeek
官网直达地址:DeepSeek Chat :: Spring AI Reference
配置的value,参考网站是对应的模型网站,一般会叫控制台、开放平台、API接口文档等。本文中的deepseek,是叫API 文档,官网直达:首次调用 API | DeepSeek API Docs

聊天可以只使用文本模型。
多模态模型:支持文字、图片、文档等。
不同的模型价格会不一样,自己注意。
附:获取deep seek的apikey
官网:DeepSeek | 深度求索。下滑官网首页就能看到API对接部分入口

点击API开发平台,登录后进入充值页面。自己学习充值1-10就行。

充值结束,点击左侧API Keys,手动创建 API key

代码实现
启动类
只是最基本的spring boot项目,没有区别。
java
package com.erbao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ChatModelsApplication {
public static void main(String[] args) {
SpringApplication.run(ChatModelsApplication.class, args);
}
}
接口
java
package com.erbao.rest;
import jakarta.servlet.http.HttpServletResponse;
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.deepseek.DeepSeekChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private DeepSeekChatModel chatModel; // deepseek 聊天的对象
/**
* 生成聊天模型的响应
* @param message 用户输入的消息
* @return 聊天模型的响应
*/
@GetMapping("/generate")
public String generate(String message) {
return chatModel.call(message);
}
/**
* 流式返回聊天模型的响应
* @param message 用户输入的消息
* @param response HttpServletResponse对象,用于设置字符编码
* @return 流式返回的聊天模型响应
*/
@GetMapping("/stream")
public Flux<String> stream(String message, HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
Prompt prompt = new Prompt(new UserMessage(message));
Flux<ChatResponse> responseFlux = chatModel.stream(prompt);
return responseFlux
.doOnNext(chatResponse -> System.out.println("ChatResponse chunk: " + chatResponse))
.map(chatResponse -> {
if (chatResponse == null || chatResponse.getResult() == null
|| chatResponse.getResult().getOutput() == null) {
return "";
}
String text = chatResponse.getResult().getOutput().getText();
return text != null ? text : "";
})
.filter(s -> !s.isEmpty());
}
}
对于private DeepSeekChatModel chatModel; 可以直接写 ChatModel,就是使用接口接收实例化对象,这样pom文件引入了什么模型依赖,就会注入什么模型。
注意:pom文件中引入了多个模型依赖,使用ChatModel接收,项目会报错,此时必须指定使用的哪个模型,例如:DeepSeekChatModel。
以上示例实现了简单的聊天和流式响应方式。