SpringBoot AI 连接 Deepseek

SpringBoot AI 连接 Deepseek

POM.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?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.3.5</version>
<relativePath/>
</parent>
<groupId>com.mine</groupId>
<artifactId>DailyReportAI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DailyReportAI</name>
<description>DailyReportAI</description>
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>
<!-- 必须手动开启milestone仓库支持,不然拿不到依赖 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<!-- 开启依赖版本管理,使用新版的 spring AI。旧版的 API 不稳定 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<!-- 移除version,由BOM统一管理 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pdf-document-reader</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-core</artifactId>
</dependency>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

编辑API和Key

application.yml:

1
2
3
4
5
6
7
8
spring:
ai:
openai:
api-key: sk-xxxxx
base-url: https://api.deepseek.com
chat:
options:
model: deepseek-flash

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Service
public class AiService {

private final ChatClient chatClient;
private final ObjectMapper objectMapper;

// 注入 ChatClient.Builder
public AiService(ChatClient.Builder builder) {
this.chatClient = builder.build();
this.objectMapper = new ObjectMapper();
}

/**
* 接收 UserMessage 对象,不再接收原始字符串
*/
public String simpleChat(UserMessage userMessage) {
return chatClient.prompt()
.messages(userMessage)
.call()
.content();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
String promptTemplate = """
根据下面这份文档内容,整理生成工作日报。

编写 5 天的日报信息:**每天**日报都包含:
1. 今天阅读的内容
2. 明日计划;

表达语言要简要流畅,今天阅读的内容约80字,明日计划约40字,同时**体现一些论文的内容**。
文档内容:
%s
""".formatted(cleanText);

// 构造UserMessage,传给simpleChat,由SpringAI框架序列化JSON(自动转义)
UserMessage userMessage = new UserMessage(promptTemplate);
String result = simpleChat(userMessage);

SpringBoot AI 连接 Deepseek
https://taylorandtony.github.io/2026/10/19/SpringBoot-AI-连接-Deepseek/
作者
TaylorAndTony
发布于
2026年10月19日
许可协议