SoFunction
Updated on 2025-04-13

Project Practice of Springboot Integration Deepseek4j

Deepseek4j starts quickly

Maven dependencies

In yourAdd the following dependencies to:

<dependency>
  <groupId></groupId>
  <artifactId>deepseek-spring-boot-starter</artifactId>
  <version>1.4.2</version>
</dependency>

Basic configuration

existorAdd necessary configurations to:

deepseek:
  api-key: your-api-key-here  # Required: Your API key  base-url:   # Optional, default to the official API address  log-requests: true  # Optional, whether to record the request log  log-responses: true  # Optional, whether to record the response log  
  # Optional timeout configuration (unit: seconds)  connect-timeout: 10
  read-timeout: 30
  call-timeout: 60
  
  # Agent configuration (optional)  proxy:
    host: 
    port: 8080
    
  # Log level configuration (optional)  log-level: BASIC

Basic usage examples

1. Streaming return example

@Autowired
private DeepSeekClient deepSeekClient;

@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatCompletionResponse> chat(String prompt) {
    return (prompt);
}

2. Advanced configuration example

@GetMapping(value = "/chat/advanced", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux&lt;ChatCompletionResponse&gt; chatAdvanced(String prompt) {
    ChatCompletionRequest request = ()
        // Model selection, support DEEPSEEK_CHAT, DEEPSEEK_REASONER, etc.        .model(ChatCompletionModel.DEEPSEEK_REASONER)
        // Add user message        .addUserMessage(prompt)
        // Add assistant message for multiple rounds of conversations        .addAssistantMessage("Last round of results")
        // Add system messages to set roles and behaviors        .addSystemMessage("You are a professional assistant")
        // Set the maximum number of generated tokens, default 2048        .maxTokens(1000)
        // Set response format, support JSON structured output        .responseFormat(...) // Optional        // function calling
        .tools(...) // Optional        .build();
        
    return (request);
}

3. Synchronous output (non-real-time response stream)

Synchronous blocking call method is not recommended. The long time to reasoning of R1 model can easily cause client connection timeout, and response delay will affect user experience.

@GetMapping(value = "/sync/chat")
public ChatCompletionResponse syncChat(String prompt) {
  ChatCompletionRequest request = ()
      // Dynamically modify this parameter according to the channel model name      .model(())
      .addUserMessage(prompt).build();

  return (request).execute();
}

This is the end of this article about the project practice of springboot integration Deepseek4j. For more related content on springboot integration Deepseek4j, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!