DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord🎨 Studio (Waitlist)
Documentation

OllamaDocumentEmbedder

This component computes the embeddings of a list of Documents using embedding models compatible with the Ollama Library.

NameOllamaDocumentEmbedder
TypeDocument Embedder
Pathhttps://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/ollama
Position in a PipelineBefore a DocumentWriter in an indexing Pipeline
Inputs“documents”: a list of Document objects to be embedded
Outputs“documents”: a list of Document objects (enriched with embeddings)

“meta”: a dictionary of metadata strings

OllamaDocumentEmbedder computes the embeddings of a list of Documents and stores the obtained vectors in the embedding field of each Document. It uses embedding models compatible with the Ollama Library.

The vectors computed by this component are necessary to perform embedding retrieval on a collection of Documents. At retrieval time, the vector that represents the query is compared with those of the Documents to find the most similar or relevant Documents.

Overview

OllamaDocumentEmbedder should be used to embed a list of Documents. For embedding a string only, use the OllamaTextEmbedder.

The component uses http\://localhost:11434/api/embeddings as the default URL as most available setups (Mac, Linux, Docker) default to port 11434.

Compatible Models

Unless specified otherwise while initializing this component, the default embedding model is "orca-mini". See other possible pre-built models in Ollama's library. To load your own custom model, follow the instructions from Ollama.

Installation

To start using this integration with Haystack, install the package with:

pip install Ollama-haystack

Make sure that you have a running Ollama model (either through a docker container, or locally hosted). No other configuration is necessary as Ollama has the embedding API built in.

Embedding Metadata

Most embedded metadata contains information about the model name and type. You can pass optional arguments, such as temperature, top_p, and others, to the Ollama generation endpoint.

The name of the model used will be automatically appended as part of the Document metadata. An example payload using the orca-mini model will look like this:

{'meta': {'model': 'orca-mini'}}

Usage

On its own

from haystack import Document
from haystack_integrations.components.embedders.ollama import OllamaDocumentEmbedder

doc = Document(content="What do llamas say once you have thanked them? No probllama!")
document_embedder = OllamaDocumentEmbedder()

result = document_embedder.run([doc])
print(result['documents'][0].embedding)

#Calculating embeddings: 100%|██████████| 1/1 [00:02<00:00, 2.82s/it]

#[-0.16412407159805298, -3.8359334468841553, ... ]

In a Pipeline

from haystack import Pipeline

from haystack_integrations.components.embedders.ollama import OllamaDocumentEmbedder

from haystack.components.preprocessors import DocumentCleaner, DocumentSplitter

from haystack.components.converters import PyPDFToDocument
from haystack.components.writers import DocumentWriter
from haystack.document_stores.types import DuplicatePolicy
from haystack.document_stores.in_memory import InMemoryDocumentStore

document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")

embedder = OllamaDocumentEmbedder(model="orca-mini", url="http://localhost:11434/api/embeddings") # This is the defaulted model and URL

cleaner = DocumentCleaner()
splitter = DocumentSplitter()
file_converter = PyPDFToDocument()
writer = DocumentWriter(document_store=document_store, policy=DuplicatePolicy.OVERWRITE)

indexing_pipeline = Pipeline()

# Add components to pipeline
indexing_pipeline.add_component("embedder", embedder)
indexing_pipeline.add_component("converter", file_converter)
indexing_pipeline.add_component("cleaner", cleaner)
indexing_pipeline.add_component("splitter", splitter)
indexing_pipeline.add_component("writer", writer)

# Connect components in pipeline
indexing_pipeline.connect("converter", "cleaner")
indexing_pipeline.connect("cleaner", "splitter")
indexing_pipeline.connect("splitter", "embedder")
indexing_pipeline.connect("embedder", "writer")

# Run Pipeline
indexing_pipeline.run({"converter": {"sources": ["files/test_pdf_data.pdf"]}})

# Calculating embeddings: 100%|██████████| 115/115
# {'embedder': {'meta': {'model': 'orca-mini'}},  'writer': {'documents_written': 115}}

Related Links

Check out the API reference in the GitHub repo or in our docs: