1. Project dependency
Add the following dependencies in :
<dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies>
2. Project structure
deepseek-project/
├── src/main/java/com/example/deepseek/
│ ├──
│ ├── config/
│ │ └──
│ ├── model/
│ │ ├──
│ │ ├──
│ │ └──
│ └── service/
│ └──
└──
3. Complete code implementation
3.1 Configuration class
package ; import ; import ; @Configuration @Getter public class DeepSeekConfig { @Value("${}") private String apiUrl; @Value("${}") private String apiKey; }
3.2 Request/Response Model
:
package ; import ; @Data public class Message { private String role; private String content; }
:
package ; import ; import ; @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) { = type; } } }
:
package ; import ; import ; @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 Service category
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; @Service @RequiredArgsConstructor public class DeepSeekService { private final DeepSeekConfig config; private final webClientBuilder; private final ObjectMapper objectMapper = new ObjectMapper(); public void startInteractiveChat() { try (Scanner scanner = new Scanner(); PrintWriter fileWriter = new PrintWriter(new FileWriter("", true))) { while (true) { ("\nPlease enter your question (enter q quit): "); String question = ().trim(); if ("q".equalsIgnoreCase(question)) { ("Program Exited"); break; } // Save the problem saveToFile(fileWriter, question, true); // Initiate a dialogue request Flux<String> responseFlux = sendChatRequest(question); StringBuilder fullResponse = new StringBuilder(); responseFlux .doOnNext(chunk -> { (chunk); (chunk); }) .doOnComplete(() -> { // Save the full reply saveToFile(fileWriter, (), false); ("\n----------------------------------------"); ("\n----------------------------------------"); (); }) .blockLast(); } } catch (IOException e) { (); } } private Flux<String> sendChatRequest(String question) { ChatRequest request = new ChatRequest(); Message userMessage = new Message(); ("user"); (question); ((userMessage)); return () .post() .uri(()) .header("Authorization", "Bearer " + ()) .header("Content-Type", "application/json") .bodyValue(request) .retrieve() .bodyToFlux() .filter(line -> ("data: ") && !("data: [DONE]")) .map(line -> { try { String jsonStr = (6); ChatResponse response = (jsonStr, ); return ().get(0).getDelta().getContent(); } catch (Exception e) { return ""; } }) .filter(content -> !()); } private void saveToFile(PrintWriter fileWriter, String content, boolean isQuestion) { String timestamp = ().format(("yyyy-MM-dd HH:mm:ss")); if (isQuestion) { ("\n[%s] Question:\n%s\n\n[%s] Answer:\n", timestamp, content, timestamp); } else { (content); } (); } }
3.4 Main application class
package ; import ; import ; import ; import ; @SpringBootApplication public class DeepSeekApplication { public static void main(String[] args) { ConfigurableApplicationContext context = (, args); DeepSeekService deepSeekService = (); (); } }
3.5 Configuration File
=/v1/chat/completions =YOUR_API_KEY
4. Detailed code explanation
4.1 Key Features
Responsive programming model using Spring WebFlux
Streaming API Response
File record conversation
Error handling and exception management
4.2 Main components
DeepSeekConfig: Manage API Configuration
DeepSeekService: Handles dialogue logic and API interaction
Model Class: Define request and response structure
5. How to use
YOUR_API_KEY in Replace
Run DeepSeekApplication
Enter a question on the console
Enter ‘q’ to exit the program
View Get conversation history
6. Performance and scalability
Improve concurrency performance using responsive programming
Flexible configuration management
Easy to expand and customize
7. Things to note
Make sure the API Key is configured correctly
Handle network exceptions
Pay attention to memory usage
Summarize
Spring Boot implementations provide a robust, scalable DeepSeek API call scheme that utilizes responsive programming to provide an efficient streaming conversation experience.
This is the article about the complete operation guide for SpringBoot calling DeepSeek API. For more related content on SpringBoot calling DeepSeek API, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!