Artificial Intelligence 9 min read

How to Integrate Spring AI with Ollama and DeepSeek‑r1 in Spring Boot 3

This article walks through installing Ollama, downloading the DeepSeek‑r1:1.5b model, configuring Spring AI in a Spring Boot 3 project, and exposing the model via a REST endpoint with Server‑Sent Events, providing complete code snippets and step‑by‑step screenshots for a functional AI integration.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Integrate Spring AI with Ollama and DeepSeek‑r1 in Spring Boot 3

Spring Boot 3 case collection PDF is now available, and the article demonstrates how to use Spring AI together with Ollama to call the DeepSeek‑r1:1.5b model.

What is Ollama?

Ollama is an open‑source framework for running large language models locally. It provides a lightweight, extensible architecture, supports multiple model families, offers an OpenAI‑compatible API, and runs on Windows, Linux and macOS.

What is Spring AI?

Spring AI is an application framework that brings Spring’s modular and portable design principles to artificial‑intelligence development, allowing POJOs to be used as building blocks for AI services.

Supported model providers

Chat Completion

Embedding

Text to Image

Audio Transcription

Text to Speech

Moderation

What is DeepSeek?

DeepSeek is a recently popular large language model.

Practical steps

2.1 Install Ollama

Download the installer from https://ollama.com/ and install the appropriate version (e.g., Windows). After installation, set the OLLAMA_HOST environment variable to change the default address and port, then verify the service at http://localhost:11111 .

2.2 Connect Ollama

Add the following Maven properties and dependencies:

<code>&lt;properties&gt;
  &lt;spring.ai.version&gt;1.0.0-M6&lt;/spring.ai.version&gt;
&lt;/properties&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.ai&lt;/groupId&gt;
  &lt;artifactId&gt;spring-ai-ollama-spring-boot-starter&lt;/artifactId&gt;
  &lt;version&gt;${spring.ai.version}&lt;/version&gt;
&lt;/dependency&gt;
</code>

Configure application.yml :

<code>spring:
  ai:
    ollama:
      base-url: http://localhost:11111
      chat:
        enabled: true
        options:
          model: deepseek-r1:1.5b
</code>

Create a ChatClient bean and a controller that streams the AI response:

<code>@Configuration
public class AIConfig {
  @Bean
  ChatClient chatClient(ChatClient.Builder builder) {
    return builder.build();
  }
}

@RestController
@RequestMapping("/ai")
public class AIController {
  private final ChatClient chatClient;
  AIController(ChatClient chatClient) { this.chatClient = chatClient; }

  @GetMapping(value = "/chat", produces = "text/html;charset=utf-8")
  public Flux&lt;String&gt; chat(String message) {
    return chatClient.prompt()
        .user(message)
        .stream()
        .content();
  }
}
</code>

2.3 Use Server‑Sent Events

Modify the endpoint to produce MediaType.TEXT_EVENT_STREAM_VALUE and add a simple HTML page that opens an EventSource to the /ai/chat URL, displaying the streamed text.

<code><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spring AI + Ollama + DeepSeek‑r1:1.5b</title>
<script>
function submitText() {
  const text = document.querySelector("#userText").value;
  const chat = document.querySelector('.chat-content');
  chat.innerHTML = '';
  const source = new EventSource(`http://localhost:8080/ai/chat?message=${text}`);
  let count = 0;
  source.onmessage = e => {
    const data = e.data;
    if (data === '\n' || data === '') { count++; }
    if (count == 2) { source.close(); }
    if (data !== '<think>' && data !== '</think>') {
      chat.appendChild(document.createTextNode(data));
    }
  };
}
</script>
<style>
.chat-content { width:600px; height:500px; border:1px solid #DDD; }
</style>
</head>
<body>
<div class="chat-content"></div>
<input type="text" id="userText" value="Tell a joke in 50 characters">
<button type="button" onclick="submitText()">Submit</button>
</body>
</html>
</code>

The stream may not terminate automatically; additional logic is required to close the connection.

Following these steps allows you to run the DeepSeek‑r1 model locally via Ollama and expose it through a Spring Boot 3 application.

JavaSpring BootDeepSeekSpring AIAI integrationOllama
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login 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.