springboot和deepseek接入
时间: 2025-05-22 18:48:31 AIGC 浏览: 34
### 集成 DeepSeek 到 Spring Boot 的方法
要在 Spring Boot 中接入 DeepSeek 模型或服务,可以通过以下方式实现完整的集成流程。
#### 1. 准备工作
为了成功调用 DeepSeek 提供的服务,首先需要完成必要的准备工作:
- **注册 DeepSeek 并获取 API 密钥**
前往 [DeepSeek 官网](https://www.deepseek.ai/) 注册账户,并创建应用以生成专属的 API Key[^3]。此密钥用于身份验证,在后续 HTTP 请求中作为参数传递。
- **记录 API 请求地址**
默认情况下,DeepSeek 提供的标准接口为 `https://api.deepseek.com/v1/chat/completions`,该 URL 将被用来发送请求并接收响应[^3]。
---
#### 2. 创建 Spring Boot 项目
利用 [Spring Initializr](https://start.spring.io/) 初始化一个新的 Spring Boot 工程,确保添加如下依赖项:
- **Spring Web**: 支持构建 RESTful 应用程序。
- **Lombok**: 可选工具库,减少样板代码量。
- **OkHttp 或 Apache HttpClient**: 实现对外部 HTTP 服务的高效调用[^3]。
以下是 Maven 构建文件中的部分依赖声明示例:
```xml
<dependencies>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Lombok (Optional) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- OkHttp Client -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>
```
---
#### 3. 编写核心逻辑
通过编写控制器和服务层来封装与 DeepSeek 的交互过程。
##### a. 配置 API 参数
在项目的配置文件 (`application.properties`) 中存储敏感数据(如 API Key 和请求地址)以便于管理:
```properties
deepseek.api.key=YOUR_API_KEY_HERE
deepseek.api.url=https://api.deepseek.com/v1/chat/completions
```
##### b. 发起 HTTP 请求
基于 OkHttp 客户端发起 POST 请求至 DeepSeek 接口,传入 JSON 数据体指定对话内容或其他选项。
```java
import okhttp3.*;
import org.json.JSONObject;
public class DeepSeekService {
private final OkHttpClient client;
private final String apiKey;
private final String apiUrl;
public DeepSeekService(String apiKey, String apiUrl) {
this.client = new OkHttpClient();
this.apiKey = apiKey;
this.apiUrl = apiUrl;
}
public String sendMessage(String message) throws Exception {
JSONObject jsonBody = new JSONObject()
.put("prompt", message)
.put("max_tokens", 150);
RequestBody body = RequestBody.create(
MediaType.get("application/json"),
jsonBody.toString());
Request request = new Request.Builder()
.url(apiUrl)
.post(body)
.addHeader("Authorization", "Bearer " + apiKey)
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return response.body().string();
}
}
}
```
##### c. 控制器暴露接口
设计一个 REST Controller 来接受外部输入并将结果返回给前端用户。
```java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/chat")
public class ChatController {
@Value("${deepseek.api.key}")
private String apiKey;
@Value("${deepseek.api.url}")
private String apiUrl;
@PostMapping
public String chat(@RequestBody String userInput) throws Exception {
DeepSeekService service = new DeepSeekService(apiKey, apiUrl);
return service.sendMessage(userInput);
}
}
```
---
#### 4. 测试功能
启动应用程序并通过 Postman 或 curl 工具测试 `/chat` 终端节点的功能性。例如:
```bash
curl -X POST http://localhost:8080/chat \
-H "Content-Type: application/json" \
-d '{"message":"你好"}'
```
如果一切正常运行,则会收到由 DeepSeek 返回的回答内容。
---
#### 总结
以上步骤展示了如何将 DeepSeek 成功嵌入到 Spring Boot 环境之中,从而充分利用其强大的自然语言处理能力提供智能化服务[^4]。
阅读全文
相关推荐



















