/ docs-website / versioned_docs / version-2.23 / pipeline-components / embedders / jinadocumentembedder.mdx
jinadocumentembedder.mdx
1 --- 2 title: "JinaDocumentEmbedder" 3 id: jinadocumentembedder 4 slug: "/jinadocumentembedder" 5 description: "This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Jina AI Embeddings models. The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents." 6 --- 7 8 # JinaDocumentEmbedder 9 10 This component computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses Jina AI Embeddings models. The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector representing the query is compared with those of the documents to find the most similar or relevant documents. 11 12 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Most common position in a pipeline** | Before a [`DocumentWriter`](../writers/documentwriter.mdx) in an indexing pipeline | 17 | **Mandatory init variables** | `api_key`: The Jina API key. Can be set with `JINA_API_KEY` env var. | 18 | **Mandatory run variables** | `documents`: A list of documents | 19 | **Output variables** | `documents`: A list of documents (enriched with embeddings) <br /> <br />`meta`: A dictionary of metadata | 20 | **API reference** | [Jina](/reference/integrations-jina) | 21 | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/jina | 22 23 </div> 24 25 ## Overview 26 27 `JinaDocumentEmbedder` enriches the metadata of documents with an embedding of their content. To embed a string, you should use the [`JinaTextEmbedder`](jinatextembedder.mdx). To see the list of compatible Jina Embeddings models, head to Jina AI’s [website](https://jina.ai/embeddings/). The default model for `JinaDocumentEmbedder` is `jina-embeddings-v2-base-en`. 28 29 To start using this integration with Haystack, install the package with: 30 31 ```shell 32 pip install jina-haystack 33 ``` 34 35 The component uses a `JINA_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`: 36 37 ```python 38 embedder = JinaDocumentEmbedder(api_key=Secret.from_token("<your-api-key>")) 39 ``` 40 41 To get a Jina Embeddings API key, head to https://jina.ai/embeddings/. 42 43 ### Embedding Metadata 44 45 Text documents often come with a set of metadata. If they are distinctive and semantically meaningful, you can embed them along with the text of the document to improve retrieval. 46 47 You can do this easily by using the Document Embedder: 48 49 ```python 50 from haystack import Document 51 from haystack_integrations.components.embedders.jina import JinaDocumentEmbedder 52 53 doc = Document(content="some text", meta={"title": "relevant title", "page number": 18}) 54 55 embedder = JinaDocumentEmbedder( 56 api_key=Secret.from_token("<your-api-key>"), 57 meta_fields_to_embed=["title"], 58 ) 59 60 docs_w_embeddings = embedder.run(documents=[doc])["documents"] 61 ``` 62 63 ## Usage 64 65 ### On its own 66 67 Here is how you can use the component on its own: 68 69 ```python 70 from haystack_integrations.components.embedders.jina import JinaDocumentEmbedder 71 72 doc = Document(content="I love pizza!") 73 74 document_embedder = JinaDocumentEmbedder(api_key=Secret.from_token("<your-api-key>")) 75 76 result = document_embedder.run([doc]) 77 print(result["documents"][0].embedding) 78 79 ## [0.017020374536514282, -0.023255806416273117, ...] 80 ``` 81 82 :::info 83 We recommend setting JINA_API_KEY as an environment variable instead of setting it as a parameter. 84 ::: 85 86 ### In a pipeline 87 88 ```python 89 from haystack import Pipeline 90 from haystack.document_stores.in_memory import InMemoryDocumentStore 91 from haystack_integrations.components.embedders.jina import JinaDocumentEmbedder 92 from haystack_integrations.components.embedders.jina import JinaTextEmbedder 93 from haystack.components.writers import DocumentWriter 94 from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever 95 96 document_store = InMemoryDocumentStore(embedding_similarity_function="cosine") 97 98 documents = [ 99 Document(content="My name is Wolfgang and I live in Berlin"), 100 Document(content="I saw a black horse running"), 101 Document(content="Germany has many big cities"), 102 ] 103 104 indexing_pipeline = Pipeline() 105 indexing_pipeline.add_component( 106 "embedder", 107 JinaDocumentEmbedder(api_key=Secret.from_token("<your-api-key>")), 108 ) 109 indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store)) 110 indexing_pipeline.connect("embedder", "writer") 111 112 indexing_pipeline.run({"embedder": {"documents": documents}}) 113 114 query_pipeline = Pipeline() 115 query_pipeline.add_component( 116 "text_embedder", 117 JinaTextEmbedder(api_key=Secret.from_token("<your-api-key>")), 118 ) 119 query_pipeline.add_component( 120 "retriever", 121 InMemoryEmbeddingRetriever(document_store=document_store), 122 ) 123 query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding") 124 125 query = "Who lives in Berlin?" 126 127 result = query_pipeline.run({"text_embedder": {"text": query}}) 128 129 print(result["retriever"]["documents"][0]) 130 131 ## Document(id=..., mimetype: 'text/plain', 132 ## text: 'My name is Wolfgang and I live in Berlin') 133 ``` 134 135 ## Additional References 136 137 🧑🍳 Cookbook: [Using the Jina-embeddings-v2-base-en model in a Haystack RAG pipeline for legal document analysis](https://haystack.deepset.ai/cookbook/jina-embeddings-v2-legal-analysis-rag)