Skip to main content

EmbeddingProvider

Trait EmbeddingProvider 

Source
pub trait EmbeddingProvider: Send + Sync {
    // Required methods
    fn embed<'a>(
        &'a self,
        alias: &'a str,
        texts: &'a [String],
        purpose: Purpose,
        budget: &'a EmbeddingTokenBudget,
    ) -> Pin<Box<dyn Future<Output = Result<EmbeddingBatch, EmbeddingError>> + Send + 'a>>;
    fn models<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<EmbeddingModel>, EmbeddingError>> + Send + 'a>>;
    fn max_input_bytes(&self, alias: &str) -> Option<u64>;

    // Provided method
    fn estimate_tokens(&self, _alias: &str, texts: &[String]) -> u64 { ... }
}
Expand description

The embedder-provided embedding provider: resolves credentials, performs the requests, and reports what each cost.

§Obligations on every implementor

  1. Positional ordering. The returned batch has one row per input text, in input order, all of dimensions width and finite.
  2. Bounded concurrency. The guest is single-threaded; fan-out across sub-batches lives here, bounded by the implementor.
  3. Count before sending. Call EmbeddingTokenBudget::mark_sent with the sub-batch’s estimate immediately before each outbound request, and never send if it refuses. Retries are outbound requests too.
  4. Settle honestly. Record a SubBatchSettlement per sent sub-batch per the module’s outcome table; unknown spend is indeterminate, not free. Return them on the batch or inside every post-send error (EmbeddingError::with_settlements).
  5. Drop safety. The future may be dropped at any await. State the budget holds must stay correct then: do mark_sent and the send with no await between them that could skip the send but keep the hold (a hold without a send is merely conservative, never unsafe).
  6. No input text in errors, and no provider response bodies.

Required Methods§

Source

fn embed<'a>( &'a self, alias: &'a str, texts: &'a [String], purpose: Purpose, budget: &'a EmbeddingTokenBudget, ) -> Pin<Box<dyn Future<Output = Result<EmbeddingBatch, EmbeddingError>> + Send + 'a>>

Source

fn models<'a>( &'a self, ) -> Pin<Box<dyn Future<Output = Result<Vec<EmbeddingModel>, EmbeddingError>> + Send + 'a>>

The locally declared catalog, without network I/O.

Source

fn max_input_bytes(&self, alias: &str) -> Option<u64>

Per-alias input byte limit, consulted before dispatch. None means the alias is not served.

Provided Methods§

Source

fn estimate_tokens(&self, _alias: &str, texts: &[String]) -> u64

Token estimate for reserving texts against alias. The default is ceil(utf8_bytes / 3) per input; saturating.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§