Java integration library enabling LangChain4j applications to use Ollama's local language models with support for chat, streaming, embeddings, and advanced reasoning features
The embedding model generates vector embeddings for text segments, enabling semantic search, clustering, and similarity comparisons.
Generates vector embeddings for text using Ollama embedding models.
package dev.langchain4j.model.ollama;
public class OllamaEmbeddingModel extends DimensionAwareEmbeddingModelThread 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 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
Throws:
IllegalArgumentException - If textSegment is nullHttpTimeoutException - If request exceeds timeoutIOException - If network errorRuntimeException - If server errorThread 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);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
Throws:
IllegalArgumentException - If textSegments is null or emptyHttpTimeoutException - If request exceeds timeoutIOException - If network errorRuntimeException - If server errorThread 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");
}| Model | Dimensions | Use Case | Speed |
|---|---|---|---|
nomic-embed-text | 768 | General semantic search | Fast |
all-minilm | 384 | Fast embeddings | Very Fast |
mxbai-embed-large | 1024 | High quality | Slower |
Install with Tessl CLI
npx tessl i tessl/maven-dev-langchain4j--langchain4j-ollama