Integrate Spring AI with DeepSeek AI

In today’s AI-driven development landscape, integrating machine learning models into applications has become essential. When you integrate Spring AI with DeepSeek AI it unlocks powerful, customizable AI solutions. This guide walks you through both connecting a local DeepSeek AI instance and connecting DeepSeek API to a Spring Boot application, empowering you to harness AI capabilities while maintaining full control over your infrastructure.

Prerequisites

  1. Java JDK 17+ and Spring Boot 3.x
  2. Either run DeepSeek AI Locally or generate DeepSeek API Key

Step 1: Set Up Spring Boot Project

Create a new Spring Boot project using Spring Initializr with the dependencies below:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.codersathi</groupId>
    <artifactId>deepseek-ai-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>deepseek-ai-demo</name>
    <description>deepseek-ai-demo</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.0.0-M5</spring-ai.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>${spring-ai.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

If you want to use DeepSeek API Key then you need to add following depsndency instead of ollama:

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

Meke sure to remove spring-ai-ollama-spring-boot-starter dependency from your pom.xml file if you are using DeepSeek API key. You don’t need to use seperate dependency to use DeepSeek API. It works with spring-ai-openai-spring-boot-starter dependency.

Step 2: Configure Spring AI for DeepSeek

Add DeepSeek connection properties to application.properties:

application.properties

Use Local Deepseek with ollama

Dependency:

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

If you are going to integrate Spring AI with local DeepSeek AI instance then use below configuration:

spring.ai.ollama.chat.enabled=true
spring.ai.ollama.chat.model=deepseek-coder-v2
spring.ai.ollama.base-url=http://localhost:11434

Use Deepseek API Key

Dependency:

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

If you are going to integrate spring ai with DeepSeek AI API then you must generate Deepseek API Key to use below configuration:

spring.ai.openai.api-key=add-your-api-key-here
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat
spring.ai.openai.embedding.enabled=false //Deepseek does not support embedding as of now

Step 3: Create REST Controller

Expose API endpoint for request processing and call DeepSeek AI model. You can call DeepSeek AI with ChatClient directly whether it is running in your local computer or calling directly Cloud DeepSeek API. This exact same code works for both local DeepSeek AI and Cloud DeepSeek AI.

ChatController.java

package com.codersathi.deepseekaidemo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/ai")
@Slf4j
public class ChatController {

	private final ChatClient chatClient;

	public ChatController(ChatClient.Builder chatClientBuilder) {
		this.chatClient = chatClientBuilder.build();
	}

	@GetMapping("/chat")
	public String generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
		log.info("Generating response");
		return chatClient.prompt(message).call().content();
	}

}

Step 4: Test Application

Type following URL in your browser and you may see the response:

http://localhost:8080/ai/chat?message=what is java

Troubleshooting Common Issues

  • Connection Refused: Verify DeepSeek AI with the help of ollama is running on port 11434 if you are using DeepSeek AI locally otherwise, you need to check your API Key in the DeepSeek platform.

Why Integrate Spring AI with Local DeepSeek AI?

  • Data Privacy: Keep sensitive data on-premises.
  • Customization: Tailor DeepSeek AI models to your needs.
  • Performance: Reduce latency by processing data locally.
  • Cost Efficiency: Avoid cloud-based API fees.

Conclusion

In this post, we learned how to integrate Spring AI with local DeepSeek AI with example. Now, you can adjust this implementation as per your need. Good Luck 🙂

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments