Skip to main content

EmbeddingProvider

Trait EmbeddingProvider 

Source
pub trait EmbeddingProvider: Send + Sync {
    // Required methods
    fn embed<'life0, 'life1, 'async_trait>(
        &'life0 self,
        texts: &'life1 [String],
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn dimensions(&self) -> usize;

    // Provided method
    fn model_id(&self) -> Option<&str> { ... }
}
Expand description

Generates vector embeddings from text content.

§Example

use adk_memory::EmbeddingProvider;

struct Lengths;

#[async_trait::async_trait]
impl EmbeddingProvider for Lengths {
    async fn embed(&self, texts: &[String]) -> adk_core::Result<Vec<Vec<f32>>> {
        Ok(texts.iter().map(|text| vec![text.len() as f32, 1.0]).collect())
    }

    fn dimensions(&self) -> usize {
        2
    }

    fn model_id(&self) -> Option<&str> {
        Some("example/lengths")
    }
}

assert_eq!(Lengths.model_id(), Some("example/lengths"));

Required Methods§

Source

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

Generate embeddings for a batch of texts.

Source

fn dimensions(&self) -> usize

Return the dimensionality of the embedding vectors.

Provided Methods§

Source

fn model_id(&self) -> Option<&str>

A stable id of the model behind the vectors, such as openai/text-embedding-3-small, so a caller that stores vectors never compares one model’s vectors with another’s that happen to share a length.

The default, None, leaves callers to tell models apart by dimensions alone.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§