Build Your First AI Chatbot with Spring Boot and DeepSeek LLM

This guide walks you through creating a Spring Boot project, configuring DeepSeek's large language model via SiliconFlow, setting up OpenAI‑compatible parameters, and implementing a REST controller that returns weather forecasts using the model, complete with step‑by‑step code snippets, configuration files, and deployment instructions.

Java Web Project
Java Web Project
Java Web Project
Build Your First AI Chatbot with Spring Boot and DeepSeek LLM

Why Combine Spring Boot and DeepSeek?

Artificial intelligence is reshaping software development, and Spring Boot offers a fast, convention‑over‑configuration way to build backend services. DeepSeek provides a cost‑effective large language model (LLM) that rivals many industry leaders. Integrating the two lets developers create intelligent applications such as chatbots, document processors, or code generators with minimal boilerplate.

Step 1: Create a Spring Boot Project

Using the Spring Initializr, start a new project and select the Web and OpenAI starters. After generation, the project automatically includes the necessary dependencies.

Spring Initializr selection
Spring Initializr selection

Step 2: Add Required Maven Dependencies

<properties>
    <java.version>17</java.version>
    <spring-ai.version>1.0.0-M5</spring-ai.version>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-openai-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>

Step 3: Configure DeepSeek (via SiliconFlow)

Register at the SiliconFlow platform (e.g., https://cloud.siliconflow.cn/i/pCa1dBVX) and generate an API key. The key and the chosen model are then placed in application.yml:

spring:
  ai:
    openai:
      api-key: YOUR_API_KEY
      base-url: https://api.siliconflow.cn
      chat:
        options:
          model: deepseek-ai/DeepSeek-R1-Distill-Llama-8B

The model name deepseek-ai/DeepSeek-R1-Distill-Llama-8B identifies the specific DeepSeek LLM you will call.

Step 4: Implement the Chat Controller

The following Java class creates a ChatClient with a system prompt that makes the model act as a weather forecaster for Suzhou. The REST endpoint /chat/{message} forwards the user’s date string to the model and returns the generated content.

package com.summer.springai.controller;

import groovy.util.logging.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*")
@Slf4j
public class ChatBotController {

    private final ChatClient chatClient;

    public ChatBotController(ChatClient.Builder builder) {
        this.chatClient = builder.defaultSystem(
            "你是一个天气预报员,当有人输入日期的时候,你输出苏州的天气预报信息,"
            + "生成结果在html页面中以markdown的格式输出,最后输出结尾的时候始终以下面的语句结尾:感谢您的咨询,我是舆情君。"
        ).build();
    }

    @GetMapping(value = "/chat/{message}")
    public String chat(@PathVariable("message") String message) {
        return chatClient.prompt()
                .user(message)
                .call()
                .content();
    }
}

Step 5: Run and Test

Start the Spring Boot application and request the endpoint, for example: http://localhost:8080/ai/chat/2025年2月12日 The response returns a markdown‑formatted weather forecast for the given date, ending with the fixed sign‑off “感谢您的咨询,我是舆情君”。

Sample chatbot response
Sample chatbot response

This simple demonstration shows how a Spring Boot service can leverage DeepSeek’s LLM to provide domain‑specific AI functionality. In a real project you could extend the controller to return JSON, store results in a database, or chain multiple model calls for more complex workflows.

JavaAILLMSpring BootDeepSeektutorialChatbot
Java Web Project
Written by

Java Web Project

Focused on Java backend technologies, trending internet tech, and the latest industry developments. The platform serves over 200,000 Java developers, inviting you to learn and exchange ideas together. Check the menu for Java learning resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.