Class OllamaEmbeddingProvider
- All Implemented Interfaces:
EmbeddingProvider
EmbeddingProvider backed by an Ollama server's /api/embed
endpoint. Enables keyless embeddings — local by default, or Ollama Cloud —
so the semantic-cache / link-suggestion tiers work without an OpenAI key.
Configuration (environment)
- OLLAMA_BASE_URL — server URL (default
http://localhost:11434) - OLLAMA_API_KEY — bearer token for Ollama Cloud (optional for local)
var provider = new OllamaEmbeddingProvider(); // nomic-embed-text @ localhost
var cache = SemanticCache.builder().embeddingProvider(provider).build();
The embedding dimension is reported as the model's known default until the first successful call, after which the real vector length is used.
Task-instruction prefixes (nomic)
nomic-embed-text is contrastively trained with a per-task instruction
prefix and produces a degenerate, collapsed cosine band without one. This provider
prepends the matching prefix (search_query: / search_document: /
clustering: / classification:) based on the EmbeddingTask
passed to embed(String, EmbeddingTask); the no-arg embed(String)
defaults to EmbeddingTask.DOCUMENT. Non-nomic models are passed through
unprefixed so a prefix is never applied to a model not trained on it.
-
Constructor Summary
ConstructorsConstructorDescriptionLocal Ollama withnomic-embed-text, honouring OLLAMA_* env vars.OllamaEmbeddingProvider(String model) Local Ollama with a custom embedding model.OllamaEmbeddingProvider(String baseUrl, String model, String apiKey) -
Method Summary
Modifier and TypeMethodDescriptionfloat[]Generates an embedding vector for the given text.float[]embed(String text, EmbeddingTask task) Generates an embedding fortextconditioned ontask.List<float[]> embedBatch(List<String> texts) Generates embeddings for multiple texts in batch.List<float[]> embedBatch(List<String> texts, EmbeddingTask task) Batch counterpart ofEmbeddingProvider.embed(String, EmbeddingTask).intReturns the dimension of the embedding vectors.Returns the name of the embedding model.
-
Constructor Details
-
OllamaEmbeddingProvider
public OllamaEmbeddingProvider()Local Ollama withnomic-embed-text, honouring OLLAMA_* env vars. -
OllamaEmbeddingProvider
Local Ollama with a custom embedding model. -
OllamaEmbeddingProvider
- Parameters:
baseUrl- Ollama server URL (e.g.http://localhost:11434orhttps://ollama.com); falls back to the default when nullmodel- embedding model name (e.g.nomic-embed-text)apiKey- bearer token for Ollama Cloud; null/blank for local
-
-
Method Details
-
embed
Description copied from interface:EmbeddingProviderGenerates an embedding vector for the given text.- Specified by:
embedin interfaceEmbeddingProvider- Parameters:
text- The text to embed- Returns:
- A float array representing the embedding vector
-
embed
Description copied from interface:EmbeddingProviderGenerates an embedding fortextconditioned ontask.Instruction-tuned models (e.g.
nomic-embed-text) embed the same text differently per task; supplying the right task is what separates related from unrelated text. The default ignorestask— correct for providers whose model has no task convention (hash-based, OpenAItext-embedding-3).- Specified by:
embedin interfaceEmbeddingProvider- See Also:
-
embedBatch
Description copied from interface:EmbeddingProviderGenerates embeddings for multiple texts in batch.- Specified by:
embedBatchin interfaceEmbeddingProvider- Parameters:
texts- The texts to embed- Returns:
- List of embedding vectors
-
embedBatch
Description copied from interface:EmbeddingProviderBatch counterpart ofEmbeddingProvider.embed(String, EmbeddingTask). The default ignorestask; task-aware providers override this.- Specified by:
embedBatchin interfaceEmbeddingProvider
-
getDimension
public int getDimension()Description copied from interface:EmbeddingProviderReturns the dimension of the embedding vectors.- Specified by:
getDimensionin interfaceEmbeddingProvider
-
getModelName
Description copied from interface:EmbeddingProviderReturns the name of the embedding model.- Specified by:
getModelNamein interfaceEmbeddingProvider
-