Skip to main content

TextEmbedder

Trait TextEmbedder 

Source
pub trait TextEmbedder: Send + Sync {
    // Required method
    fn embed_texts(
        &self,
        texts: &[String],
        mode: EmbedMode,
    ) -> Result<Vec<Vec<f32>>>;

    // Provided methods
    fn embed_text(&self, text: &str, mode: EmbedMode) -> Result<Vec<f32>> { ... }
    fn model_id(&self) -> Option<&str> { ... }
    fn dimension(&self) -> Option<usize> { ... }
    fn capabilities(&self) -> TextEmbedderCapabilities { ... }
}
Expand description

Minimal interface for “text → dense vector” encoders (bi-encoder style).

This covers the common “sentence embedding” family: one vector per input string.

Important: there are multiple kinds of “embeddings” used in retrieval:

  • Dense sentence embeddings (this trait): one (d)-dim vector per string.
  • Dense token embeddings / late interaction (e.g. ColBERT): many vectors per string.
  • Sparse embeddings (e.g. SPLADE): a weighted sparse vector over vocabulary IDs.
  • Binary / quantized embeddings: compressed representations for ANN speed/memory.

We start with the dense-sentence contract because it’s the smallest stable surface that many parts of the workspace can share (iksh, chunking, retrieval prototypes).

Required Methods§

Source

fn embed_texts( &self, texts: &[String], mode: EmbedMode, ) -> Result<Vec<Vec<f32>>>

Embed texts into vectors.

Recommended invariant for backends that can afford it: L2-normalize outputs so cosine similarity equals dot product and downstream fusion is less brittle.

Provided Methods§

Source

fn embed_text(&self, text: &str, mode: EmbedMode) -> Result<Vec<f32>>

Convenience: embed a single text, avoiding the &[s.to_string()][0].clone() boilerplate.

Source

fn model_id(&self) -> Option<&str>

Optional: model identifier for debugging and provenance.

Source

fn dimension(&self) -> Option<usize>

Optional: embedding dimension as returned by this embedder.

Notes:

  • If you wrap an embedder with apply_output_dim(Some(d)), this should become Some(d).
  • Many remote backends can’t report this without a request; returning None is fine.
Source

fn capabilities(&self) -> TextEmbedderCapabilities

Optional: backend capability declaration.

This exists to design around “silent drift” failure modes:

  • prompt applied client-side vs server-side vs internally
  • whether EmbedMode is actually used
  • whether outputs are L2-normalized

Backends that don’t override this should be treated as “unknown” by callers.

Implementations on Foreign Types§

Source§

impl<T: TextEmbedder + ?Sized> TextEmbedder for Box<T>

Implementors§