Skip to main content

EmbeddingModel

Trait EmbeddingModel 

Source
pub trait EmbeddingModel {
    type Error: Error + 'static;

    const MAX_DOCUMENTS: usize;

    // Required methods
    fn ndims(&self) -> usize;
    fn embed_texts(
        &self,
        texts: Vec<String>,
    ) -> impl Future<Output = Result<Vec<Embedding>, Self::Error>>;

    // Provided method
    fn embed_text(
        &self,
        text: &str,
    ) -> impl Future<Output = Result<Embedding, Self::Error>> { ... }
}
Expand description

Trait for models that can generate vector embeddings from text.

Implement this for each provider. The EmbeddingsBuilder uses MAX_DOCUMENTS to chunk requests so you never exceed the provider’s per-request batch limit.

§Example

use irig::embeddings::{EmbeddingModel, Embedding, EmbeddingError};

pub struct MyEmbedder<H> { client: H, model: String }

impl<H: HttpClient> EmbeddingModel for MyEmbedder<H> {
    const MAX_DOCUMENTS: usize = 100;
    type Error = EmbeddingError;

    fn ndims(&self) -> usize { 1536 }

    async fn embed_texts(&self, texts: Vec<String>) -> Result<Vec<Embedding>, EmbeddingError> {
        // call API, return one Embedding per input text (same order)
        todo!()
    }
}

Required Associated Constants§

Source

const MAX_DOCUMENTS: usize

Maximum number of texts the provider accepts in a single request.

Required Associated Types§

Source

type Error: Error + 'static

Required Methods§

Source

fn ndims(&self) -> usize

Dimensionality of the output vectors.

Source

fn embed_texts( &self, texts: Vec<String>, ) -> impl Future<Output = Result<Vec<Embedding>, Self::Error>>

Embed a batch of texts. Must return exactly one Embedding per input text, in the same order.

Provided Methods§

Source

fn embed_text( &self, text: &str, ) -> impl Future<Output = Result<Embedding, Self::Error>>

Embed a single text. Provided as a default for convenience.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§