/ docs-website / docs / pipeline-components / embedders / jinatextembedder.mdx
jinatextembedder.mdx
  1  ---
  2  title: "JinaTextEmbedder"
  3  id: jinatextembedder
  4  slug: "/jinatextembedder"
  5  description: "This component transforms a string into a vector that captures its semantics using a Jina Embeddings model. When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents."
  6  ---
  7  
  8  # JinaTextEmbedder
  9  
 10  This component transforms a string into a vector that captures its semantics using a Jina Embeddings model. When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents.
 11  
 12  <div className="key-value-table">
 13  
 14  |  |  |
 15  | --- | --- |
 16  | **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx)  in a query/RAG pipeline |
 17  | **Mandatory init variables** | `api_key`: The Jina API key. Can be set with `JINA_API_KEY` env var. |
 18  | **Mandatory run variables** | `text`: A string |
 19  | **Output variables** | `embedding`: A list of float numbers  <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  `JinaTextEmbedder` embeds a simple string (such as a query) into a vector. For embedding lists of documents, use the use the [`JinaDocumentEmbedder`](jinadocumentembedder.mdx), which enriches the document with the computed embedding, also known as vector. To see the list of compatible Jina Embeddings models, head to Jina AI’s [website](https://jina.ai/embeddings/). The default model for `JinaTextEmbedder` 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 = JinaTextEmbedder(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  ## Usage
 44  
 45  ### On its own
 46  
 47  Here is how you can use the component on its own:
 48  
 49  ```python
 50  from haystack_integrations.components.embedders.jina import JinaTextEmbedder
 51  
 52  text_to_embed = "I love pizza!"
 53  
 54  text_embedder = JinaTextEmbedder(api_key=Secret.from_token("<your-api-key>"))
 55  
 56  print(text_embedder.run(text_to_embed))
 57  
 58  ## {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
 59  ## 'meta': {'model': 'text-embedding-ada-002-v2',
 60  ## 'usage': {'prompt_tokens': 4, 'total_tokens': 4}}}
 61  ```
 62  
 63  :::info
 64  We recommend setting JINA_API_KEY as an environment variable instead of setting it as a parameter.
 65  :::
 66  
 67  ### In a pipeline
 68  
 69  ```python
 70  from haystack import Document
 71  from haystack import Pipeline
 72  from haystack.document_stores.in_memory import InMemoryDocumentStore
 73  from haystack_integrations.components.embedders.jina import JinaDocumentEmbedder
 74  from haystack_integrations.components.embedders.jina import JinaTextEmbedder
 75  from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
 76  
 77  document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
 78  
 79  documents = [
 80      Document(content="My name is Wolfgang and I live in Berlin"),
 81      Document(content="I saw a black horse running"),
 82      Document(content="Germany has many big cities"),
 83  ]
 84  
 85  document_embedder = JinaDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
 86  documents_with_embeddings = document_embedder.run(documents)["documents"]
 87  document_store.write_documents(documents_with_embeddings)
 88  
 89  query_pipeline = Pipeline()
 90  query_pipeline.add_component(
 91      "text_embedder",
 92      JinaTextEmbedder(api_key=Secret.from_token("<your-api-key>")),
 93  )
 94  query_pipeline.add_component(
 95      "retriever",
 96      InMemoryEmbeddingRetriever(document_store=document_store),
 97  )
 98  query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
 99  
100  query = "Who lives in Berlin?"
101  
102  result = query_pipeline.run({"text_embedder": {"text": query}})
103  
104  print(result["retriever"]["documents"][0])
105  
106  ## Document(id=..., mimetype: 'text/plain',
107  ## text: 'My name is Wolfgang and I live in Berlin')
108  ```
109  
110  ## Additional References
111  
112  🧑‍🍳 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)