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.

§归一化契约(P2-8)

所有返回的向量都是 L2 归一化的单位向量(零向量除外),与 provider 内部 是否已归一化无关。这保证下游无论用 cosine、点积还是 L2 距离,结果都不随 provider 漂移。HTTP provider 在返回前统一调用 l2_normalize

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

§语义约定(P1-1)
  • 任一文本为空或全空白(trim().is_empty())→ Err(EmbeddingError::EmptyInput)
  • 空切片 &[] 视为“没有要嵌入的文本“→ Ok(vec![])(无事可做不算错误)。

默认实现循环调用 Self::embed_query,并前置统一判空;各 provider 覆写时须遵循同样的契约,不得再各自分裂。

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§