DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord


This is an embedding Retriever compatible with the Chroma Document Store.

Most common Position in a Pipeline1. After a text embedder and before a PromptBuilder in a RAG Pipeline
2. The last component in a semantic search Pipeline
3. After a text embedder and before an ExtractiveReader in an ExtractiveQA Pipeline
Mandatory Input variables"query_embedding": a list of floats
Output variables“documents”: a list of Documents


The ChromaEmbeddingRetriever is an embedding-based Retriever compatible with the ChromaDocumentStore. It compares the query and Document embeddings and fetches the Documents most relevant to the query from the ChromaDocumentStore based on the outcome.

The query needs to be embedded before being passed to this component. For example, you could use a text embedder component.

In addition to the query_embedding, the ChromaEmbeddingRetriever accepts other optional parameters, including top_k (the maximum number of Documents to retrieve) and filters to narrow down the search space.


On its own

This Retriever needs the ChromaDocumentStore and indexed Documents to run.

from haystack_integrations.document_stores.chroma import ChromaDocumentStore
from haystack_integrations.components.retrievers.chroma import ChromaEmbeddingRetriever

document_store = ChromaDocumentStore()

retriever = ChromaEmbeddingRetriever(document_store=document_store)

# example run query[0.1]*384)

In a Pipeline

Here is how you could use the ChromaEmbeddingRetriever in a Pipeline. In this example, you would create two Pipelines: an indexing one and a querying one.

In the indexing Pipeline, the Documents are passed to the document embedder and then written into the Document Store.

Then, in the querying Pipeline, we use a text embedder to get the vector representation of the input query that will be then passed to the ChromaEmbeddingRetriever to get the results.

import os
from pathlib import Path

from haystack import Pipeline
from haystack.dataclasses import Document
from haystack.components.writers import DocumentWriter
# Note: the following requires a "pip install sentence-transformers"
from haystack.components.embedders import SentenceTransformersDocumentEmbedder, SentenceTransformersTextEmbedder

from haystack_integrations.document_stores.chroma import ChromaDocumentStore
from haystack_integrations.components.retrievers.chroma import ChromaEmbeddingRetriever
from sentence_transformers import SentenceTransformer

# Chroma is used in-memory so we use the same instances in the two pipelines below
document_store = ChromaDocumentStore()

documents = [
    Document(content="This contains variable declarations", meta={"title": "one"}),
    Document(content="This contains another sort of variable declarations", meta={"title": "two"}),
    Document(content="This has nothing to do with variable declarations", meta={"title": "three"}),
    Document(content="A random doc", meta={"title": "four"}),

indexing = Pipeline()
indexing.add_component("embedder", SentenceTransformersDocumentEmbedder())
indexing.add_component("writer", DocumentWriter(document_store))
indexing.connect("embedder.documents", "writer.documents"){"embedder": {"documents": documents}})

querying = Pipeline()
querying.add_component("query_embedder", SentenceTransformersTextEmbedder())
querying.add_component("retriever", ChromaEmbeddingRetriever(document_store))
querying.connect("query_embedder.embedding", "retriever.query_embedding")
results ={"query_embedder": {"text": "Variable declarations"}})

for d in results["retriever"]["documents"]:
    print(d.meta, d.score)

Related Links

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