调用deepseek接口

[复制链接]
发表于 2025-10-29 07:44:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
初次调用

http哀求
  1. curl https://api.deepseek.com/chat/completions \
  2.   -H "Content-Type: application/json" \
  3.   -H "Authorization: Bearer <DeepSeek API Key>" \
  4.   -d '{
  5.         "model": "deepseek-chat",
  6.         "messages": [
  7.           {"role": "system", "content": "You are a helpful assistant."},
  8.           {"role": "user", "content": "Hello!"}
  9.         ],
  10.         "stream": false
  11.       }'
复制代码
 {"role": "system", "content": "You are a helpful assistant."}

代码 
  1.     @Test
  2.     public void deepSeekApiOne() {
  3.         // DeepSeek API Key
  4.         String apiKey = "sk-87553aba7e5b4554bfd773cd3056414d";
  5.         // 创建 OkHttpClient 实例并设置超时时间
  6.         OkHttpClient client = new OkHttpClient.Builder()
  7.                 .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
  8.                 .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
  9.                 .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
  10.                 .build();
  11.         String systemRoleContent = "You are a helpful assistant.";
  12.         String userRoleContent = "介绍一下魔兽争霸";
  13.         // 使用 Gson 构建请求体
  14.         JsonObject requestBodyJson = new JsonObject();
  15.         requestBodyJson.addProperty("model", "deepseek-chat");
  16.         JsonArray messagesArray = new JsonArray();
  17.         JsonObject systemMessage = new JsonObject();
  18.         systemMessage.addProperty("role", "system");
  19.         systemMessage.addProperty("content", systemRoleContent);
  20.         messagesArray.add(systemMessage);
  21.         JsonObject userMessage = new JsonObject();
  22.         userMessage.addProperty("role", "user");
  23.         userMessage.addProperty("content", userRoleContent);
  24.         messagesArray.add(userMessage);
  25.         requestBodyJson.add("messages", messagesArray);
  26.         requestBodyJson.addProperty("stream", false);
  27.         Gson gson = new Gson();
  28.         String jsonBody = gson.toJson(requestBodyJson);
  29.         MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  30.         RequestBody body = RequestBody.create(JSON, jsonBody);
  31.         // 构建请求
  32.         Request request = new Request.Builder()
  33.                 .url("https://api.deepseek.com/chat/completions")
  34.                 .post(body)
  35.                 .addHeader("Content-Type", "application/json")
  36.                 .addHeader("Authorization", "Bearer " + apiKey)
  37.                 .build();
  38.         int retryCount = 0;
  39.         while (retryCount < MAX_RETRIES) {
  40.             try (Response response = client.newCall(request).execute()) {
  41.                 if (response.isSuccessful()) {
  42.                     String responseBody = response.body().string();
  43.                     System.out.println("响应结果: " + responseBody);
  44.                     return;
  45.                 } else {
  46.                     System.out.println("请求失败,状态码: " + response.code());
  47.                     String errorBody = response.body() != null ? response.body().string() : "无错误响应内容";
  48.                     System.out.println("响应内容: " + errorBody);
  49.                 }
  50.             } catch (IOException e) {
  51.                 System.out.println("请求因异常失败: " + e.getMessage());
  52.                 if (retryCount < MAX_RETRIES - 1) {
  53.                     System.out.println("正在重试请求... 第 " + (retryCount + 2) + " 次,共 " + MAX_RETRIES + " 次尝试");
  54.                 }
  55.             }
  56.             retryCount++;
  57.         }
  58.         System.out.println("已达到最大重试次数,请求失败。");
  59.     }
  60. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表