Ollama AI Models
and Spring AI
are different entities that communicate via Rest Services
. The Spring Ai
auto pull Model
mechanism fetches the Models
from the repository if not available. This feature is particularly useful for development
and testing
purposes and deploying the application on new Servers.
GGUF Hugging Face provides thousands of free Models
The Spring Ai Auto pull Model supports 3 strategies.
Pull Strategies | Description |
---|---|
always | PullModelStrategy.ALWAYS will pull the Model even if it's already available, this will make sure the latest Model is used. |
when_missing | PullModelStrategy.WHEN_MISSING will only pull the Model if it's not available. This may use an Older Version of the Model if already available. |
never | PullModelStrategy.NEVER will pull the model. |
Auto Pulling is not recommended in the Production Envirment
Spring Ai
Auto Pulling Model
allows us to configure the Model
pulling properties as well such as timeout
and max retries
, if required we can pull multiple additional Models
as well.
Application initialization will only happen after the Model is pulled, this might slow down the startup for the first time.
Along with the main Model
, The Spring Ai
pulls additional Models
by setting the properties spring.ai.ollama.init.chat.additional-models
.
package com.example.springai.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SpringAiController {
private final ChatClient chatClient;
public SpringAiController(ChatClient.Builder chatClient) {
this.chatClient = chatClient.build();
}
@GetMapping("/hello")
String hello() {
String helloPrompt = "Hello, I am learning Ai with Spring";
return this.chatClient.prompt().user(helloPrompt).call().content();
}
}
package com.example.springai;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAiApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAiApplication.class, args);
}
}
spring.application.name=SpringAi
spring.docker.compose.lifecycle-management=start-only
spring.threads.virtual.enabled=true
# If running the Ollama Docker Instance separately, then set this property
spring.docker.compose.enabled=false
#Auto-pulling Models
spring.ai.ollama.init.pull-model-strategy=when_missing
spring.ai.ollama.init.timeout=15m
spring.ai.ollama.init.max-retries=3
# The default Ollama Model in Spring Ai is mistral, but it can be changed by setting the below property.
spring.ai.ollama.chat.options.model=llama3.1
# If additional Models are required, then set this property
#spring.ai.ollama.init.chat.additional-models=llama3.2, qwen2.5
services:
ollama-model:
image: ollama/ollama:latest
container_name: ollama_container
ports:
- 11434:11434/tcp
healthcheck:
test: ollama --version || exit 1
command: serve
volumes:
- ./ollama/ollama:/root/.ollama
- ./entrypoint.sh:/entrypoint.sh
pull_policy: missing
tty: true
restart: no
entrypoint: [ "/usr/bin/bash", "/entrypoint.sh" ]
open-webui:
image: ghcr.io/open-webui/open-webui:main
container_name: open_webui_container
environment:
WEBUI_AUTH: false
ports:
- "8081:8080"
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- open-webui:/app/backend/data
restart: no
volumes:
open-webui:
#!/bin/bash
# Start Ollama in the background.
/bin/ollama serve &
# Record Process ID.
pid=$!
# Pause for Ollama to start.
sleep 5
# The default Ollama Model in Spring Ai is mistral, but it can be changed in the applications property file. Make sure to download the same Model here
echo "🔴 Retrieve llama3.1 model..."
ollama pull mistral
echo "🟢 Done!"
# Wait for the Ollama process to finish.
wait $pid
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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.3.2</version>
<relativePath/>
</parent>
<groupId>com.example.springai</groupId>
<artifactId>auto_pulling_models</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Auto-pulling Models</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
<spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.springai.SpringAiApplication</mainClass>
<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>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>
Run the curl to see the Spring Ai Auto Pull Model
curl --location 'http://localhost:8080/hello'