Skip to main content
Version: 3.1

DynamoDBEmbeddingRetriever

An embedding-based Retriever compatible with the Amazon DynamoDB 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 a TransformersExtractiveReader in an extractive QA pipeline
Mandatory init variablesdocument_store: An instance of a DynamoDBDocumentStore
Mandatory run variablesquery_embedding: A vector representing the query (a list of floats)
Output variablesdocuments: A list of documents
API referenceAmazon DynamoDB
GitHub linkhttps://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/dynamodb

Overview

The DynamoDBEmbeddingRetriever is an embedding-based Retriever compatible with the DynamoDBDocumentStore. It compares the query and Document embeddings and fetches the Documents most relevant to the query using DynamoDB's native SearchVectors API with cosine similarity.

When using the DynamoDBEmbeddingRetriever in your Pipeline, make sure embeddings are available. Add a Document Embedder to your indexing Pipeline and a Text Embedder to your query Pipeline.

In addition to query_embedding, the Retriever accepts optional parameters including top_k (the maximum number of Documents to retrieve) and filters to narrow down the search space. The filter_policy parameter controls how run-time filters combine with the filters set at initialization.

Candidate limit

DynamoDB's SearchVectors returns at most 100 candidates per request, so top_k cannot exceed 100. Metadata filters are applied client-side to those candidates, because DynamoDB can only filter on attributes fixed in the index at creation time. A selective filter can therefore return fewer than top_k documents even when more matching documents exist.

Installation

Install the integration:

shell
pip install dynamodb-haystack

The pipeline example below also uses the Sentence Transformers embedders:

shell
pip install sentence-transformers-haystack

Set your AWS credentials and region as environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION) or rely on any other boto3 credential source.

Usage

On its own

python
from haystack_integrations.document_stores.dynamodb import DynamoDBDocumentStore
from haystack_integrations.components.retrievers.dynamodb import (
DynamoDBEmbeddingRetriever,
)

document_store = DynamoDBDocumentStore(embedding_dimension=768)
retriever = DynamoDBEmbeddingRetriever(document_store=document_store)

# using a fake vector to keep the example simple
retriever.run(query_embedding=[0.1] * 768)

In a Pipeline

python
from haystack import Document, Pipeline
from haystack.document_stores.types import DuplicatePolicy
from haystack_integrations.components.embedders.sentence_transformers import (
SentenceTransformersTextEmbedder,
SentenceTransformersDocumentEmbedder,
)
from haystack_integrations.document_stores.dynamodb import DynamoDBDocumentStore
from haystack_integrations.components.retrievers.dynamodb import (
DynamoDBEmbeddingRetriever,
)

document_store = DynamoDBDocumentStore(embedding_dimension=768)

documents = [
Document(content="There are over 7,000 languages spoken around the world today."),
Document(
content="Elephants have been observed to recognize themselves in mirrors."
),
Document(
content="Bioluminescent waves can be seen in the Maldives and Puerto Rico."
),
]

document_embedder = SentenceTransformersDocumentEmbedder()
documents_with_embeddings = document_embedder.run(documents)

document_store.write_documents(
documents_with_embeddings.get("documents"),
policy=DuplicatePolicy.OVERWRITE,
)

query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder())
query_pipeline.add_component(
"retriever",
DynamoDBEmbeddingRetriever(document_store=document_store),
)
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")

result = query_pipeline.run(
{"text_embedder": {"text": "How many languages are there?"}}
)
print(result["retriever"]["documents"][0])