SpringBoot调用DeepSeek API的完整操作指南

 更新时间:2025年02月10日 08:17:48   作者:老大白菜  
这篇文章主要为大家详细介绍了SpringBoot调用DeepSeek API的完整操作指南,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

1. 项目依赖

在 pom.xml 中添加以下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>

2. 项目结构

deepseek-project/
├── src/main/java/com/example/deepseek/
│   ├── DeepSeekApplication.java
│   ├── config/
│   │   └── DeepSeekConfig.java
│   ├── model/
│   │   ├── ChatRequest.java
│   │   ├── ChatResponse.java
│   │   └── Message.java
│   └── service/
│       └── DeepSeekService.java
└── conversation.txt

3. 完整代码实现

3.1 配置类 DeepSeekConfig.java

package com.example.deepseek.config;

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

@Configuration
@Getter
public class DeepSeekConfig {
    @Value("${deepseek.api.url}")
    private String apiUrl;

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

3.2 请求/响应模型

Message.java:

package com.example.deepseek.model;

import lombok.Data;

@Data
public class Message {
    private String role;
    private String content;
}

ChatRequest.java:

package com.example.deepseek.model;

import lombok.Data;
import java.util.List;

@Data
public class ChatRequest {
    private String model = "deepseek-ai/DeepSeek-V3";
    private List<Message> messages;
    private boolean stream = true;
    private int max_tokens = 2048;
    private double temperature = 0.7;
    private double top_p = 0.7;
    private int top_k = 50;
    private double frequency_penalty = 0.5;
    private int n = 1;
    private ResponseFormat response_format = new ResponseFormat("text");

    @Data
    public static class ResponseFormat {
        private String type;
        
        public ResponseFormat(String type) {
            this.type = type;
        }
    }
}

ChatResponse.java:

package com.example.deepseek.model;

import lombok.Data;
import java.util.List;

@Data
public class ChatResponse {
    private List<Choice> choices;

    @Data
    public static class Choice {
        private Delta delta;
    }

    @Data
    public static class Delta {
        private String content;
    }
}

3.3 服务类 DeepSeekService.java

package com.example.deepseek.service;

import com.example.deepseek.config.DeepSeekConfig;
import com.example.deepseek.model.ChatRequest;
import com.example.deepseek.model.ChatResponse;
import com.example.deepseek.model.Message;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Scanner;

@Service
@RequiredArgsConstructor
public class DeepSeekService {
    private final DeepSeekConfig config;
    private final WebClient.Builder webClientBuilder;
    private final ObjectMapper objectMapper = new ObjectMapper();

    public void startInteractiveChat() {
        try (Scanner scanner = new Scanner(System.in);
             PrintWriter fileWriter = new PrintWriter(new FileWriter("conversation.txt", true))) {

            while (true) {
                System.out.print("\n请输入您的问题 (输入 q 退出): ");
                String question = scanner.nextLine().trim();

                if ("q".equalsIgnoreCase(question)) {
                    System.out.println("程序已退出");
                    break;
                }

                // 保存问题
                saveToFile(fileWriter, question, true);

                // 发起对话请求
                Flux<String> responseFlux = sendChatRequest(question);

                StringBuilder fullResponse = new StringBuilder();
                responseFlux
                    .doOnNext(chunk -> {
                        System.out.print(chunk);
                        fullResponse.append(chunk);
                    })
                    .doOnComplete(() -> {
                        // 保存完整回复
                        saveToFile(fileWriter, fullResponse.toString(), false);
                        System.out.println("\n----------------------------------------");
                        fileWriter.println("\n----------------------------------------");
                        fileWriter.flush();
                    })
                    .blockLast();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Flux<String> sendChatRequest(String question) {
        ChatRequest request = new ChatRequest();
        Message userMessage = new Message();
        userMessage.setRole("user");
        userMessage.setContent(question);
        request.setMessages(Collections.singletonList(userMessage));

        return webClientBuilder.build()
            .post()
            .uri(config.getApiUrl())
            .header("Authorization", "Bearer " + config.getApiKey())
            .header("Content-Type", "application/json")
            .bodyValue(request)
            .retrieve()
            .bodyToFlux(String.class)
            .filter(line -> line.startsWith("data: ") && !line.equals("data: [DONE]"))
            .map(line -> {
                try {
                    String jsonStr = line.substring(6);
                    ChatResponse response = objectMapper.readValue(jsonStr, ChatResponse.class);
                    return response.getChoices().get(0).getDelta().getContent();
                } catch (Exception e) {
                    return "";
                }
            })
            .filter(content -> !content.isEmpty());
    }

    private void saveToFile(PrintWriter fileWriter, String content, boolean isQuestion) {
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (isQuestion) {
            fileWriter.printf("\n[%s] Question:\n%s\n\n[%s] Answer:\n", timestamp, content, timestamp);
        } else {
            fileWriter.print(content);
        }
        fileWriter.flush();
    }
}

3.4 主应用类 DeepSeekApplication.java

package com.example.deepseek;

import com.example.deepseek.service.DeepSeekService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DeepSeekApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DeepSeekApplication.class, args);
        DeepSeekService deepSeekService = context.getBean(DeepSeekService.class);
        deepSeekService.startInteractiveChat();
    }
}

3.5 配置文件 application.properties

deepseek.api.url=https://api.siliconflow.cn/v1/chat/completions
deepseek.api.key=YOUR_API_KEY

4. 代码详解

4.1 关键特性

使用 Spring WebFlux 的响应式编程模型

流式处理 API 响应

文件记录对话

错误处理和异常管理

4.2 主要组件

DeepSeekConfig: 管理 API 配置

DeepSeekService: 处理对话逻辑和 API 交互

模型类: 定义请求和响应结构

5. 使用方法

替换 application.properties 中的 YOUR_API_KEY

运行 DeepSeekApplication

在控制台输入问题

输入 ‘q’ 退出程序

查看 conversation.txt 获取对话记录

6. 性能和可扩展性

使用响应式编程提高并发性能

灵活的配置管理

易于扩展和定制

7. 注意事项

确保正确配置 API Key

处理网络异常

注意内存使用

总结

Spring Boot 实现提供了一个健壮、可扩展的 DeepSeek API 调用方案,利用响应式编程提供高效的流式对话体验。

到此这篇关于SpringBoot调用DeepSeek API的完整操作指南的文章就介绍到这了,更多相关SpringBoot调用DeepSeek API内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 通过Java实现中文分词与文本关键词提取

    通过Java实现中文分词与文本关键词提取

    这篇文章主要为大家详细介绍了如何利用Java实现中文分词以及文本关键词提取功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习学习
    2023-06-06
  • 基于ArrayList源码解析(基于JDK1.8)

    基于ArrayList源码解析(基于JDK1.8)

    这篇文章主要介绍了关于ArrayList源码解析(基于JDK1.8),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • logback TimeBasedRollingPolicy按天生成日志源码解析

    logback TimeBasedRollingPolicy按天生成日志源码解析

    这篇文章主要为大家介绍了logback TimeBasedRollingPolicy按天生成日志源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • 解决spring boot2集成activiti6踩过的坑

    解决spring boot2集成activiti6踩过的坑

    这篇文章主要介绍了解决spring boot2集成activiti6踩过的坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 不调用方法实现hutool导出excel图片示例详解

    不调用方法实现hutool导出excel图片示例详解

    这篇文章主要为大家介绍了不调用方法实现hutool导出excel图片示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • java基于包结构的请求路由实现实例分享

    java基于包结构的请求路由实现实例分享

    基于包结构的请求路由简单实现实例分享,大家参考使用吧
    2013-12-12
  • Feign 使用HttpClient和OkHttp方式

    Feign 使用HttpClient和OkHttp方式

    这篇文章主要介绍了Feign 使用HttpClient和OkHttp方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Java线程中Thread方法下的Join方法详解

    Java线程中Thread方法下的Join方法详解

    这篇文章主要介绍了Java线程中Thread方法下的Join方法详解,在项目中往往会遇到这样一个场景,就是需要等待几件事情都给做完后才能走下面的事情,这个时候就需要用到Thread方法下的Join方法,join方法是无参且没有返回值的,需要的朋友可以参考下
    2024-01-01
  • 基于SpringBoot实现防盗链功能

    基于SpringBoot实现防盗链功能

    防盗链是保护资源服务器的常用方法,旨在防止未经授权的外部链接直接访问服务器上的资源,如图片、音频和视频文件,在本文中,我们将探讨防盗链的概念和原理,并结合 Spring Boot 提供一个完整的可运行示例,需要的朋友可以参考下
    2024-12-12
  • java Socket实现网页版在线聊天

    java Socket实现网页版在线聊天

    这篇文章主要为大家详细介绍了java Socket实现网页版在线聊天具体代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05

最新评论