CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-dev-langchain4j--langchain4j-ollama

Java integration library enabling LangChain4j applications to use Ollama's local language models with support for chat, streaming, embeddings, and advanced reasoning features

Overview
Eval results
Files

language-models.mddocs/

Language Models

Language models provide simple text completion without conversation context, suitable for single-turn prompts and text generation tasks.

OllamaLanguageModel

Synchronous language model for blocking text completion.

Class Signature

package dev.langchain4j.model.ollama;

public class OllamaLanguageModel implements LanguageModel

Thread Safety: Immutable after build(); safe for concurrent requests

Nullability: Instance never null after successful build()

Public Methods

generate

public Response<String> generate(String prompt)

Generates text completion from a prompt.

Parameters:

  • prompt - The input text prompt (must not be null or empty)

Returns: Response<String> containing generated text and token usage metadata

  • Never null
  • Content never null (may be empty string)
  • Deterministic with same seed and temperature=0.0

Throws:

  • IllegalArgumentException - If prompt is null or empty
  • HttpTimeoutException - If request exceeds configured timeout
  • IOException - If network connectivity fails
  • RuntimeException - If Ollama server returns error

Thread Safety: Safe for concurrent calls

Retry Behavior: Automatically retries on transient failures up to maxRetries times

Example:

import dev.langchain4j.model.output.Response;
import java.io.IOException;

try {
    Response<String> response = model.generate("Once upon a time");
    String text = response.content();
    TokenUsage usage = response.tokenUsage();
    System.out.println("Generated: " + text);
} catch (IOException e) {
    System.err.println("Network error: " + e.getMessage());
}

OllamaStreamingLanguageModel

Streaming language model for real-time token-by-token text completion.

Class Signature

package dev.langchain4j.model.ollama;

public class OllamaStreamingLanguageModel implements StreamingLanguageModel

Thread Safety: Immutable after build(); safe for concurrent requests

Streaming Threading: Callbacks invoked on HTTP client thread

Public Methods

generate

public void generate(String prompt, StreamingResponseHandler<String> handler)

Generates text completion from a prompt with streaming output.

Parameters:

  • prompt - The input text prompt (must not be null or empty)
  • handler - Handler for streaming response callbacks (must not be null)

Returns: void - Method returns immediately; response arrives via callbacks

Throws:

  • IllegalArgumentException - If prompt or handler is null

Error Handling: Errors during streaming trigger handler.onError(Throwable)

No Retry: Streaming operations do not automatically retry

Thread Safety: Handler must be thread-safe if shared across calls

See Also

  • Chat Models - For conversational AI
  • Embedding Model - For text embeddings
  • Request Parameters - Detailed parameter documentation

Install with Tessl CLI

npx tessl i tessl/maven-dev-langchain4j--langchain4j-ollama

docs

architecture.md

chat-models.md

embedding-model.md

index.md

language-models.md

model-management.md

request-parameters.md

spi.md

types.md

README.md

tile.json