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

调用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("已达到最大重试次数,请求失败。");
    }
}

相关文章:

  • DeepSeek接入大数据能做什么
  • c# —— StringBuilder 类
  • three.js+WebGL踩坑经验合集(8.2):z-fighting叠面问题和camera.near的坑爹关系
  • C语言:在主函数中输入十个等长的字符串。用另一函数对它们排序,然后在主函数输出这10个已排好序的字符串。
  • 数据结构-栈、队列、哈希表
  • PyTorch与TensorFlow的对比:哪个框架更适合你的项目?
  • 什么是动态IP?静态IP和动态IP有什么区别?
  • C++中std::condition_variable_any、std::lock_guard 和 std::unique_
  • UE5控件组件显示UMG文本不正常
  • 1、AI量化学习资料 - 用DEEPSEEK玩转PTrade策略开发.zip\AI量化学习资料 - 1、PTrade策略开发提示词(参考模板).md
  • SpringBoot速成(14)文件上传P23-P26
  • 【JAVA实战】JAVA实现Excel模板下载并填充模板下拉选项数据
  • 【C++】36.C++IO流
  • 级联选择器多选动态加载
  • 洛谷P11042 [蓝桥杯 2024 省 Java B] 类斐波那契循环数
  • Linux系统配置阿里云yum源,安装docker
  • Step-Video-T2V:阶跃星辰发布最强开源视频生成模型(论文详解)
  • 《深度学习》——ResNet网络
  • 单纯禁用Cookie能否保证隐私安全?
  • 跳表的C语言实现
  • 解放日报:“感觉全世界人都在上海买买买”
  • 复旦建校120周年大型义诊举行,百余名专家服务市民超三千人次
  • 马上评|科学红毯,让科学家成为“最亮的星”
  • 上海国际珠宝时尚功能区未来三年如何建设?六大行动将开展
  • 网易有道一季度净利润同比增长247%:有能力在今年实现更强劲的利润增长
  • 讲座|消逝之钟:《红楼梦》与《布登勃洛克一家》中的时间观