Skip to main content

EmbeddingEngine

Trait EmbeddingEngine 

Source
pub trait EmbeddingEngine: Send + Sync {
    // Required methods
    fn embed<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        texts: &'life1 [&'life2 str],
    ) -> Pin<Box<dyn Future<Output = EmbeddingResult<Vec<Vec<f32>>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn dimension(&self) -> usize;
    fn batch_size(&self) -> usize;
    fn max_sequence_length(&self) -> usize;
}
Expand description

Core trait for text embedding engines

Provides async interface for embedding generation while allowing synchronous implementations (e.g., ONNX) to be wrapped via spawn_blocking.

All returned embeddings are L2-normalized to unit vectors for cosine similarity.

Required Methods§

Source

fn embed<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, texts: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = EmbeddingResult<Vec<Vec<f32>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Embed a batch of text strings into normalized vectors

§Arguments
  • texts - Slice of strings to embed
§Returns
  • Vector of embeddings, one per input text. Each embedding is L2-normalized.
§Errors
  • Returns error if tokenization, inference, or normalization fails
§Example
let texts = vec!["Hello world".to_string()];
let embeddings = engine.embed(&texts).await?;
assert_eq!(embeddings.len(), 1);
assert_eq!(embeddings[0].len(), engine.dimension());
Source

fn dimension(&self) -> usize

Get the dimensionality of embeddings produced by this engine

§Returns
  • Number of dimensions in output vectors (e.g., 384 for BGE-Small)
Source

fn batch_size(&self) -> usize

Get the optimal batch size for this engine

Batches larger than this should be chunked by the caller.

§Returns
  • Maximum number of texts to process in a single embed() call
Source

fn max_sequence_length(&self) -> usize

Get the maximum sequence length (in tokens) supported

Input texts will be truncated to this length during tokenization.

§Returns
  • Maximum token count (e.g., 512 for BGE-Small-v1.5)

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§