valkeyembeddingretriever.mdx
1 --- 2 title: "ValkeyEmbeddingRetriever" 3 id: valkeyembeddingretriever 4 slug: "/valkeyembeddingretriever" 5 description: "This is an embedding Retriever compatible with the Valkey Document Store." 6 --- 7 8 # ValkeyEmbeddingRetriever 9 10 This is an embedding Retriever compatible with the Valkey Document Store. 11 12 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Most common position in a pipeline** | 1. After a Text Embedder and before a [`ChatPromptBuilder`](../builders/chatpromptbuilder.mdx) or [`PromptBuilder`](../builders/promptbuilder.mdx) in a RAG pipeline 2. The last component in a semantic search pipeline 3. After a Text Embedder and before an [`ExtractiveReader`](../readers/extractivereader.mdx) in an extractive QA pipeline | 17 | **Mandatory init variables** | `document_store`: An instance of a [ValkeyDocumentStore](../../document-stores/valkeydocumentstore.mdx) | 18 | **Mandatory run variables** | `query_embedding`: A list of floats | 19 | **Output variables** | `documents`: A list of documents | 20 | **API reference** | [Valkey](/reference/integrations-valkey) | 21 | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/valkey | 22 23 </div> 24 25 ## Overview 26 27 The `ValkeyEmbeddingRetriever` is an embedding-based Retriever compatible with the [`ValkeyDocumentStore`](../../document-stores/valkeydocumentstore.mdx). It compares the query and Document embeddings and fetches the Documents most relevant to the query from the `ValkeyDocumentStore` based on vector similarity. 28 29 ### Parameters 30 31 When using the `ValkeyEmbeddingRetriever` in your system, ensure the query and Document [embeddings](../embedders.mdx) are available. You can do so by adding a Document embedder to your indexing pipeline and a text embedder to your query pipeline. 32 33 In addition to the `query_embedding`, the `ValkeyEmbeddingRetriever` accepts other optional parameters, including `top_k` (the maximum number of Documents to retrieve) and `filters` to narrow down the search space. 34 35 ## Usage 36 37 ### Installation 38 39 To start using Valkey with Haystack, install the package with: 40 41 ```shell 42 pip install valkey-haystack 43 ``` 44 45 ### On its own 46 47 This Retriever needs an instance of `ValkeyDocumentStore` and indexed Documents to run. 48 49 ```python 50 from haystack_integrations.document_stores.valkey import ValkeyDocumentStore 51 from haystack_integrations.components.retrievers.valkey import ValkeyEmbeddingRetriever 52 53 document_store = ValkeyDocumentStore( 54 nodes_list=[("localhost", 6379)], 55 index_name="my_documents", 56 embedding_dim=768, 57 distance_metric="cosine", 58 ) 59 60 retriever = ValkeyEmbeddingRetriever(document_store=document_store) 61 62 # Using a fake vector to keep the example simple 63 retriever.run(query_embedding=[0.1] * 768) 64 ``` 65 66 ### In a Pipeline 67 68 ```python 69 from haystack import Document, Pipeline 70 from haystack.components.embedders import ( 71 SentenceTransformersDocumentEmbedder, 72 SentenceTransformersTextEmbedder, 73 ) 74 from haystack.components.writers import DocumentWriter 75 from haystack_integrations.document_stores.valkey import ValkeyDocumentStore 76 from haystack_integrations.components.retrievers.valkey import ValkeyEmbeddingRetriever 77 78 document_store = ValkeyDocumentStore( 79 nodes_list=[("localhost", 6379)], 80 index_name="my_documents", 81 embedding_dim=768, 82 distance_metric="cosine", 83 ) 84 85 documents = [ 86 Document(content="There are over 7,000 languages spoken around the world today."), 87 Document( 88 content="Elephants have been observed to behave in a way that indicates a high level of self-awareness, such as recognizing themselves in mirrors.", 89 ), 90 Document( 91 content="In certain parts of the world, like the Maldives, Puerto Rico, and San Diego, you can witness the phenomenon of bioluminescent waves.", 92 ), 93 ] 94 95 indexing = Pipeline() 96 indexing.add_component("embedder", SentenceTransformersDocumentEmbedder()) 97 indexing.add_component("writer", DocumentWriter(document_store)) 98 indexing.connect("embedder.documents", "writer.documents") 99 indexing.run({"embedder": {"documents": documents}}) 100 101 query_pipeline = Pipeline() 102 query_pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder()) 103 query_pipeline.add_component( 104 "retriever", 105 ValkeyEmbeddingRetriever(document_store=document_store), 106 ) 107 query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding") 108 109 query = "How many languages are there?" 110 result = query_pipeline.run({"text_embedder": {"text": query}}) 111 112 print(result["retriever"]["documents"][0]) 113 ``` 114 115 For a full RAG example with `ValkeyEmbeddingRetriever`, see the [ValkeyDocumentStore](../../document-stores/valkeydocumentstore.mdx#using-valkey-in-a-rag-pipeline) documentation.