Skip to main content


What is Ollama?

Ollama is an advanced AI tool that allows users to easily set up and run large language models locally (in CPU and GPU modes). With Ollama, users can leverage powerful language models such as Llama 2 and even customize and create their own models. Ollama bundles model weights, configuration, and data into a single package, defined by a Modelfile. It optimizes setup and configuration details, including GPU usage.

For more details about Ollama, check these out:


Watch this presentation at Docker Con 23:

Watch this intro by Code to the Moon:

Get started

To get started, add the following dependencies to your project's pom.xml:



Try out a simple chat example code:

import dev.langchain4j.model.ollama.OllamaChatModel;
import org.testcontainers.ollama.OllamaContainer;
import org.testcontainers.utility.DockerImageName;

public class OllamaChatExample {

public static void main(String[] args) {
// The model name to use (e.g., "orca-mini", "mistral", "llama2", "codellama", "phi", or
// "tinyllama")
String modelName = "orca-mini";

// Create and start the Ollama container
OllamaContainer ollama =
new OllamaContainer(DockerImageName.parse("langchain4j/ollama-" + modelName + ":latest")

// Build the ChatLanguageModel
ChatLanguageModel model =

// Example usage
String answer = model.generate("Provide 3 short bullet points explaining why Java is awesome");

// Stop the Ollama container

private static String baseUrl(GenericContainer<?> ollama) {
return String.format("http://%s:%d", ollama.getHost(), ollama.getFirstMappedPort());

Try out a simple streaming chat example code:

import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.ollama.OllamaStreamingChatModel;
import dev.langchain4j.model.output.Response;
import org.testcontainers.ollama.OllamaContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.concurrent.CompletableFuture;

public class OllamaStreamingChatExample {

static String MODEL_NAME = "orca-mini"; // try "mistral", "llama2", "codellama" or "phi"
static String DOCKER_IMAGE_NAME = "langchain4j/ollama-" + MODEL_NAME + ":latest";

static OllamaContainer ollama = new OllamaContainer(

public static void main(String[] args) {
StreamingChatLanguageModel model = OllamaStreamingChatModel.builder()
.baseUrl(String.format("http://%s:%d", ollama.getHost(), ollama.getMappedPort(PORT)))

String userMessage = "Write a 100-word poem about Java and AI";

CompletableFuture<Response<AiMessage>> futureResponse = new CompletableFuture<>();
model.generate(userMessage, new StreamingResponseHandler<AiMessage>() {

public void onNext(String token) {

public void onComplete(Response<AiMessage> response) {

public void onError(Throwable error) {



OllamaChatModel and OllamaStreamingChatModel classes can be instantiated with the following params with the builder pattern:

baseUrlThe base URL of Ollama server.String
modelNameThe name of the model to use from Ollama server.String
temperatureControls the randomness of the generated responses. Higher values (e.g., 1.0) result in more diverse output, while lower values (e.g., 0.2) produce more deterministic responses.Double
topKSpecifies the number of highest probability tokens to consider for each step during generation.Integer
topPControls the diversity of the generated responses by setting a threshold for the cumulative probability of top tokens.Double
repeatPenaltyPenalizes the model for repeating similar tokens in the generated output.Double
seedSets the random seed for reproducibility of generated responses.Integer
numPredictThe number of predictions to generate for each input prompt.Integer
stopA list of strings that, if generated, will mark the end of the response.List<String>
formatThe desired format for the generated output.String
timeoutThe maximum time allowed for the API call to complete.Duration
maxRetriesThe maximum number of retries in case of API call failure.Integer

Usage Example:

OllamaChatModel ollamaChatModel = OllamaChatModel.builder()