Skip to main content

Embeddings

Trait Embeddings 

Source
pub trait Embeddings: Send + Sync {
    // Required methods
    fn embed_query<'life0, 'life1, 'async_trait>(
        &'life0 self,
        text: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<f32>, EmbeddingError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn dimension(&self) -> usize;
    fn model_name(&self) -> &str;

    // Provided method
    fn embed_documents<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        texts: &'life1 [&'life2 str],
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>, EmbeddingError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             Self: 'async_trait { ... }
}
Expand description

Embedding model trait — re-exported from lc-embeddings.

Used by vector store implementations that need to embed documents on the fly (e.g., Pinecone’s upsert method). Embedding model trait

Defines the interface for generating text embedding vectors.

§Normalization contract (P2-8)

All returned vectors are L2-normalized unit vectors (except zero vectors), regardless of whether the provider normalizes internally. This guarantees downstream cosine, dot-product, or L2-distance results do not drift across providers. HTTP providers call l2_normalize uniformly before returning.

Required Methods§

Source

fn embed_query<'life0, 'life1, 'async_trait>( &'life0 self, text: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<f32>, EmbeddingError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Generate an embedding vector for a single text.

§Arguments
  • text - Input text
§Returns

Embedding vector (typically 1536 dimensions or higher)

Source

fn dimension(&self) -> usize

Get the embedding vector dimension.

Source

fn model_name(&self) -> &str

Get the model name.

Provided Methods§

Source

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

Generate embedding vectors for multiple documents.

§Arguments
  • texts - List of input texts
§Returns

List of embedding vectors

§Semantic contract (P1-1)
  • Any empty or all-whitespace text (trim().is_empty()) → Err(EmbeddingError::EmptyInput);
  • An empty slice &[] is treated as “no text to embed” → Ok(vec![]) (nothing to do is not an error).

The default implementation loops over Self::embed_query with a uniform emptiness check up front; providers that override it must follow the same contract and not diverge.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§