当前位置: 首页 > article >正文

调用deepseek接口

首次调用

http请求

curl https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <DeepSeek API Key>" \
  -d '{
        "model": "deepseek-chat",
        "messages": [
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"}
        ],
        "stream": false
      }'

 {"role": "system", "content": "You are a helpful assistant."}

代码 

    @Test
    public void deepSeekApiOne() {
        // DeepSeek API Key
        String apiKey = "sk-87553aba7e5b4554bfd773cd3056414d";

        // 创建 OkHttpClient 实例并设置超时时间
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                .build();

        String systemRoleContent = "You are a helpful assistant.";
        String userRoleContent = "介绍一下魔兽争霸";

        // 使用 Gson 构建请求体
        JsonObject requestBodyJson = new JsonObject();
        requestBodyJson.addProperty("model", "deepseek-chat");

        JsonArray messagesArray = new JsonArray();
        JsonObject systemMessage = new JsonObject();
        systemMessage.addProperty("role", "system");
        systemMessage.addProperty("content", systemRoleContent);
        messagesArray.add(systemMessage);

        JsonObject userMessage = new JsonObject();
        userMessage.addProperty("role", "user");
        userMessage.addProperty("content", userRoleContent);
        messagesArray.add(userMessage);

        requestBodyJson.add("messages", messagesArray);
        requestBodyJson.addProperty("stream", false);

        Gson gson = new Gson();
        String jsonBody = gson.toJson(requestBodyJson);

        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, jsonBody);

        // 构建请求
        Request request = new Request.Builder()
                .url("https://api.deepseek.com/chat/completions")
                .post(body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + apiKey)
                .build();

        int retryCount = 0;
        while (retryCount < MAX_RETRIES) {
            try (Response response = client.newCall(request).execute()) {
                if (response.isSuccessful()) {
                    String responseBody = response.body().string();
                    System.out.println("响应结果: " + responseBody);
                    return;
                } else {
                    System.out.println("请求失败,状态码: " + response.code());
                    String errorBody = response.body() != null ? response.body().string() : "无错误响应内容";
                    System.out.println("响应内容: " + errorBody);
                }
            } catch (IOException e) {
                System.out.println("请求因异常失败: " + e.getMessage());
                if (retryCount < MAX_RETRIES - 1) {
                    System.out.println("正在重试请求... 第 " + (retryCount + 2) + " 次,共 " + MAX_RETRIES + " 次尝试");
                }
            }
            retryCount++;
        }
        System.out.println("已达到最大重试次数,请求失败。");
    }
}


http://www.kler.cn/a/554110.html

相关文章:

  • ⭐ Unity 横向滑动列表 首尾相连 轮转图
  • Rust 面试题
  • 机器学习·数据处理
  • 【Python爬虫(17)】突破爬虫IP限制,解锁数据抓取新姿势
  • 【Scrapy】Scrapy教程4——命令行工具
  • 实现一个专注应用-后端开发(一)-搭建
  • QML Image 圆角设置
  • 从猜想终结到算法革新,弹性哈希开启数据存储新篇章
  • docker run --ipc=host参数含义
  • UniApp 面试题 超基础
  • C++效率掌握之STL库:vector函数全解
  • ubuntu 创建交换分区 或者扩容交换分区
  • 鸿蒙中,UIAbility组件启动模式(3种分别是Singleton(单实例模式)Multiton(多实例模式)Specified(指定实例模式))
  • Python常见面试题的详解13
  • 解决 Nginx 代理后 HTTP 头部丢失的问题:以 access_token 为例
  • 【ISO 14229-1:2023 UDS诊断(会话控制0x10服务)测试用例CAPL代码全解析⑤】
  • rust学习五、认识所有权
  • unity学习47:寻路和导航,unity2022后版本如何使用 Navmesh 和 bake
  • 图解MySQL【日志】——Buffer Pool
  • Java Applet 学习笔记(详细版)