optimumtextembedder.mdx
  1  ---
  2  title: "OptimumTextEmbedder"
  3  id: optimumtextembedder
  4  slug: "/optimumtextembedder"
  5  description: "A component to embed text using models loaded with the Hugging Face Optimum library."
  6  ---
  7  
  8  # OptimumTextEmbedder
  9  
 10  A component to embed text using models loaded with the Hugging Face Optimum 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)                                            |
 19  | **API reference**                      | [Optimum](/reference/integrations-optimum)                                                       |
 20  | **GitHub link**                        | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/optimum |
 21  
 22  </div>
 23  
 24  ## Overview
 25  
 26  `OptimumTextEmbedder` embeds text strings using models loaded with the [HuggingFace Optimum](https://huggingface.co/docs/optimum/index) library. It uses the [ONNX runtime](https://onnxruntime.ai/) for high-speed inference.
 27  
 28  The default model is `sentence-transformers/all-mpnet-base-v2`.
 29  
 30  Similarly to other Embedders, this component allows adding prefixes (and suffixes) to include instructions. For more details, refer to the component’s API reference.
 31  
 32  There are three useful parameters specific to the Optimum Embedder that you can control with various modes:
 33  
 34  - [Pooling](/reference/integrations-optimum#optimumembedderpooling): generate a fixed-sized sentence embedding from a variable-sized sentence embedding
 35  - [Optimization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/optimization): apply graph optimization to the model and improve inference speed
 36  - [Quantization](https://huggingface.co/docs/optimum/onnxruntime/usage_guides/quantization): reduce the computational and memory costs
 37  
 38  Find all the available mode details in our Optimum [API Reference](/reference/integrations-optimum).
 39  
 40  ### Authentication
 41  
 42  Authentication with a Hugging Face API Token is only required to access private or gated models through Serverless Inference API or the Inference Endpoints.
 43  
 44  The component uses an `HF_API_TOKEN` or `HF_TOKEN` environment variable, or you can pass a Hugging Face API token at initialization. See our [Secret Management](../../concepts/secret-management.mdx) page for more information.
 45  
 46  ## Usage
 47  
 48  To start using this integration with Haystack, install it with:
 49  
 50  ```shell
 51  pip install optimum-haystack
 52  ```
 53  
 54  ### On its own
 55  
 56  ```python
 57  from haystack_integrations.components.embedders.optimum import OptimumTextEmbedder
 58  
 59  text_to_embed = "I love pizza!"
 60  
 61  text_embedder = OptimumTextEmbedder(model="sentence-transformers/all-mpnet-base-v2")
 62  text_embedder.warm_up()
 63  
 64  print(text_embedder.run(text_to_embed))
 65  
 66  ## {'embedding': [-0.07804739475250244, 0.1498992145061493,, ...]}
 67  ```
 68  
 69  ### In a pipeline
 70  
 71  Note that this example requires GPU support to execute.
 72  
 73  ```python
 74  from haystack import Pipeline
 75  
 76  from haystack_integrations.components.embedders.optimum import (
 77      OptimumTextEmbedder,
 78      OptimumEmbedderPooling,
 79      OptimumEmbedderOptimizationConfig,
 80      OptimumEmbedderOptimizationMode,
 81  )
 82  
 83  pipeline = Pipeline()
 84  embedder = OptimumTextEmbedder(
 85      model="intfloat/e5-base-v2",
 86      normalize_embeddings=True,
 87      onnx_execution_provider="CUDAExecutionProvider",
 88      optimizer_settings=OptimumEmbedderOptimizationConfig(
 89          mode=OptimumEmbedderOptimizationMode.O4,
 90          for_gpu=True,
 91      ),
 92      working_dir="/tmp/optimum",
 93      pooling_mode=OptimumEmbedderPooling.MEAN,
 94  )
 95  pipeline.add_component("embedder", embedder)
 96  
 97  results = pipeline.run(
 98      {
 99          "embedder": {
100              "text": "Ex profunditate antique doctrinae, Ad caelos supra semper, Hoc incantamentum evoco, draco apparet, Incantamentum iam transactum est",
101          },
102      },
103  )
104  
105  print(results["embedder"]["embedding"])
106  ```