Spring AI DeepSeek 详细连接指南
本指南详细介绍如何在 Spring AI 项目中集成和使用 DeepSeek AI 模型,包括 ChatModel 和 ChatClient 的完整示例。
目录
项目概述
Spring AI 是 Spring 生态系统中的 AI 抽象层,提供了统一的 API 来访问各种 AI 模型和功能。DeepSeek 是一家中国 AI 公司,提供高性能的对话模型和推理模型。
核心特性
- 统一 API: 通过 Spring AI 的统一接口访问 DeepSeek 模型
- 多模型支持: 支持
deepseek-chat和deepseek-reasoner两种模型 - 流式响应: 支持实时流式输出
- 工具调用: 支持函数调用和工具集成
- Spring Boot 原生集成: 自动配置和属性绑定
DeepSeek AI 简介
支持的模型
-
deepseek-chat
- 基于 DeepSeek-V3 模型
- 支持 64K 上下文窗口
- 适用于通用对话和文本生成
-
deepseek-reasoner
- 专用推理模型
- 生成思维链(Chain of Thought, CoT)
- 适用于复杂问题推理和解决
API 特性
- 完全兼容 OpenAI API 格式
- 支持流式和非流式响应
- 内置函数调用支持
- 提供 token 使用统计
环境准备
1. 获取 DeepSeek API Key
- 访问 DeepSeek 平台
- 注册并登录账户
- 在控制台中生成 API Key
5. 确保 API Key 有足够的额度
2. 项目要求
- Java 17+
- Spring Boot 3.x
- Spring AI 1.0+
依赖配置
Maven 依赖
<?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.5.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>wu.zuxian</groupId>
<artifactId>spring-ai-deepseek</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-ai-deepseek</name>
<description>spring-ai-deepseek</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.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-deepseek</artifactId>
<version>1.1.0-M3</version>
</dependency>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>java-dotenv</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
ChatModel 集成
1. 基础配置
application.yml 配置
spring:
ai:
deepseek:
# 基础连接配置
api-key: ${DEEPSEEK_API_KEY} # 从环境变量读取 API Key
base-url: https://api.deepseek.com # API 基础 URL
# 聊天模型配置
chat:
enabled: true # 启用聊天模型
options:
model: deepseek-chat # 默认模型
temperature: 0.7 # 温度参数 (0.0-2.0)
max-tokens: 2000 # 最大生成 token 数
top-p: 0.9 # 核采样参数
# 高级选项
frequency-penalty: 0.0 # 频率惩罚
presence-penalty: 0.0 # 存在惩罚
stop: ["\n\n", "###"] # 停止序列
# 工具调用配置
internal-tool-execution-enabled: true # 内部工具执行
需要将.env文件中的环境变量值加载到系统变量中去:
package wu.zuxian.springaideepseek;
import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvEntry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAiDeepseekApplication {
public static void main(String[] args) {
// 加载.env文件到系统环境变量
loadEnvFile();
SpringApplication.run(SpringAiDeepseekApplication.class, args);
}
/**
* 加载.env文件中的环境变量到系统环境
*/
private static void loadEnvFile() {
try {
Dotenv dotenv = Dotenv.configure()
.ignoreIfMissing()
.ignoreIfMalformed()
.load();
// 将环境变量设置到系统属性中
for (DotenvEntry entry : dotenv.entries()) {
System.setProperty(entry.getKey(), entry.getValue());
}
} catch (Exception e) {
System.err.println("加载.env文件失败: " + e.getMessage());
}
}
}
2. 基础使用示例
简单对话
package wu.zuxian.springaideepseek;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
public class ChatService {
private final ChatModel chatModel;
@Autowired
public ChatService(ChatModel chatModel) {
this.chatModel = chatModel;
}
//http://127.0.0.1:8080/chat?message=我是一个Java程序员,请帮我想一个花名
@GetMapping("/chat")
public String chat(String message) {
Prompt prompt = new Prompt(message);
ChatResponse response = chatModel.call(prompt);
return response.getResult().getOutput().getText();
}
//http://127.0.0.1:8080/stream-chat?message=我是一个Java程序员,请帮我想一个花名
@GetMapping("/stream-chat")
public Flux<String> streamChat(String message) {
Prompt prompt = new Prompt(message);
return chatModel.stream(prompt)
.map(response -> response.getResult().getOutput().getText());
}
}
非流式对话:
流式对话:
注意:会有中文乱码
解决方案: 配置文件修改即可。
server:
servlet:
encoding:
charset: UTF-8
force: true
更多信息和详细文档,请参考: