Integrate DeepSeek with Spring AI: Step‑by‑Step Guide for Java Developers
This guide walks Java developers through integrating DeepSeek’s large‑language model into Spring AI, covering environment setup, Maven dependencies, configuration, and sample controller code for both standard and streaming chat responses, including real‑time streaming.
Spring AI is a project in the Spring ecosystem that integrates AI into Spring applications, supporting major providers like Anthropic, OpenAI, Microsoft, Amazon, Google, and Ollama.
Spring AI connects your data and APIs with AI models.
Chat
Embedding
Text‑to‑Image
Audio‑to‑Text
Text‑to‑Audio
DeepSeek, developed by the Chinese team DeepSeek, is a multimodal large model with strong reasoning and code generation abilities, offering low cost and high performance.
Low cost (no special hardware, open‑source, simple usage).
High performance (strong inference, accurate answers).
Integration Steps
1. Environment Preparation
JDK 17 or higher
Maven or Gradle
DeepSeek API Key (obtain from https://platform.deepseek.com/usage)
2. Create Spring Boot Project
Use Spring Initializr to create a Spring Boot 3.2.x project.
3. Add Dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
</dependencies>4. Configuration
# Required
spring.ai.openai.api-key=your-api-key
spring.ai.openai.base-url=https://api.deepseek.com
# Model selection (example uses chat model)
spring.ai.openai.chat.options.model=deepseek-chat5. Write Code
Create a controller that uses DeepSeekClient for chat and streaming responses.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/chat")
public class ChatController {
@Autowired
private DeepSeekClient deepSeekClient;
@PostMapping
public String chat(@RequestBody String message) {
return deepSeekClient.chatCompletion(message).getOutput().getContent();
}
@GetMapping(value = "/stream", produces = "text/event-stream")
public Flux<String> chatStream(@RequestParam String message) {
return deepSeekClient.chatFluxCompletion(message)
.map(response -> response.getOutput().getContent());
}
}6. Streaming Output Considerations
Streaming reduces perceived latency by sending partial results; the backend must implement server‑sent events and the frontend must handle them.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
