李锋镝的博客

  • 首页
  • 时间轴
  • 留言
  • 插件
  • 左邻右舍
  • 关于我
    • 关于我
    • 另一个网站
  • 知识库
  • 赞助
Destiny
自是人生长恨水长东
  1. 首页
  2. 原创
  3. 正文

Springboot接入DeepSeek API

2025年2月16日 250点热度 0人点赞 0条评论

1. 准备工作

注册并获取 API Key:你需要在 DeepSeek 平台注册账号,并获取 API Key,这是调用 API 的凭证。
创建 Spring Boot 项目:可以使用 Spring Initializr(https://start.spring.io/)创建一个新的 Spring Boot 项目,添加 Spring Web 依赖。

2. 添加依赖

在 pom.xml 中添加必要的依赖,这里使用 OkHttp 作为 HTTP 客户端来发送请求:

<dependencies>
    <!-- Spring Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- OkHttp -->
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.9.3</version>
    </dependency>
    <!-- JSON 处理 -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.8</version>
    </dependency>
</dependencies>

3. 编写配置类

创建一个配置类来存储 DeepSeek 的 API 相关信息,如 API Key 和 API 地址:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeepSeekConfig {
    @Value("${deepseek.api.key}")
    private String apiKey;

    @Value("${deepseek.api.url}")
    private String apiUrl;

    public String getApiKey() {
        return apiKey;
    }

    public String getApiUrl() {
        return apiUrl;
    }
}

4. 编写服务类

创建一个服务类来封装调用 DeepSeek API 的逻辑:

import com.google.gson.Gson;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class DeepSeekService {
    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
    private final OkHttpClient client = new OkHttpClient();
    private final Gson gson = new Gson();

    @Autowired
    private DeepSeekConfig config;

    public String callDeepSeekApi(String prompt) throws IOException {
        // 构建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("prompt", prompt);
        // 这里可以根据 API 文档添加更多参数,如 temperature、max_tokens 等
        requestBody.put("temperature", 0.7);
        requestBody.put("max_tokens", 200);

        String json = gson.toJson(requestBody);
        RequestBody body = RequestBody.create(json, JSON);

        // 构建请求
        Request request = new Request.Builder()
               .url(config.getApiUrl())
               .addHeader("Authorization", "Bearer " + config.getApiKey())
               .post(body)
               .build();

        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            return response.body().string();
        }
    }
}

5. 编写控制器类

创建一个控制器类来处理客户端的请求,并调用 DeepSeek 服务:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class DeepSeekController {
    @Autowired
    private DeepSeekService deepSeekService;

    @GetMapping("/deepseek")
    public String callDeepSeek(@RequestParam String prompt) {
        try {
            return deepSeekService.callDeepSeekApi(prompt);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error calling DeepSeek API: " + e.getMessage();
        }
    }
}

6. 配置文件

在 application.properties 中添加 DeepSeek 的 API 相关配置:

deepseek.api.key=your_api_key
deepseek.api.url=your_api_url

请将 your_api_key 替换为你实际的 API Key,your_api_url 替换为 DeepSeek 提供的 API 地址。

7. 测试

启动 Spring Boot 应用程序,访问 http://localhost:8080/deepseek?prompt=生成一个 Java 方法,用于计算两个整数的和,你将得到 DeepSeek 返回的响应结果。
以上示例代码展示了如何在 Spring Boot 项目中接入 DeepSeek Coder 的 API,你可以根据实际需求调整请求参数和处理逻辑。

除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/archives/article/4194

相关文章

  • 本地部署 DeepSeek 模型并进行 Spring Boot 整合
  • SpringBoot常用注解
  • CompletableFuture使用详解
  • 金融级JVM深度调优实战的经验和技巧
  • SpringBoot 中内置的 49 个常用工具类
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: AI DeepSeek
最后更新:2025年2月16日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >

文章评论

1 2 3 4 5 6 7 8 9 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 55 57 58 60 61 62 63 64 65 66 67 69 72 74 76 77 78 79 80 81 82 85 86 87 90 92 93 94 95 96 97 98 99
取消回复

既然选择了远方,便只顾风雨兼程。

最新 热点 随机
最新 热点 随机
SpringBoot框架自动配置之spring.factories和AutoConfiguration.imports 应用型负载均衡(ALB)和网络型负载均衡(NLB)区别 什么是Helm? TransmittableThreadLocal介绍与使用 ReentrantLock深度解析 RedisTemplate和Redisson的区别
玩博客的人是不是越来越少了?准备入手个亚太的ECS,友友们有什么建议吗?什么是Helm?2024年11月1号 农历十月初一别再背线程池的七大参数了,现在面试官都这么问URL地址末尾加不加“/”有什么区别
TransmittableThreadLocal介绍与使用 新买的笔记本发货啦…… JAVA技术点总结一 从零搭建Spring Cloud Gateway网关(一) GitLab创建新项目,初次提交命令和流程 如何高效的学习技术?
标签聚合
日常 数据库 文学 JVM Spring MySQL JAVA 架构 Redis 分布式 教程 多线程 面试 K8s SQL IDEA docker SpringBoot ElasticSearch 设计模式
友情链接
  • i架构
  • 临窗旋墨
  • 博友圈
  • 博客录
  • 博客星球
  • 哥斯拉
  • 志文工作室
  • 搬砖日记
  • 旋律的博客
  • 旧时繁华
  • 林羽凡
  • 知向前端
  • 蜗牛工作室
  • 集博栈
  • 韩小韩博客
  • 風の声音

COPYRIGHT © 2025 lifengdi.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Dylan

津ICP备2024022503号-3