DeepSeek 作为一款卓越的国产 AI 大模型,性能和 ChatGPT 不相上下,而且无需复杂的网络环境,更适合国人。目前越来越多的公司考虑在自己的应用中集成。Java 开发者可以借助 Spring AI 集成 DeepSeek,非常简单方便!
简介
Spring AI 是 Spring 生态中的一个新项目,其主要完成了几种常见生成式模型的适配,包括对话、文生图、文生语音等。Spring AI 目前是通过使用现有的 OpenAI 客户端与 DeepSeek AI 集成的,如图所示:

动手体验
1. 新建项目
首先新建一个Maven项目,JDK选的是17版本

2. pom.xml文件引入依赖
项目pom.xml文件引入
spring-ai-openai-spring-boot-starter依赖
4.0.0
com.jms
jms-ai-deepseek
1.0.0
UTF-8
UTF-8
17
17
17
1.5.16
3.4.2
1.0.0-M6
1.18.26
4.2.1
org.springframework.ai
spring-ai-openai-spring-boot-starter
${spring-ai-openai-spring-boot-starter.version}
org.projectlombok
lombok
${lombok.version}
ch.qos.logback
logback-classic
${logback.version}
ch.qos.logback
logback-core
${logback.version}
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-function-context
org.springframework.cloud
spring-cloud-function-core
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.springframework.cloud
spring-cloud-function-context
${spring-cloud-function.version}
org.springframework.cloud
spring-cloud-function-core
${spring-cloud-function.version}
3. 添加DeepSeek API KEY
DeepSeek API KEY 可以在 DeepSeek 开放平台中自行创建,地址:
https://platform.deepseek.com/api_keys

然后在Spring Boot项目添加application.properties配置文件,加入以下内容(替成你创建的api-key)
# 服务端口
server.port=8089
# 服务名
spring.application.name=jms-ai-deepseek
# DeepSeek的OpenAI式端点
spring.ai.openai.base-url=https://api.deepseek.com/v1
# DeepSeek API KEY
spring.ai.openai.api-key=你创建的api-key
# 配置AI大模型
# deepseek-chat 模型已全面升级为 DeepSeek-V3,接口不变。 通过指定
model=deepseek-chat 即可调用 DeepSeek-V3
# deepseek-reasoner 是 DeepSeek 最新推出的推理模型 DeepSeek-R1。通过指定
model=deepseek-reasoner,即可调用 DeepSeek-R1。
spring.ai.openai.chat.options.model=deepseek-chat
4. 添加Spring Boot启动类
添加Spring Boot启动类 DeepSeekApplication
@SpringBootApplication
public class DeepSeekApplication {
public static void main(String[] args) {
SpringApplication.run(DeepSeekApplication.class,args);
}
}
5. 添加Controller控制器类
新建一个控制器类DeepSeekController,添加文本接口:
@RestController
@RequestMapping("/ai")
@CrossOrigin
@RequiredArgsConstructor
public class DeepSeekController {
private final DeepSeekService deepSeekService;
/**
* 对接文本模型
*
* @param message 消息内容
* @return AI答案
*/
@GetMapping("/chat")
public String completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return deepSeekService.completion(message);
}
}
6. 添加Service服务接口和实现类
新建一个DeepSeekService接口,支持文本模型处理方法:
public interface DeepSeekService {
/**
* 文本模型
*
* @param message 消息内容
* @return AI答案
*/
String completion(String message);
}
新建一个DeepSeekServiceImpl类实现DeepSeekService接口,具体实现如下:
@Slf4j
@Service
public class DeepSeekServiceImpl implements DeepSeekService {
private final ChatClient chatClient;
public DeepSeekServiceImpl(ChatClient.Builder chatClientBuilder) {
// 构造方法注入 ChatClient.Builder,用于构建 ChatClient 实例
this.chatClient = chatClientBuilder.build();
}
/**
* 文本模型
*
* @param message 消息内容
* @return AI答案
*/
@Override
public String completion(String message) {
log.info("DeepSeek response");
// 调用 ChatClient 的 prompt 方法生成响应
// 1. new Prompt(message): 创建一个包含用户输入消息的 Prompt 对象
// 2. call(): 调用ChatClient与AI模型交互以获取响应
// 3. content(): 获取响应的内容部分
return chatClient.prompt(new Prompt(message)).call().content();
}
}
可以看到,几行代码就搞定了,非常简单。
接下来启动DeepSeekApplication应用来测试下效果!!!
测试
调用文本模型接口,可以看到AI生成了一个笑话。

总结
Spring AI框架提供了多种模型选择,简化了我们AI开发功能,只需简单几步即可轻松实现。Spring项目接入DeepSeek大模型介绍到此就结束了,感兴趣的同学可以去试试哈!