下面是《Spring Boot 整合 DeepSeek 实现 AI 对话(支持 API 调用和本地部署)》的完整实践指南。本文适合开发者快速将 DeepSeek 大模型集成进 Spring Boot 项目中,无论你使用的是 DeepSeek 官方 API,还是在本地运行模型(如 DeepSeek LLM 本地部署)。
✅ Spring Boot 整合 DeepSeek 实现 AI 对话(API 调用 + 本地部署)
📌 一、前提准备
✅ DeepSeek 模型简介
- DeepSeek 是一款开源大语言模型(LLM),在中文和多语言任务中表现优异。
- 模型支持 API 接入和本地部署(基于 GGUF、Hugging Face 等)。
- DeepSeek Chat API 与 OpenAI API 接口格式兼容,便于集成。
✅ 二、API 方式调用 DeepSeek(推荐使用官方推理服务)
1. 获取 API Key 和 Endpoint
- 登录:https://platform.deepseek.com/
- 获取:
- API-KEY(密钥)
- Endpoint(通常为
https://api.deepseek.com/v1/chat/completions
)
2. 添加 Maven 依赖
你可以使用 Spring AI,也可以直接使用 RestTemplate
或 WebClient
。
推荐方式:使用 Spring AI(OpenAI 接口兼容)
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>0.8.1</version>
</dependency>
3. 配置 application.yml
spring:
ai:
openai:
base-url: https://api.deepseek.com/v1
api-key: sk-xxxxxxx # 你的 DeepSeek API Key
model: deepseek-chat # 或 deepseek-coder 等
4. 编写 Chat 接口代码
@RestController
@RequestMapping("/chat")
public class ChatController {
@Autowired
private OpenAiChatClient chatClient;
@GetMapping
public String chat(@RequestParam String prompt) {
return chatClient.call(prompt);
}
}
✅ 三、本地部署 DeepSeek 模型(离线运行)
1. 下载模型
- DeepSeek 模型可以从 Hugging Face 下载(如
deepseek-ai/deepseek-llm-7b-base
) - 或使用 GGUF 格式部署(用于 llama.cpp / Ollama / LM Studio)
2. 使用 Ollama 本地运行(推荐)
安装 Ollama:
curl -fsSL https://ollama.com/install.sh | sh
拉取模型:
ollama pull deepseek:7b
启动模型:
ollama run deepseek
默认会启动一个本地服务(通常是 http://localhost:11434
)
3. Spring Boot 本地对接接口代码(使用 WebClient
调用)
@Service
public class LocalDeepSeekService {
private final WebClient webClient = WebClient.builder()
.baseUrl("http://localhost:11434") // Ollama 默认地址
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
public Mono<String> chat(String prompt) {
Map<String, Object> body = Map.of(
"model", "deepseek",
"prompt", prompt,
"stream", false
);
return webClient.post()
.uri("/api/generate")
.bodyValue(body)
.retrieve()
.bodyToMono(JsonNode.class)
.map(json -> json.get("response").asText());
}
}
4. 控制器调用
@RestController
@RequestMapping("/local-chat")
public class LocalChatController {
@Autowired
private LocalDeepSeekService deepSeekService;
@GetMapping
public Mono<String> localChat(@RequestParam String prompt) {
return deepSeekService.chat(prompt);
}
}
✅ 四、进阶功能支持
功能 | API 调用 | 本地部署 |
---|---|---|
多轮对话 | ✅(通过传入 message history) | ✅(自行维护上下文) |
流式输出 | ✅(支持 SSE) | ✅(Ollama 支持流式) |
JSON 输出结构 | ✅ | ✅ |
GPT 接口兼容 | ✅ 完全兼容 OpenAI Chat | ✅ Ollama 支持 GPT 格式 |
插件扩展 | ❌ | ✅(可嵌入工具链) |
✅ 五、常见问题与解决方案
问题 | 说明 |
---|---|
调用失败 401 | 检查 API Key 是否正确 |
返回模型不支持 | 模型名填写错误,或 API Key 无权限 |
本地模型运行慢 | 需要 16GB 以上内存,建议使用 GPU |
多轮对话混乱 | 未正确维护上下文 message history |
✅ 六、项目结构建议
src/
└── main/
├── java/
│ ├── controller/ # ChatController、LocalChatController
│ ├── service/ # DeepSeekService、LocalDeepSeekService
└── resources/
└── application.yml
✅ 七、总结与建议
适用场景 | 推荐方式 |
---|---|
轻量 AI 接入、无需部署 | ✅ DeepSeek 官方 API |
内网使用、安全性高 | ✅ 本地部署(Ollama + DeepSeek 模型) |
多轮对话、插件开发 | ✅ 本地部署自定义更灵活 |
云服务兼容性 | ✅ DeepSeek API 兼容 OpenAI,可接入 Spring AI |
好的,下面是你需要的 完整 Spring Boot 示例工程代码结构,整合了 DeepSeek API 调用 + 本地部署(Ollama)双通道对话功能,包括:
- Maven 依赖
application.yml
配置- Controller、Service 分层代码
- 支持 OpenAI 接口兼容模型(如 DeepSeek-Chat)
✅ 一、工程结构概览
deepseek-chat/
├── pom.xml
├── src/
│ └── main/
│ ├── java/
│ │ └── com/example/deepseek/
│ │ ├── DeepSeekChatApplication.java
│ │ ├── controller/
│ │ │ ├── ApiChatController.java
│ │ │ └── LocalChatController.java
│ │ └── service/
│ │ ├── ApiDeepSeekService.java
│ │ └── LocalDeepSeekService.java
│ └── resources/
│ └── application.yml
✅ 二、pom.xml
(核心依赖)
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>deepseek-chat</artifactId>
<version>1.0.0</version>
<properties>
<java.version>17</java.version>
<spring.boot.version>3.2.4</spring.boot.version>
</properties>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring AI OpenAI Starter -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>0.8.1</version>
</dependency>
<!-- Reactor/JSON处理 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
</project>
✅ 三、application.yml
配置
spring:
ai:
openai:
base-url: https://api.deepseek.com/v1
api-key: sk-你的API密钥
model: deepseek-chat
✅ 四、主程序入口 DeepSeekChatApplication.java
package com.example.deepseek;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeepSeekChatApplication {
public static void main(String[] args) {
SpringApplication.run(DeepSeekChatApplication.class, args);
}
}
✅ 五、API 模式:调用官方 DeepSeek Chat 接口
1. Service 类 ApiDeepSeekService.java
package com.example.deepseek.service;
import org.springframework.ai.openai.api.OpenAiChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ApiDeepSeekService {
@Autowired
private OpenAiChatClient chatClient;
public String chat(String prompt) {
return chatClient.call(prompt);
}
}
2. 控制器 ApiChatController.java
package com.example.deepseek.controller;
import com.example.deepseek.service.ApiDeepSeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api-chat")
public class ApiChatController {
@Autowired
private ApiDeepSeekService apiDeepSeekService;
@GetMapping
public String chat(@RequestParam String prompt) {
return apiDeepSeekService.chat(prompt);
}
}
✅ 六、本地部署方式:使用 Ollama + DeepSeek 模型
1. Service 类 LocalDeepSeekService.java
package com.example.deepseek.service;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.util.Map;
@Service
public class LocalDeepSeekService {
private final WebClient webClient = WebClient.builder()
.baseUrl("http://localhost:11434") // Ollama 默认地址
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
public Mono<String> chat(String prompt) {
Map<String, Object> body = Map.of(
"model", "deepseek",
"prompt", prompt,
"stream", false
);
return webClient.post()
.uri("/api/generate")
.bodyValue(body)
.retrieve()
.bodyToMono(JsonNode.class)
.map(json -> json.get("response").asText());
}
}
2. 控制器 LocalChatController.java
package com.example.deepseek.controller;
import com.example.deepseek.service.LocalDeepSeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/local-chat")
public class LocalChatController {
@Autowired
private LocalDeepSeekService localDeepSeekService;
@GetMapping
public Mono<String> chat(@RequestParam String prompt) {
return localDeepSeekService.chat(prompt);
}
}
✅ 七、访问测试
启动 Spring Boot 项目后,你可以通过如下方式访问:
- 在线 API 模式(官方 DeepSeek):
GET http://localhost:8080/api-chat?prompt=你好
- 本地模型模式(Ollama DeepSeek):
GET http://localhost:8080/local-chat?prompt=你好
✅ 八、扩展建议(可选)
功能 | 技术建议 |
---|---|
多轮对话上下文 | 使用 message history 或 Redis/session 缓存上下文 |
向量搜索/知识库 | 集成 Milvus、Weaviate、Pinecone |
流式输出 | WebSocket / SSE |
接入前端页面 | 可使用 Vue/React 调用接口 |
提示词模板化 | 使用 PromptTemplate (Spring AI 支持) |
发表回复