EmbeddingModel

Trait EmbeddingModel 

Source
pub trait EmbeddingModel<VALUE>: Send + Sync
where VALUE: Send + Sync,
{ // Required methods fn provider(&self) -> &str; fn model_id(&self) -> &str; fn max_embeddings_per_call<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<usize>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn supports_parallel_calls<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; fn do_embed<'life0, 'async_trait>( &'life0 self, options: EmbedOptions<VALUE>, ) -> Pin<Box<dyn Future<Output = Result<EmbedResponse, Box<dyn Error + Sync + Send>>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; // Provided methods fn specification_version(&self) -> &str { ... } fn embed( &self, values: impl Into<Vec<VALUE>>, ) -> EmbedBuilder<'_, Self, VALUE> where Self: Sized { ... } }
Expand description

The core trait for embedding model implementations following the v3 specification.

This trait defines the interface that all embedding models must implement to generate vector representations of input values. It supports a generic VALUE type to enable future extensibility beyond text embeddings (e.g., image embeddings, audio embeddings).

§Generic Parameters

  • VALUE: The type of values that this model can embed (e.g., String for text, potentially Vec<u8> for images in future versions). Must be Send + Sync.

§Design Notes

Implementations should be stateless or use Arc/Box for shared state management. The trait provides metadata about the model’s capabilities and limits, allowing client code to optimize request batching and concurrency strategies.

Required Methods§

Source

fn provider(&self) -> &str

Returns the provider identifier for this embedding model.

This is typically the name of the service provider (e.g., "openai", "anthropic", "cohere"). It identifies which external service provides the embedding functionality.

§Examples
  • "openai" - For OpenAI’s embedding models
  • "cohere" - For Cohere’s embedding models
Source

fn model_id(&self) -> &str

Returns the provider-specific model identifier.

This is the model identifier as used by the provider’s API. Different providers use different naming conventions for their models.

§Examples
  • "text-embedding-3-small" - OpenAI’s small embedding model
  • "text-embedding-3-large" - OpenAI’s large embedding model
  • "embed-english-v3.0" - Cohere’s English embedding model
Source

fn max_embeddings_per_call<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<usize>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Returns the maximum number of embeddings that can be generated in a single API call.

This defines the batch size limit for embedding requests to this model. When embedding large numbers of values, clients should respect this limit by splitting requests into appropriately sized batches.

§Returns
  • Some(n) - The maximum number of embeddings per call is n
  • None - The model has no documented batch size limit
Source

fn supports_parallel_calls<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Indicates whether this model supports parallel embedding requests.

When true, multiple embedding requests can be sent to the service concurrently for improved throughput. When false, requests should be processed sequentially to avoid rate limiting or service errors.

§Returns

true if the model can handle multiple concurrent embedding requests, false otherwise.

Source

fn do_embed<'life0, 'async_trait>( &'life0 self, options: EmbedOptions<VALUE>, ) -> Pin<Box<dyn Future<Output = Result<EmbedResponse, Box<dyn Error + Sync + Send>>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Performs the actual embedding generation for the provided values.

This is the internal method that implements the core embedding functionality. Clients should prefer using the embed() builder method instead of calling this directly. The do_ prefix indicates this is an internal implementation detail.

§Arguments
  • options - The embedding request options, including values, headers, and provider options
§Returns

A Result containing the EmbedResponse with generated embeddings on success, or an error if embedding generation fails.

§Implementation Notes

Implementations of this method should:

  • Validate the input values according to model constraints
  • Respect the provider_options and headers in the request options
  • Return meaningful error messages for failures
  • Handle API-specific response processing and error handling

Provided Methods§

Source

fn specification_version(&self) -> &str

Returns the API specification version implemented by this model.

This returns the version of the embedding model specification being used. Currently always returns "v3".

Source

fn embed(&self, values: impl Into<Vec<VALUE>>) -> EmbedBuilder<'_, Self, VALUE>
where Self: Sized,

Creates a builder for constructing and executing an embedding request.

This is the primary way to initiate an embedding request. It provides a fluent interface for configuring the request before sending it to the model.

§Arguments
  • values - The input values to be embedded, converted into a vector
§Returns

An EmbedBuilder that can be further configured with additional options before sending.

§Example
let response = model.embed(vec!["hello world"])
    .headers(custom_headers)
    .send()
    .await?;

Implementations on Foreign Types§

Source§

impl<T, VALUE> EmbeddingModel<VALUE> for &T
where T: EmbeddingModel<VALUE> + ?Sized, VALUE: Send + Sync + 'static,

Source§

fn specification_version(&self) -> &str

Source§

fn provider(&self) -> &str

Source§

fn model_id(&self) -> &str

Source§

fn max_embeddings_per_call<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<usize>> + Send + 'async_trait>>
where 'life0: 'async_trait, &T: 'async_trait,

Source§

fn supports_parallel_calls<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where 'life0: 'async_trait, &T: 'async_trait,

Source§

fn do_embed<'life0, 'async_trait>( &'life0 self, options: EmbedOptions<VALUE>, ) -> Pin<Box<dyn Future<Output = Result<EmbedResponse, Box<dyn Error + Sync + Send>>> + Send + 'async_trait>>
where 'life0: 'async_trait, &T: 'async_trait,

Source§

impl<T, VALUE> EmbeddingModel<VALUE> for Box<T>
where T: EmbeddingModel<VALUE> + ?Sized, VALUE: Send + Sync + 'static,

Source§

fn specification_version(&self) -> &str

Source§

fn provider(&self) -> &str

Source§

fn model_id(&self) -> &str

Source§

fn max_embeddings_per_call<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<usize>> + Send + 'async_trait>>
where 'life0: 'async_trait, Box<T>: 'async_trait,

Source§

fn supports_parallel_calls<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where 'life0: 'async_trait, Box<T>: 'async_trait,

Source§

fn do_embed<'life0, 'async_trait>( &'life0 self, options: EmbedOptions<VALUE>, ) -> Pin<Box<dyn Future<Output = Result<EmbedResponse, Box<dyn Error + Sync + Send>>> + Send + 'async_trait>>
where 'life0: 'async_trait, Box<T>: 'async_trait,

Source§

impl<T, VALUE> EmbeddingModel<VALUE> for Arc<T>
where T: EmbeddingModel<VALUE> + ?Sized, VALUE: Send + Sync + 'static,

Source§

fn specification_version(&self) -> &str

Source§

fn provider(&self) -> &str

Source§

fn model_id(&self) -> &str

Source§

fn max_embeddings_per_call<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<usize>> + Send + 'async_trait>>
where 'life0: 'async_trait, Arc<T>: 'async_trait,

Source§

fn supports_parallel_calls<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where 'life0: 'async_trait, Arc<T>: 'async_trait,

Source§

fn do_embed<'life0, 'async_trait>( &'life0 self, options: EmbedOptions<VALUE>, ) -> Pin<Box<dyn Future<Output = Result<EmbedResponse, Box<dyn Error + Sync + Send>>> + Send + 'async_trait>>
where 'life0: 'async_trait, Arc<T>: 'async_trait,

Implementors§