Integrating DeepSeek Large Model with Spring AI: A Step-by-Step Guide
This article explains how to integrate DeepSeek's large language models into a Spring AI application, covering model selection, API key configuration, URL setup, dependency inclusion, and providing complete Java code examples for both synchronous and streaming chat interactions.
DeepSeek, a domestically developed large language model, offers two main series: the chat‑oriented deepseek-chat (V series) and the reasoning‑focused deepseek-reasoner (R series). The article introduces these models and points to the official update log for version history.
To use DeepSeek with Spring AI, first obtain an API key from the DeepSeek portal and configure the Spring properties spring.ai.openai.api-key , spring.ai.openai.base-url (set to api.deepseek.com ), and spring.ai.openai.chat.model with the desired model name.
Next, add the required Maven dependency:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>Configure the application.yml (or properties) with your key, base URL, and model:
spring:
ai:
openai:
api-key: sk-xxx # your key
base-url: https://api.deepseek.com
chat:
options:
model: deepseek-chatA simple REST controller demonstrates both blocking and streaming chat calls. The blocking endpoint returns a map with the generated text, while the streaming endpoint returns a Flux<ChatResponse> that streams partial responses.
package com.ivy.controller;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.util.Map;
@RestController
public class ChatController {
private final OpenAiChatModel chatModel;
public ChatController(OpenAiChatModel chatModel) { this.chatModel = chatModel; }
@GetMapping("/ai/generate")
public Map
generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", this.chatModel.call(message));
}
@GetMapping("/ai/generateStream")
public Flux
generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return this.chatModel.stream(prompt);
}
}The article notes that due to resource limits the DeepSeek service may be unavailable, and suggests local deployment of the model for learning and experimentation. It concludes with a link to the full source repository.
Source code repository: https://github.com/Fj-ivy/spring-ai-examples
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.