当前位置: 首页 > 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("已达到最大重试次数,请求失败。");
    }
}

http://www.dtcms.com/a/25609.html

相关文章:

  • 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语言实现
  • MySQL5.7 创建用户并授予超管权限脚本
  • uni-app发起网络请求的三种方式
  • 探讨如何加快 C# 双循环的速度效率
  • 【route】route add命令详解
  • 记一次一波三折的众测SRC经历
  • DevOps自动化部署详解:从理念到实践
  • DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方DeepSeek接入)
  • 从零搭建微服务项目Base(第5章——SpringBoot项目LogBack日志配置+Feign使用)
  • 开源AI智能名片2+1链动模式S2B2C商城小程序在社交价值挖掘中的应用与策略研究
  • Linux 网络安全技巧