Spring AI 2 + Ollama: ChatClient setup and sampling

Learn to connect Java to Ollama, configure ChatClient, and inspect token probabilities with an HTTP interceptor that requests the top 10 alternatives.

Player not loading? Watch on YouTube

This tutorial connects a Java application to a local LLM through Spring AI 2 and Ollama, without API keys. Borisov starts Ollama in Docker and uses IntelliJ IDEA to build a service around ChatClient. He describes the demonstrated baseline as Spring Boot 4 and Java 25, and notes that the container uses CPU execution unless GPU access is configured.

The code walkthrough follows Ollama auto-configuration into application properties, then sets request timeouts, a system prompt and token limits. Calls use the synchronous content method so the response is easier to inspect. To examine token selection, Borisov adds an HTTP interceptor that requests logprobs and prints normalized probabilities for alternative tokens.

Short sentence-completion examples show how context, temperature, top-k and top-p affect the output. In the demonstrated pipeline, top-k runs before temperature, followed by top-p. Borisov cautions that other runtimes can apply these steps in a different order, and that temperature zero does not guarantee identical results across hardware.

A practical section covers option merging: calling options can replace ChatClient defaults. The Spring AI 2 example instead passes a Prompt containing request-specific options so the framework combines them with existing settings. This explains why changing temperature can unexpectedly discard a top-k setting.