Skip to main content

EmbeddingService

Trait EmbeddingService 

Source
pub trait EmbeddingService: Send + Sync {
    // Required methods
    fn embed<'life0, 'life1, 'async_trait>(
        &'life0 self,
        texts: &'life1 [String],
        model: EmbeddingModel,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn supports_model(&self, model: EmbeddingModel) -> bool;
    fn name(&self) -> &'static str;

    // Provided methods
    fn embed_one<'life0, 'life1, 'async_trait>(
        &'life0 self,
        text: &'life1 str,
        model: EmbeddingModel,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<f32>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn embed_with_role<'life0, 'life1, 'async_trait>(
        &'life0 self,
        texts: &'life1 [String],
        model: EmbeddingModel,
        role: EmbeddingRole,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn embed_query<'life0, 'life1, 'async_trait>(
        &'life0 self,
        texts: &'life1 [String],
        model: EmbeddingModel,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn embed_passage<'life0, 'life1, 'async_trait>(
        &'life0 self,
        texts: &'life1 [String],
        model: EmbeddingModel,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn model_config(&self, model: EmbeddingModel) -> ModelConfig { ... }
}
Expand description

Stable: external consumers may depend on this; breaking changes require a SemVer bump.

Async interface for producing one embedding per input text.

See docs/service.md for role handling and implementation requirements.

Required Methods§

Source

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

Stable: generate embeddings for multiple texts.

Returns a vector of embeddings, one for each input text, in the same order. Applies no role-specific prompt prefix (equivalent to Generic role). Use EmbeddingService::embed_query / EmbeddingService::embed_passage for asymmetric retrieval models.

Source

fn supports_model(&self, model: EmbeddingModel) -> bool

Stable: check if the service supports a given model.

Source

fn name(&self) -> &'static str

Stable: get the name/identifier of this service.

Provided Methods§

Source

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

Stable: generate an embedding for a single text.

This is a convenience method that calls embed with a single-element slice.

Source

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

Stable: embed texts under a retrieval role, applying that role’s instruction.

The published length cap applies to the text the caller supplies, so this validates first and prepends the instruction second. Doing it the other way round charges the caller for bytes the service itself added, which rejects text that is within the documented limit.

Implementors that enforce a text-length cap inside EmbeddingService::embed receive prepared text here, which is longer than the caller’s by up to EmbeddingModel::max_instruction_bytes. Size that guard accordingly, or override this method to reach the backend without passing through it.

See docs/service.md for role and cache behavior.

Source

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

Stable: embed query texts after applying the model’s query instruction.

See docs/service.md for role and cache behavior.

Source

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

Stable: embed passages after applying the model’s document instruction.

See docs/service.md for role and cache behavior.

Source

fn model_config(&self, model: EmbeddingModel) -> ModelConfig

Unstable: returns the effective configuration used for this model’s cache keys.

See docs/service.md for output-dimension behavior.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§