DEV Community

Cover image for SpringAI + DeepSeek: Faster Than Brewing a Coffee
Danilo P. De Luca
Danilo P. De Luca

Posted on

SpringAI + DeepSeek: Faster Than Brewing a Coffee

Over the past few days, there’s been plenty of buzz about DeepSeek and OpenAI. If you haven’t come across the news yet, no worries—this post isn’t about the headlines. But if you’re a #Java developer curious about how to use this 'new' AI, which has shown better results, lower costs, and already surpassed OpenAI in downloads and usage or if you’re looking to integrate with any AI for the first time here’s the best part: it’s faster than brewing a cup of coffee!

Spring AI

First, let’s talk about SpringAI. As many of us know, the Spring Framework ecosystem is vast, and every time a new technology or concept gains traction in our field—especially one that interests the software developer and Java communities—efforts are made to integrate it with core frameworks like Spring Boot.

With the rise of AI, it was no different. That’s why SpringAI was created: to simplify the integration of Java applications with any AI domain or platform.

Spring AI is an application framework for AI engineering. Its goal is to apply to the AI domain Spring ecosystem design principles such as portability and modular design and promote using POJOs as the building blocks of an application to the AI domain.

Spring AI architectural illustration

DeepSeek

DeepSeek is a player in the game of AI Platforms, like the famous OpenAI it contains a lot of features and ways to integrate with, as well as a bunch of AI models, here you can take a look at more features, including their chat, additionally its important to share that it's an open-source model.

DeepSeek-V3 achieves a significant breakthrough in inference speed over previous models.
It tops the leaderboard among open-source models and rivals the most advanced closed-source models globally.

Integrating SpringAI with DeepSeek

First, you might have Java installed on your local machine. Once you have it, we will use the Spring Start project to set up our brand-new project, which will integrate with DeepSeek.

  1. Go to Spring Start
  2. Choose your preferences, I suggest Maven + Java + Spring Boot 3.4.2 (latest) and for Project Metadata I filled as below, although you can place anything that you want, also I like jar packaging and I set up Java 21.

Project metadata filled with maven, java 21, spring boot 3.4.2, jar packaging

  1. Add Dependencies, for this we will need 2 dependencies:
  2. Spring Web
  3. OpenAI Adding dependencies Spring Web and OpenAI

After that, it's ready to generate your project. Click on 'Generate,' and a ZIP file containing the project will be downloaded. Unzip it into your favorite directory (in my case, I just unzipped it in my chaotic Downloads directory—remember to clean up your Downloads folder regularly!)

Open the project in your favorite IDE. I'm using IntelliJ in this example (if you need help with that, check out the article "Using Java 21 with IntelliJ IDEA").

Once you open the project you will find the following structure and classes on it:

Project structure after importing it into your IDE

Before we start, let's create the DeepSeek keys.

DeepSeek - Creating your API-Key

When integrating with any AI Platform, we need an API-Key and with DeepSeek is the same, so the following are the steps to create it:

  1. Go to the DeepSeek Platform website, and register yourself using your preferred email.
  2. When logged, face the API Keys menu:

DeepSeek menu options with API-Key

  1. Create your new API Key - remember to keep it safe as you do with your passwords and crypto-wallets, also you can delete it whenever you want.

Creating your API-Key in DeepSeek

If you would like to know more about the pricing take a look at this documentation

SpringAI + DeepSeek

Now that we have our project and the API Key, Time to bring it to life!

As you may know, once we add the Spring OpenAI dependency to our application (you can check your pom/gradle file for spring-ai-openai-spring-boot-starter) all the setup for integrating with any AI platform is managed through environment variables. If you want to learn more, feel free to check out thedocumentation. To integrate with #DeepSeek, we will need 4 environment variables:

spring.application.name=deepseek
spring.ai.openai.api-key=${API_KEY_DEEPSEEK}
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat
Enter fullscreen mode Exit fullscreen mode

Note: the spring.ai.openai.chat.options.model has two options depending on your need.

The deepseek-chat model has been upgraded to DeepSeek-V3. The API remains unchanged. You can invoke DeepSeek-V3 by specifying model='deepseek-chat'.
The deepseek-reasoner is the latest reasoning model, DeepSeek-R1, released by DeepSeek. You can invoke DeepSeek-R1 by specifying model='deepseek-reasoner'.

Once you add this variables it will be able to integrate with DeepSeek. Now lets create some piece of code to ask something for it.

In your Application.java file add the following line of code:

@Bean
    public CommandLineRunner runner(ChatClient.Builder builder) {
        return args -> {
            ChatClient chatClient = builder.build();
            String response = chatClient.prompt("Tell a brief history of Java programming language.").call().content();
            System.out.println(response);
        };
    }
Enter fullscreen mode Exit fullscreen mode

This code will send the message "Tell a brief history of Java programming language." will send it to DeepSeek and print the response, so to test it run (or use the maven/gradle plugin in IntelliJ to get it running):

./mvnw spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Spring Application log and DeepSeek integration response for the question

There is it! Now you have one Java Application integrating with DeepSeek - _and opps you couldn't even finish brewing your coffee ☕ _

What I like most about it is the fact that we can integrate with any AI platform by making only 3 to 5 lines of code changes. A big shoutout to Spring AI! And big thanks to Dan Vega for sharing and contributing to Spring Ecosystem!

Some references:

Top comments (0)