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
- Positional ordering. The returned batch has one row per input text,
in input order, all of
dimensionswidth and finite. - Bounded concurrency. The guest is single-threaded; fan-out across sub-batches lives here, bounded by the implementor.
- Count before sending. Call
EmbeddingTokenBudget::mark_sentwith the sub-batch’s estimate immediately before each outbound request, and never send if it refuses. Retries are outbound requests too. - Settle honestly. Record a
SubBatchSettlementper sent sub-batch per the module’s outcome table; unknown spend isindeterminate, not free. Return them on the batch or inside every post-send error (EmbeddingError::with_settlements). - Drop safety. The future may be dropped at any await. State the budget
holds must stay correct then: do
mark_sentand 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). - No input text in errors, and no provider response bodies.
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>>
Sourcefn models<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<Vec<EmbeddingModel>, EmbeddingError>> + Send + 'a>>
fn models<'a>( &'a self, ) -> Pin<Box<dyn Future<Output = Result<Vec<EmbeddingModel>, EmbeddingError>> + Send + 'a>>
The locally declared catalog, without network I/O.
Sourcefn max_input_bytes(&self, alias: &str) -> Option<u64>
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§
Sourcefn estimate_tokens(&self, _alias: &str, texts: &[String]) -> u64
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".