coheredocumentembedder.mdx
  1  ---
  2  title: "CohereDocumentEmbedder"
  3  id: coheredocumentembedder
  4  slug: "/coheredocumentembedder"
  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 Cohere embedding models."
  6  ---
  7  
  8  # CohereDocumentEmbedder
  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 Cohere embedding models.
 11  
 12  The vectors computed by this component are necessary to perform embedding retrieval on a collection of documents. At retrieval time, the vector that represents the query is compared with those of the documents to find the most similar or relevant documents.
 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 Cohere API key. Can be set with `COHERE_API_KEY` or `CO_API_KEY` env var. |
 18  | **Mandatory run variables** | “documents”: A list of documents to be embedded |
 19  | **Output variables** | “documents”: A list of documents (enriched with embeddings)  <br /> <br />“meta”: A dictionary of metadata strings |
 20  | **API reference** | [Cohere](/reference/integrations-cohere) |
 21  | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere |
 22  
 23  ## Overview
 24  
 25  `CohereDocumentEmbedder` enriches the metadata of documents with an embedding of their content. To embed a string, you should use the [`CohereTextEmbedder`](coheretextembedder.mdx).
 26  
 27  The component supports the following Cohere models:
 28  `"embed-english-v3.0"`, `"embed-english-light-v3.0"`, `"embed-multilingual-v3.0"`,
 29  `"embed-multilingual-light-v3.0"`, `"embed-english-v2.0"`, `"embed-english-light-v2.0"`,
 30  `"embed-multilingual-v2.0"`. The default model is `embed-english-v2.0`. This list of all supported models can be found in Cohere’s [model documentation](https://docs.cohere.com/docs/models#representation).
 31  
 32  To start using this integration with Haystack, install it with:
 33  
 34  ```shell
 35  pip install cohere-haystack
 36  ```
 37  
 38  The component uses a `COHERE_API_KEY` or `CO_API_KEY` environment variable by default. Otherwise, you can pass an API key at initialization with `api_key`:
 39  
 40  ```python
 41  embedder = CohereDocumentEmbedder(api_key=Secret.from_token("<your-api-key>"))
 42  ```
 43  
 44  To get a Cohere API key, head over to https://cohere.com/.
 45  
 46  ### Embedding Metadata
 47  
 48  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.
 49  
 50  You can do this by using the Document Embedder:
 51  
 52  ```python
 53  from haystack import Document
 54  from cohere_haystack.embedders.document_embedder import CohereDocumentEmbedder
 55  
 56  doc = Document(content="some text", meta={"title": "relevant title", "page number": 18})
 57  
 58  embedder = CohereDocumentEmbedder(api_key=Secret.from_token("<your-api-key>", meta_fields_to_embed=["title"])
 59  
 60  docs_w_embeddings = embedder.run(documents=[doc])["documents"]
 61  ```
 62  
 63  ## Usage
 64  
 65  ### On its own
 66  
 67  Remember to set `COHERE_API_KEY` as an environment variable first, or pass it in directly.
 68  
 69  Here is how you can use the component on its own:
 70  
 71  ```python
 72  from haystack import Document
 73  from haystack_integrations.components.embedders.cohere.document_embedder import (
 74      CohereDocumentEmbedder,
 75  )
 76  
 77  doc = Document(content="I love pizza!")
 78  
 79  embedder = CohereDocumentEmbedder()
 80  
 81  result = embedder.run([doc])
 82  print(result["documents"][0].embedding)
 83  ## [-0.453125, 1.2236328, 2.0058594, 0.67871094...]
 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.components.writers import DocumentWriter
 92  from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
 93  
 94  from haystack_integrations.components.embedders.cohere.document_embedder import (
 95      CohereDocumentEmbedder,
 96  )
 97  from haystack_integrations.components.embedders.cohere.text_embedder import (
 98      CohereTextEmbedder,
 99  )
100  
101  document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
102  
103  documents = [
104      Document(content="My name is Wolfgang and I live in Berlin"),
105      Document(content="I saw a black horse running"),
106      Document(content="Germany has many big cities"),
107  ]
108  
109  indexing_pipeline = Pipeline()
110  indexing_pipeline.add_component("embedder", CohereDocumentEmbedder())
111  indexing_pipeline.add_component("writer", DocumentWriter(document_store=document_store))
112  indexing_pipeline.connect("embedder", "writer")
113  
114  indexing_pipeline.run({"embedder": {"documents": documents}})
115  
116  query_pipeline = Pipeline()
117  query_pipeline.add_component("text_embedder", CohereTextEmbedder())
118  query_pipeline.add_component(
119      "retriever",
120      InMemoryEmbeddingRetriever(document_store=document_store),
121  )
122  query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
123  
124  query = "Who lives in Berlin?"
125  
126  result = query_pipeline.run({"text_embedder": {"text": query}})
127  
128  print(result["retriever"]["documents"][0])
129  
130  ## Document(id=..., text: 'My name is Wolfgang and I live in Berlin')
131  ```