1. 准备工作
注册并获取 API Key:你需要在 DeepSeek 平台注册账号,并获取 API Key,这是调用 API 的凭证。
创建 Spring Boot 项目:可以使用 Spring Initializr(https://start.spring.io/)创建一个新的 Spring Boot 项目,添加 Spring Web 依赖。
2. 添加依赖
在 pom.xml 中添加必要的依赖,这里使用 OkHttp 作为 HTTP 客户端来发送请求:
<dependencies>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- OkHttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
<!-- JSON 处理 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
</dependencies>
3. 编写配置类
创建一个配置类来存储 DeepSeek 的 API 相关信息,如 API Key 和 API 地址:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DeepSeekConfig {
@Value("${deepseek.api.key}")
private String apiKey;
@Value("${deepseek.api.url}")
private String apiUrl;
public String getApiKey() {
return apiKey;
}
public String getApiUrl() {
return apiUrl;
}
}
4. 编写服务类
创建一个服务类来封装调用 DeepSeek API 的逻辑:
import com.google.gson.Gson;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Service
public class DeepSeekService {
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
private final OkHttpClient client = new OkHttpClient();
private final Gson gson = new Gson();
@Autowired
private DeepSeekConfig config;
public String callDeepSeekApi(String prompt) throws IOException {
// 构建请求体
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("prompt", prompt);
// 这里可以根据 API 文档添加更多参数,如 temperature、max_tokens 等
requestBody.put("temperature", 0.7);
requestBody.put("max_tokens", 200);
String json = gson.toJson(requestBody);
RequestBody body = RequestBody.create(json, JSON);
// 构建请求
Request request = new Request.Builder()
.url(config.getApiUrl())
.addHeader("Authorization", "Bearer " + config.getApiKey())
.post(body)
.build();
// 发送请求并获取响应
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return response.body().string();
}
}
}
5. 编写控制器类
创建一个控制器类来处理客户端的请求,并调用 DeepSeek 服务:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
public class DeepSeekController {
@Autowired
private DeepSeekService deepSeekService;
@GetMapping("/deepseek")
public String callDeepSeek(@RequestParam String prompt) {
try {
return deepSeekService.callDeepSeekApi(prompt);
} catch (IOException e) {
e.printStackTrace();
return "Error calling DeepSeek API: " + e.getMessage();
}
}
}
6. 配置文件
在 application.properties 中添加 DeepSeek 的 API 相关配置:
deepseek.api.key=your_api_key
deepseek.api.url=your_api_url
请将 your_api_key 替换为你实际的 API Key,your_api_url 替换为 DeepSeek 提供的 API 地址。
7. 测试
启动 Spring Boot 应用程序,访问 http://localhost:8080/deepseek?prompt=生成一个 Java 方法,用于计算两个整数的和
,你将得到 DeepSeek 返回的响应结果。
以上示例代码展示了如何在 Spring Boot 项目中接入 DeepSeek Coder 的 API,你可以根据实际需求调整请求参数和处理逻辑。