Skip to content

Em invoker

Defines a base class for embedding model invokers used in Gen AI applications.

Authors

Henry Wicaksono (henry.wicaksono@gdplabs.id)

References

NONE

BaseEMInvoker(model_id, default_hyperparameters=None, valid_extensions_map=None, retry_config=None, langchain_kwargs=None)

Bases: ABC

A base class for embedding model invokers used in Gen AI applications.

The BaseEMInvoker class provides a framework for invoking embedding models.

Attributes:

Name Type Description
model_id str

The model ID of the embedding model.

model_provider str

The provider of the embedding model.

model_name str

The name of the embedding model.

default_hyperparameters dict[str, Any]

Default hyperparameters for invoking the embedding model.

retry_config RetryConfig

The retry configuration for the embedding model.

Initializes a new instance of the BaseEMInvoker class.

Parameters:

Name Type Description Default
model_id ModelId

The model ID of the embedding model.

required
default_hyperparameters dict[str, Any] | None

Default hyperparameters for invoking the embedding model. Defaults to None, in which case an empty dictionary is used.

None
valid_extensions_map dict[str, set[str]] | None

A dictionary mapping for validating the content type of the multimodal inputs. They keys are the mime types (e.g. "image") and the values are the set of valid file extensions for the corresponding mime type. Defaults to None, in which case an empty dictionary is used.

None
retry_config RetryConfig | None

The retry configuration for the embedding model. Defaults to None, in which case a default config with no retry and 30.0 seconds timeout is used.

None
langchain_kwargs dict[str, Any] | None

Additional keyword arguments to initiate the LangChain embedding model. Defaults to None.

None

model_id: str property

The model ID of the embedding model.

Returns:

Name Type Description
str str

The model ID of the embedding model.

model_name: str property

The name of the embedding model.

Returns:

Name Type Description
str str

The name of the embedding model.

model_provider: str property

The provider of the embedding model.

Returns:

Name Type Description
str str

The provider of the embedding model.

invoke(content, hyperparameters=None) async

Invokes the embedding model with the provided content or list of contents.

This method invokes the embedding model with the provided content or list of contents. It includes retry logic with exponential backoff for transient failures.

Parameters:

Name Type Description Default
content EMContent | list[EMContent]

The input or list of inputs to be embedded using the embedding model.

required
hyperparameters dict[str, Any] | None

A dictionary of hyperparameters for the embedding model. Defaults to None, in which case the default hyperparameters are used.

None

Returns:

Type Description
Vector | list[Vector]

Vector | list[Vector]: The vector representations of the input contents: 1. If the input is an EMContent, the output is a Vector. 2. If the input is a list[EMContent], the output is a list[Vector].

Raises:

Type Description
CancelledError

If the invocation is cancelled.

ModelNotFoundError

If the model is not found.

ProviderAuthError

If the model authentication fails.

ProviderInternalError

If the model internal error occurs.

ProviderInvalidArgsError

If the model parameters are invalid.

ProviderOverloadedError

If the model is overloaded.

ProviderRateLimitError

If the model rate limit is exceeded.

TimeoutError

If the invocation times out.

ValueError

If the input content is invalid.

to_langchain()

Converts the current embedding model invoker to an instance of LangChain Embeddings object.

This method converts the EM invoker to an instance of LangChain's Embeddings object. This method requires the appropriate langchain-<provider> package to be installed.

Returns:

Name Type Description
Embeddings Embeddings

An instance of LangChain Embeddings object.

Raises:

Type Description
ValueError

If langchain_module_name or langchain_class_name is missing.