SpringBoot项目连接deepseek

目录

[6 连接deepseek](#6 连接deepseek)

[6.1 通用步骤](#6.1 通用步骤)

[6.2 SpringBoot项目里的步骤](#6.2 SpringBoot项目里的步骤)


6 连接deepseek

6.1 通用步骤

  1. 打开deepseek官网https://www.deepseek.com
  2. 点击API开放平台
  3. 点击用量信息去充值
  4. 充值完后点击API keys,然后创建API key

    创建成功后,要保存后弹框里的信息,因为关闭,就再也找不到了,除非删除,在重新创建
  5. 点击接口文档,找到对话,然后再最右边选择自己需要的语言。

    复制OKHTTP里面的内容。

6.2 SpringBoot项目里的步骤

  1. 导入相关依赖
java 复制代码
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.14.9</version>
</dependency>

2.复制并且修改下面的代码,修改第6行代码,把从sk开头的东西改为6.1里创建的API key。

java 复制代码
import okhttp3.*;
import java.io.IOException;

public class Utils {
    // 替换为你的 API Key
    private static final String API_KEY = "Bearer sk-666";
    private static final String API_URL = "https://api.deepseek.com/chat/completions";

    public static void main(String[] args) throws IOException {
        // 1. 创建请求体(JSON)
        String jsonBody = String.format("{\n"
                        + "  \"model\": \"deepseek-chat\",\n"
                        + "  \"messages\": [\n"
                        + "    {\"role\": \"user\", \"content\": \"%s\"}\n"
                        + "  ],\n"
                        + "  \"temperature\": 0.7,\n"
                        + "  \"top_p\": 0.8\n"
                        + "}", "你好");

        // 2. 创建 HTTP 请求
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.get("application/json"), jsonBody);

        Request request = new Request.Builder()
               .url(API_URL)
               .post(body)
               .addHeader("Content-Type", "application/json")
               .addHeader("Accept", "application/json")
               .addHeader("Authorization", API_KEY) // 直接使用 API Key
               .build();

        // 3. 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("请求失败: " + response);
            }
            System.out.println("响应结果:\n" + response.body().string());
        }
    }
}

此时运行会出现以下结果

只要把第18行代码的"你好",改成自己问的东西,就可以和得到deepseek对应的回答了。

上面这个代码要放在service层。示例如下

java 复制代码
package com.qcby.websocket.service.impl;


import com.qcby.websocket.service.DeepSeekService;
import okhttp3.*;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class DeepSeekServiceImpl implements DeepSeekService {
    // 替换为你的 API Key
    private static final String API_KEY = "Bearer sk-6666";
    private static final String API_URL = "https://api.deepseek.com/chat/completions";

    public  String getJson(String context) throws IOException {
        // 1. 创建请求体(JSON)
        String jsonBody = String.format("{\n"
                + "  \"model\": \"deepseek-chat\",\n"
                + "  \"messages\": [\n"
                + "    {\"role\": \"user\", \"content\": \"%s\"}\n"
                + "  ],\n"
                + "  \"temperature\": 0.7,\n"
                + "  \"top_p\": 0.8\n"
                + "}", context);
        // 2. 创建 HTTP 请求
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.get("application/json"), jsonBody);

        Request request = new Request.Builder()
                .url(API_URL)
                .post(body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .addHeader("Authorization", API_KEY) // 直接使用API Key
                .build();

        // 3. 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("请求失败: " + response);
            }
           return  response.body().string();
        }

    }

}
相关推荐
想不到一个好的ID2 小时前
Redis Cluster
后端
EatFan3 小时前
Spring Boot 4.0 升级清单:JDK 基线、Jakarta 命名空间、Observation API 与第三方组件兼容一次讲清
java·spring boot·后端
大大大大晴天3 小时前
从System.out到可观测性:Java 日志框架的演进与选型
后端
辰海Coding3 小时前
平台如何接入一台物联网设备?又该如何设计它的通信协议?
java·物联网
代码民工正电荷3 小时前
JAVA重要知识点和集合
java·开发语言
李少兄5 小时前
IDEA Apifox Helper 插件踩坑:2.0.15-243 版本“导出指定接口”参数描述丢失问题解析与修复方案
java·ide·intellij-idea
JavaGuide5 小时前
Gemini 杀疯了,反重力给我用爽了!分享 8 个 Antigravity 使用技巧
人工智能·后端
努力攀登的小k5 小时前
《新手练表:从建表到删表全操作|从零学会表操作》
java·数据库·mysql
想不到一个好的ID6 小时前
Redis 哨兵(Sentinel)
后端
Yetian_20006 小时前
Postman测试上传图片文件接口
java·postman