1、添加pom依赖
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version></dependency><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.14.9</version></dependency>
import java.io.IOException;import java.util.concurrent.TimeUnit;import com.alibaba.fastjson.JSONObject;import okhttp3.MediaType;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.RequestBody;import okhttp3.Response;import okhttp3.ResponseBody;/**** @author wst 2025年3月5日 上午11:28:15**/public class DeepSeekApp {private static final String API_URL = "https://api.deepseek.com/chat/completions";private static final String API_KEY = "key **************";public static void main(String[] args) throws IOException {// 报文体DeepSeekChatBody deepSeekChatBody = new DeepSeekChatBody();DeepSeekMessage message = new DeepSeekMessage();message.setContent("你是谁");DeepSeekMessage[] messages = new DeepSeekMessage[] { message };deepSeekChatBody.setMessages(messages);String requestBody = JSONObject.toJSONString(deepSeekChatBody);System.out.println("报文:" + requestBody);// 请求OkHttpClient client = new OkHttpClient().newBuilder() // -.connectTimeout(60, TimeUnit.SECONDS) // -.readTimeout(120, TimeUnit.SECONDS) // -.writeTimeout(120, TimeUnit.SECONDS) // -.build();MediaType mediaType = MediaType.parse("application/json");RequestBody reqBody = RequestBody.create(mediaType, requestBody);Request request = new Request.Builder().url(API_URL).method("POST", reqBody) // -.addHeader("Content-Type", "application/json") // -.addHeader("Accept", "application/json") //.addHeader("Authorization", "Bearer " + API_KEY).build();Response response = client.newCall(request).execute();System.out.println(response.code());System.out.println(response.message());ResponseBody respBody = response.body();// 解析String bodyStr = respBody.string();System.out.println("响应:" + bodyStr);JSONObject jobj = JSONObject.parseObject(bodyStr);Object[] choices = jobj.getObject("choices", Object[].class);String content = JSONObject.parseObject(choices[0].toString()) // -.getJSONObject("message").getString("content");System.out.println("回答:" + content);}}
import java.util.HashMap;import java.util.Map;/**** @author wst 2025年3月5日 上午11:28:11**/public class DeepSeekChatBody {private String model = "deepseek-chat";private int frequency_penalty = 0;private int max_tokens = 2048;private int presence_penalty = 0;private Map<String, String> response_format = new HashMap<String, String>();private Object stop = null;private boolean stream = false;private Object stream_options = null;private int temperature = 1;private int top_p = 1;private Object tools = null;private String tool_choice = "none";private boolean logprobs = false;private Object top_logprobs = null;/*** 发送的消息数组*/private DeepSeekMessage[] messages;public DeepSeekChatBody() {super();response_format.put("type", "text");}public String getModel() {return model;}public void setModel(String model) {this.model = model;}public DeepSeekMessage[] getMessages() {return messages;}public void setMessages(DeepSeekMessage[] messages) {this.messages = messages;}public int getFrequency_penalty() {return frequency_penalty;}public void setFrequency_penalty(int frequency_penalty) {this.frequency_penalty = frequency_penalty;}public int getMax_tokens() {return max_tokens;}public void setMax_tokens(int max_tokens) {this.max_tokens = max_tokens;}public int getPresence_penalty() {return presence_penalty;}public void setPresence_penalty(int presence_penalty) {this.presence_penalty = presence_penalty;}public Map<String, String> getResponse_format() {return response_format;}public void setResponse_format(Map<String, String> response_format) {this.response_format = response_format;}public Object getStop() {return stop;}public void setStop(Object stop) {this.stop = stop;}public boolean isStream() {return stream;}public void setStream(boolean stream) {this.stream = stream;}public Object getStream_options() {return stream_options;}public void setStream_options(Object stream_options) {this.stream_options = stream_options;}public int getTemperature() {return temperature;}public void setTemperature(int temperature) {this.temperature = temperature;}public int getTop_p() {return top_p;}public void setTop_p(int top_p) {this.top_p = top_p;}public Object getTools() {return tools;}public void setTools(Object tools) {this.tools = tools;}public String getTool_choice() {return tool_choice;}public void setTool_choice(String tool_choice) {this.tool_choice = tool_choice;}public boolean isLogprobs() {return logprobs;}public void setLogprobs(boolean logprobs) {this.logprobs = logprobs;}public Object getTop_logprobs() {return top_logprobs;}public void setTop_logprobs(Object top_logprobs) {this.top_logprobs = top_logprobs;}}
/**** @author wst 2025年3月5日 上午11:29:15**/public class DeepSeekMessage {private String role = "system";private String content;public String getRole() {return role;}public void setRole(String role) {this.role = role;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}