JavaSpringAIDeepSeek

Integrating DeepSeek with Spring AI: A Quick Guide

By Arapsih Güngör
Picture of the author
Published on
Illustration of Java code integrating DeepSeek AI with Spring AI for powerful AI functionalities

Integrating DeepSeek with Spring AI: A Quick Guide

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.

Setting Up Your Project

Before we begin, ensure you have:

  • Java 17 (or higher) installed
  • Maven for dependency management
  • A DeepSeek API key

Add the Spring AI dependency to your pom.xml:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

Configuration

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.

Implementing AI Chat

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);
    }
}

Endpoints Provided:

  • /ai/generate: Returns a single-response generation from DeepSeek AI.
  • /ai/generateStream: Streams responses for dynamic or ongoing conversations.

Key Points to Remember

  1. Valid DeepSeek API Key: Ensure you provide a valid API key in your configuration.
  2. Base URL: By default, set to https://api.deepseek.com.
  3. Model Selection: Choose the appropriate DeepSeek model (e.g., deepseek-chat) for your use case.
  4. Unstable Features: DeepSeek's Function Calling feature may still be unstable in its current version. Use with caution.

Conclusion

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.

Resources