远方蔚蓝
一刹那情真,相逢不如不见

文章数量 127

访问次数 332454

运行天数 1629

最近活跃 2025-03-05 16:28:15

进入后台管理系统

一个简单的java调用DeepSeek示例


1、添加pom依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.83</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.squareup.okhttp3</groupId>
  8. <artifactId>okhttp</artifactId>
  9. <version>3.14.9</version>
  10. </dependency>
  1. import java.io.IOException;
  2. import java.util.concurrent.TimeUnit;
  3. import com.alibaba.fastjson.JSONObject;
  4. import okhttp3.MediaType;
  5. import okhttp3.OkHttpClient;
  6. import okhttp3.Request;
  7. import okhttp3.RequestBody;
  8. import okhttp3.Response;
  9. import okhttp3.ResponseBody;
  10. /**
  11. *
  12. * @author wst 2025年3月5日 上午11:28:15
  13. *
  14. */
  15. public class DeepSeekApp {
  16. private static final String API_URL = "https://api.deepseek.com/chat/completions";
  17. private static final String API_KEY = "key **************";
  18. public static void main(String[] args) throws IOException {
  19. // 报文体
  20. DeepSeekChatBody deepSeekChatBody = new DeepSeekChatBody();
  21. DeepSeekMessage message = new DeepSeekMessage();
  22. message.setContent("你是谁");
  23. DeepSeekMessage[] messages = new DeepSeekMessage[] { message };
  24. deepSeekChatBody.setMessages(messages);
  25. String requestBody = JSONObject.toJSONString(deepSeekChatBody);
  26. System.out.println("报文:" + requestBody);
  27. // 请求
  28. OkHttpClient client = new OkHttpClient()
  29. .newBuilder() // -
  30. .connectTimeout(60, TimeUnit.SECONDS) // -
  31. .readTimeout(120, TimeUnit.SECONDS) // -
  32. .writeTimeout(120, TimeUnit.SECONDS) // -
  33. .build();
  34. MediaType mediaType = MediaType.parse("application/json");
  35. RequestBody reqBody = RequestBody.create(mediaType, requestBody);
  36. Request request = new Request.Builder()
  37. .url(API_URL).method("POST", reqBody) // -
  38. .addHeader("Content-Type", "application/json") // -
  39. .addHeader("Accept", "application/json") //
  40. .addHeader("Authorization", "Bearer " + API_KEY).build();
  41. Response response = client.newCall(request).execute();
  42. System.out.println(response.code());
  43. System.out.println(response.message());
  44. ResponseBody respBody = response.body();
  45. // 解析
  46. String bodyStr = respBody.string();
  47. System.out.println("响应:" + bodyStr);
  48. JSONObject jobj = JSONObject.parseObject(bodyStr);
  49. Object[] choices = jobj.getObject("choices", Object[].class);
  50. String content = JSONObject.parseObject(choices[0].toString()) // -
  51. .getJSONObject("message").getString("content");
  52. System.out.println("回答:" + content);
  53. }
  54. }
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. /**
  4. *
  5. * @author wst 2025年3月5日 上午11:28:11
  6. *
  7. */
  8. public class DeepSeekChatBody {
  9. private String model = "deepseek-chat";
  10. private int frequency_penalty = 0;
  11. private int max_tokens = 2048;
  12. private int presence_penalty = 0;
  13. private Map<String, String> response_format = new HashMap<String, String>();
  14. private Object stop = null;
  15. private boolean stream = false;
  16. private Object stream_options = null;
  17. private int temperature = 1;
  18. private int top_p = 1;
  19. private Object tools = null;
  20. private String tool_choice = "none";
  21. private boolean logprobs = false;
  22. private Object top_logprobs = null;
  23. /**
  24. * 发送的消息数组
  25. */
  26. private DeepSeekMessage[] messages;
  27. public DeepSeekChatBody() {
  28. super();
  29. response_format.put("type", "text");
  30. }
  31. public String getModel() {
  32. return model;
  33. }
  34. public void setModel(String model) {
  35. this.model = model;
  36. }
  37. public DeepSeekMessage[] getMessages() {
  38. return messages;
  39. }
  40. public void setMessages(DeepSeekMessage[] messages) {
  41. this.messages = messages;
  42. }
  43. public int getFrequency_penalty() {
  44. return frequency_penalty;
  45. }
  46. public void setFrequency_penalty(int frequency_penalty) {
  47. this.frequency_penalty = frequency_penalty;
  48. }
  49. public int getMax_tokens() {
  50. return max_tokens;
  51. }
  52. public void setMax_tokens(int max_tokens) {
  53. this.max_tokens = max_tokens;
  54. }
  55. public int getPresence_penalty() {
  56. return presence_penalty;
  57. }
  58. public void setPresence_penalty(int presence_penalty) {
  59. this.presence_penalty = presence_penalty;
  60. }
  61. public Map<String, String> getResponse_format() {
  62. return response_format;
  63. }
  64. public void setResponse_format(Map<String, String> response_format) {
  65. this.response_format = response_format;
  66. }
  67. public Object getStop() {
  68. return stop;
  69. }
  70. public void setStop(Object stop) {
  71. this.stop = stop;
  72. }
  73. public boolean isStream() {
  74. return stream;
  75. }
  76. public void setStream(boolean stream) {
  77. this.stream = stream;
  78. }
  79. public Object getStream_options() {
  80. return stream_options;
  81. }
  82. public void setStream_options(Object stream_options) {
  83. this.stream_options = stream_options;
  84. }
  85. public int getTemperature() {
  86. return temperature;
  87. }
  88. public void setTemperature(int temperature) {
  89. this.temperature = temperature;
  90. }
  91. public int getTop_p() {
  92. return top_p;
  93. }
  94. public void setTop_p(int top_p) {
  95. this.top_p = top_p;
  96. }
  97. public Object getTools() {
  98. return tools;
  99. }
  100. public void setTools(Object tools) {
  101. this.tools = tools;
  102. }
  103. public String getTool_choice() {
  104. return tool_choice;
  105. }
  106. public void setTool_choice(String tool_choice) {
  107. this.tool_choice = tool_choice;
  108. }
  109. public boolean isLogprobs() {
  110. return logprobs;
  111. }
  112. public void setLogprobs(boolean logprobs) {
  113. this.logprobs = logprobs;
  114. }
  115. public Object getTop_logprobs() {
  116. return top_logprobs;
  117. }
  118. public void setTop_logprobs(Object top_logprobs) {
  119. this.top_logprobs = top_logprobs;
  120. }
  121. }
  1. /**
  2. *
  3. * @author wst 2025年3月5日 上午11:29:15
  4. *
  5. */
  6. public class DeepSeekMessage {
  7. private String role = "system";
  8. private String content;
  9. public String getRole() {
  10. return role;
  11. }
  12. public void setRole(String role) {
  13. this.role = role;
  14. }
  15. public String getContent() {
  16. return content;
  17. }
  18. public void setContent(String content) {
  19. this.content = content;
  20. }
  21. }