subtitlesEmbedding Model (EM) Invoker

gllm-inferencearrow-up-right | Tutorial: Embedding Model (EM) Invoker | Use Case: Your First RAG Pipeline| API Referencearrow-up-right

What’s an EM Invoker?

The EM invoker is a unified interface designed to help you to convert inputs into into numerical vector representations. In this tutorial, you'll learn how to invoke an embedding model using OpenAIEMInvoker in just a few lines of code. You can also explore other types of EM Invokers, available herearrow-up-right.

chevron-rightPrerequisiteshashtag

This example specifically requires completion of all setup steps listed on the Prerequisites page.

Available EM Invokers

The EM invoker provides the following built-in implementations:

  1. AzureOpenAIEMInvoker

  2. BedrockEMInvoker

  3. CohereEMInvoker

  4. GoogleEMInvoker

  5. JinaEMInvoker

  6. LangChainEMInvoker

  7. OpenAIEMInvoker

  8. TwelveLabsEMInvoker

  9. VoyageEMInvoker

Installation

Quickstart

Let’s jump into a basic example using OpenAIEMInvoker. We’ll ask the model a simple question and print the response.

Expected Output

That’s it! You've just made your first successful embedding model call using OpenAIEMInvoker. Fast, clean, and ready to scale into more complex use cases!


Batch Invocation

EM invokers can also be used to vectorize multiple inputs at once. This can be done by providing a list of inputs. When processing a list of inputs, the output will be a list of vectors, where each element corresponds to an element in the input list. Let's try it!

Expected Output


Multimodal Input

Some embedding model providers, such as Voyagearrow-up-right, also have the capability to vectorize more than just text! Let's try to embed an image using VoyageEMInvoker. To do this, you can get a Voyage API keyarrow-up-right and export it as an environment variable.

Then, we can embed multimodal inputs such as image by loading them as an Attachment object!

Expected Output

And there it is, you've successfully vectorized an image into its numerical vector representations!


Text Truncation

Text truncation allows you to control how text inputs are handled when they exceed the maximum length supported by the embedding model. This is particularly useful when dealing with long documents or when you need to ensure consistent input lengths.

Truncation can be configured using the TruncationConfig class with the following parameters:

  1. max_length: Maximum number of characters to keep (required)

  2. truncate_side: Which side to truncate from (defaults to TruncateSide.RIGHT)

    • TruncateSide.LEFT: Keep the end of the text, truncate from the beginning

    • TruncateSide.RIGHT: Keep the beginning of the text, truncate from the end (default)


Vector Fusion

Vector fusion is a feature that enables fusing multiple results into a single vector. This allows the EM invoker to embed mixed modality contents, represented as tuples of contents.

This feature can be enabled by providing a vector fuser to the vector_fuser parameter. Two vector fuser strategies are available:

  • AverageVectorFuser — fuses vectors by averaging them element-wise.

  • SumVectorFuser — fuses vectors by summing them element-wise.

Example using SumVectorFuser:

Expected Output

circle-info

Some EM invokers, such as the VoyageEMInvoker, has a built-in vector fusion capability, essentially allowing it to perform vector fusion without the need of providing any vector fuser.


Retry & Timeout

Retry & timeout functionality provides robust error handling and reliability for embedding model interactions. It allows you to automatically retry failed requests and set time limits for operations, ensuring your applications remain responsive and resilient to network issues or API failures.

Retry & timeout can be configured via the RetryConfig class' parameters:

  1. max_retries: Maximum number of retry attempts (defaults to 3 maximum retry attempts).

  2. timeout: Maximum time in seconds to wait for each request (defaults to 30.0 seconds). To disable timeout, this parameter can be set to None.

You can also configure other parameters available herearrow-up-right. Now let's try to apply it to our EM invoker!


If you encounter errors, refer to the Troubleshooting Guide for detailed explanations of common errors and how to resolve them.


And there we go! You've successfully completed the tutorial of using EM invokers!

Last updated

Was this helpful?