java调用DeepSeek接口
jdk 1.8以上的可以使用,java.net.http.HttpClient,这个类是在Java 9中引入的
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import org.json.JSONObject;
public class DeepSeekClient {
private String API_URL;
private String apiKey;
private String model;
private int maxTokens;
private double temperature;
private HttpClient httpClient;
public DeepSeekClient(String API_URL, String apiKey, String model, int maxTokens, double temperature, HttpClient httpClient) {
this.API_URL = API_URL;
this.apiKey = apiKey;
this.model = model;
this.maxTokens = maxTokens;
this.temperature = temperature;
this.httpClient = httpClient;
}
public DeepSeekClient(String apiKey, String model) {
this.apiKey = apiKey;
this.model = model;
this.API_URL = "https://api.deepseek.com/v1/chat/completions";
this.maxTokens = 1000;
// this.temperature = 0.7;
this.temperature = 1.5;
this.httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(10))
.build();
}
public String getAPI_URL() {
return API_URL;
}
public void setAPI_URL(String API_URL) {
this.API_URL = API_URL;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getMaxTokens() {
return maxTokens;
}
public void setMaxTokens(int maxTokens) {
this.maxTokens = maxTokens;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temperature) {
this.temperature = temperature;
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
/**
* 调用DeepSeek聊天API
* apiKey API密钥
* model 使用的模型名称
* maxTokens 最大返回token数
* temperature 生成温度系数
* @param prompt 用户输入的提示内容
* @return API响应内容
* @throws Exception 包含网络异常和API错误
*/
public String callDeepSeekAPI(String prompt) throws Exception {
// 构建请求体
String requestBody = String.format("{"
+ "\"model\": \"%s\","
+ "\"messages\": [{\"role\": \"user\", \"content\": \"%s\"}],"
+ "\"max_tokens\": %d,"
+ "\"temperature\": %f"
+ "}", model, prompt.replace("\"", "\\\""), maxTokens, temperature);
// 创建HTTP请求
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
// 发送请求并获取响应
HttpResponse<String> response = httpClient.send(
request,
HttpResponse.BodyHandlers.ofString()
);
// 检查响应状态
if (response.statusCode() != 200) {
throw new RuntimeException("API请求失败: " + response.body());
}
return response.body();
}
String parseResponse(String jsonResponse) {
JSONObject jsonObject = new JSONObject(jsonResponse);
// 获取choices数组中的第一个元素
JSONObject firstChoice = jsonObject.getJSONArray("choices").getJSONObject(0);
// 从第一个元素中获取message对象
JSONObject message = firstChoice.getJSONObject("message");
// 从message对象中获取content字段的值
String content = message.getString("content");
return content;
}
}
创建一个Demo测试一下
public class demo {
public static void main(String[] args) {
DeepSeekClient deepseekClient = new DeepSeekClient("你deepseek的apiKey", "deepseek-reasoner");
try {
String response = deepseekClient.callDeepSeekAPI("你是谁?100字以内回答");
System.out.println("API响应内容:");
System.out.println(deepseekClient.parseResponse(response));
} catch (Exception e) {
System.err.println("调用失败: " + e.getMessage());
}
}
}