Class OllamaEmbeddingProvider

java.lang.Object
com.tnsai.llm.cache.OllamaEmbeddingProvider
All Implemented Interfaces:
EmbeddingProvider

public class OllamaEmbeddingProvider extends Object implements 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 Details

    • OllamaEmbeddingProvider

      public OllamaEmbeddingProvider()
      Local Ollama with nomic-embed-text, honouring OLLAMA_* env vars.
    • OllamaEmbeddingProvider

      public OllamaEmbeddingProvider(String model)
      Local Ollama with a custom embedding model.
    • OllamaEmbeddingProvider

      public OllamaEmbeddingProvider(String baseUrl, String model, String apiKey)
      Parameters:
      baseUrl - Ollama server URL (e.g. http://localhost:11434 or https://ollama.com); falls back to the default when null
      model - embedding model name (e.g. nomic-embed-text)
      apiKey - bearer token for Ollama Cloud; null/blank for local
  • Method Details