DocumentationAPI ReferenceπŸ““ TutorialsπŸ§‘β€πŸ³ Cookbook🀝 IntegrationsπŸ’œ Discord

ChromaEmbeddingRetriever

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

NameChromaEmbeddingRetriever
Path<https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/chroma>
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

Overview

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.

Usage

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
retriever.run(query_embedding=[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")
indexing.run({"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 = querying.run({"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: