Skip to main content

Embedder

Trait Embedder 

Source
pub trait Embedder: Send + Sync {
    // Required methods
    fn id(&self) -> &str;
    fn capabilities(&self, model: &ModelId) -> Option<EmbeddingCapabilities>;
    fn embed<'life0, 'async_trait>(
        &'life0 self,
        request: EmbedRequest,
    ) -> Pin<Box<dyn Future<Output = Result<Embeddings>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn validate<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _model: &'life1 ModelId,
    ) -> Pin<Box<dyn Future<Output = Access> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Available on crate feature embeddings only.
Expand description

Something that can turn text into vectors.

§Implementing one

Four rules, and the first two are the ones that fail quietly.

  1. The reply is index for index with the request. Sort by whatever index field the vendor sends rather than trusting arrival order. Getting this wrong pairs every document with another document’s vector, and every later query still returns results.
  2. Every Embedding carries the model that served it, from the reply rather than from the request. It is what stops two embedding spaces being compared as one.
  3. Do not invent usage. Usage::absent when the provider reported nothing, and Usage::embedding when it reported prompt tokens — never zeros.
  4. embed takes &self, so anything shared must be immutable after construction or behind an atomic, exactly as for crate::Provider.

Required Methods§

Source

fn id(&self) -> &str

A short name for this embedder, used in records and reports.

Source

fn capabilities(&self, model: &ModelId) -> Option<EmbeddingCapabilities>

What this model can do when reached through this embedder.

None for a model it does not recognise, which is a different answer from one it knows and has nothing to offer for.

Source

fn embed<'life0, 'async_trait>( &'life0 self, request: EmbedRequest, ) -> Pin<Box<dyn Future<Output = Result<Embeddings>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Turns some text into vectors.

§Errors

See crate::Error. A reply the provider sent and this crate could not read is an crate::Error::Unreadable, never a short list of vectors: a batch that comes back with fewer vectors than it had inputs cannot be lined up, and guessing which input was dropped is worse than failing.

Provided Methods§

Source

fn validate<'life0, 'life1, 'async_trait>( &'life0 self, _model: &'life1 ModelId, ) -> Pin<Box<dyn Future<Output = Access> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Whether a request for this model would be accepted, without sending one.

The same two rules as crate::Provider::validate: it must not cost anything, and it must not report a rejected credential as Access::Unknown. The default answers Unknown, which is the honest answer for an embedder with nothing free to ask.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl Embedder for GeminiEmbeddings

Available on crate feature gemini only.
Source§

impl Embedder for OpenAiEmbeddings

Available on crate feature openai and (crate features cli or openai) only.