Enhancing your Java Spring Boot application with AI capabilities has never been easier. This guide will walk you through integrating DeepSeek AI with Spring AI, providing your users with powerful AI functionalities.
Enhancing your Java Spring Boot application with AI capabilities has never been easier. This guide will walk you through integrating DeepSeek AI with Spring AI, providing your users with powerful AI functionalities.
Before we begin, ensure you have:
Add the Spring AI dependency to your pom.xml:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
Configure your application.properties (or application.yml) with the following:
spring.ai.openai.api-key=<YOUR_DEEPSEEK_API_KEY>
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat
spring.ai.openai.chat.options.temperature=0.7
spring.ai.openai.embedding.enabled=false
Replace <YOUR_DEEPSEEK_API_KEY> with your actual DeepSeek API key.
Below is a simple controller showcasing how to use DeepSeek AI for text generation and streaming responses:
@RestController
public class ChatController {
private final OpenAiChatModel chatModel;
@Autowired
public ChatController(OpenAiChatModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/ai/generate")
public Map<String, String> generate(
@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", this.chatModel.call(message));
}
@GetMapping("/ai/generateStream")
public Flux<ChatResponse> generateStream(
@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return this.chatModel.stream(prompt);
}
}
/ai/generate: Returns a single-response generation from DeepSeek AI./ai/generateStream: Streams responses for dynamic or ongoing conversations.https://api.deepseek.com.deepseek-chat) for your use case.By following these steps, you can quickly integrate DeepSeek AI with Spring AI in your Java Spring Boot application. This integration offers AI-powered text generation, chat functionality, and more — all through a straightforward configuration process.
Remember to handle API responses and errors gracefully for a robust user experience.
If you found this article helpful and would like to discuss how these concepts can be applied to your project, I'd love to hear from you.