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