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

embedding-model.mddocs/

Embedding Model

The embedding model generates vector embeddings for text segments, enabling semantic search, clustering, and similarity comparisons.

OllamaEmbeddingModel

Generates vector embeddings for text using Ollama embedding models.

Class Signature

package dev.langchain4j.model.ollama;

public class OllamaEmbeddingModel extends DimensionAwareEmbeddingModel

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

Deterministic: Same input produces same output for same model (embeddings are deterministic)

Nullability: Instance never null after successful build()

Public Methods

embed

public Response<Embedding> embed(TextSegment textSegment)

Generates an embedding for a single text segment.

Parameters:

  • textSegment - Text segment to embed (must not be null)

Returns: Response<Embedding> containing the embedding

  • Never null
  • Embedding never null
  • Vector dimensions consistent for same model

Throws:

  • IllegalArgumentException - If textSegment is null
  • HttpTimeoutException - If request exceeds timeout
  • IOException - If network error
  • RuntimeException - If server error

Thread Safety: Safe for concurrent calls

Deterministic: Yes - same input produces same embedding

Example:

import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.model.output.Response;

TextSegment segment = TextSegment.from("Hello world");
Response<Embedding> response = model.embed(segment);
Embedding embedding = response.content();

float[] vector = embedding.vector();
System.out.println("Vector dimensions: " + vector.length);

embedAll

public Response<List<Embedding>> embedAll(List<TextSegment> textSegments)

Generates embeddings for multiple text segments in a single batch request.

Parameters:

  • textSegments - List of text segments to embed (must not be null or empty)

Returns: Response<List<Embedding>> containing embeddings for each segment

  • Never null
  • List size equals input list size
  • Order preserved (embeddings[i] corresponds to textSegments[i])
  • All embeddings have same dimensions

Throws:

  • IllegalArgumentException - If textSegments is null or empty
  • HttpTimeoutException - If request exceeds timeout
  • IOException - If network error
  • RuntimeException - If server error

Thread Safety: Safe for concurrent calls

Performance: More efficient than multiple embed() calls

Example:

import java.util.List;

List<TextSegment> segments = List.of(
    TextSegment.from("The quick brown fox"),
    TextSegment.from("jumps over the lazy dog")
);

Response<List<Embedding>> response = model.embedAll(segments);
List<Embedding> embeddings = response.content();

for (int i = 0; i < embeddings.size(); i++) {
    float[] vector = embeddings.get(i).vector();
    System.out.println("Segment " + i + ": " + vector.length + " dimensions");
}

Common Embedding Models

ModelDimensionsUse CaseSpeed
nomic-embed-text768General semantic searchFast
all-minilm384Fast embeddingsVery Fast
mxbai-embed-large1024High qualitySlower

See Also

  • Chat Models - For conversational AI
  • Language Models - For text generation
  • Model Management - Managing Ollama models

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