ollamatextembedder.mdx
  1  ---
  2  title: "OllamaTextEmbedder"
  3  id: ollamatextembedder
  4  slug: "/ollamatextembedder"
  5  description: "This component computes the embeddings of a string using embedding models compatible with the Ollama Library."
  6  ---
  7  
  8  # OllamaTextEmbedder
  9  
 10  This component computes the embeddings of a string using embedding models compatible with the Ollama Library.
 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 run variables** | `text`: A string |
 18  | **Output variables** | `embedding`: A list of float numbers (vectors)  <br /> <br />`meta`: A dictionary of metadata strings |
 19  | **API reference** | [Ollama](/reference/integrations-ollama) |
 20  | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/ollama |
 21  
 22  </div>
 23  
 24  `OllamaDocumentEmbedder` computes the embeddings of a list of documents and stores the obtained vectors in the embedding field of each document. It uses embedding models compatible with the Ollama Library.
 25  
 26  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.
 27  
 28  ## Overview
 29  
 30  `OllamaTextEmbedder` should be used to embed a string. For embedding a list of documents, use the [`OllamaDocumentEmbedder`](ollamadocumentembedder.mdx).
 31  
 32  The component uses `http://localhost:11434` as the default URL as most available setups (Mac, Linux, Docker) default to port 11434.
 33  
 34  ### Compatible Models
 35  
 36  Unless specified otherwise while initializing this component, the default embedding model is "nomic-embed-text". See other possible pre-built models in Ollama's [library](https://ollama.ai/library). To load your own custom model, follow the [instructions](https://github.com/ollama/ollama/blob/main/docs/modelfile.md) from Ollama.
 37  
 38  ### Installation
 39  
 40  To start using this integration with Haystack, install the package with:
 41  
 42  ```shell
 43  pip install ollama-haystack
 44  ```
 45  
 46  Make sure that you have a running Ollama model (either through a docker container, or locally hosted). No other configuration is necessary as Ollama has the embedding API built in.
 47  
 48  ### Embedding Metadata
 49  
 50  Most embedded metadata contains information about the model name and type. You can pass [optional arguments](https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values), such as temperature, top_p, and others, to the Ollama generation endpoint.
 51  
 52  The name of the model used will be automatically appended as part of the metadata. An example payload using the nomic-embed-text model will look like this:
 53  
 54  ```python
 55  {"meta": {"model": "nomic-embed-text"}}
 56  ```
 57  
 58  ## Usage
 59  
 60  ### On its own
 61  
 62  ```python
 63  from haystack_integrations.components.embedders.ollama import OllamaTextEmbedder
 64  
 65  embedder = OllamaTextEmbedder()
 66  
 67  result = embedder.run(
 68      text="What do llamas say once you have thanked them? No probllama!",
 69  )
 70  
 71  print(result["embedding"])
 72  ```
 73  
 74  ### In a pipeline
 75  
 76  ```python
 77  from haystack import Document
 78  from haystack import Pipeline
 79  from haystack.document_stores.in_memory import InMemoryDocumentStore
 80  from cohere_haystack.embedders.text_embedder import OllamaTextEmbedder
 81  from cohere_haystack.embedders.document_embedder import OllamaDocumentEmbedder
 82  from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
 83  
 84  document_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
 85  
 86  documents = [
 87      Document(content="My name is Wolfgang and I live in Berlin"),
 88      Document(content="I saw a black horse running"),
 89      Document(content="Germany has many big cities"),
 90  ]
 91  
 92  document_embedder = OllamaDocumentEmbedder()
 93  documents_with_embeddings = document_embedder.run(documents)["documents"]
 94  document_store.write_documents(documents_with_embeddings)
 95  
 96  query_pipeline = Pipeline()
 97  query_pipeline.add_component("text_embedder", OllamaTextEmbedder())
 98  query_pipeline.add_component(
 99      "retriever",
100      InMemoryEmbeddingRetriever(document_store=document_store),
101  )
102  query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
103  
104  query = "Who lives in Berlin?"
105  
106  result = query_pipeline.run({"text_embedder": {"text": query}})
107  
108  print(result["retriever"]["documents"][0])
109  ```