Skip to main content

Embedder

Trait Embedder 

Source
pub trait Embedder: Send + Sync {
    // Required methods
    fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>>;
    fn dimension(&self) -> usize;
    fn is_available(&self) -> bool;
    fn backend_name(&self) -> &'static str;

    // Provided methods
    fn embed(&self, text: &str) -> Result<Vec<f32>> { ... }
    fn embed_query(&self, query: &str) -> Result<Vec<f32>> { ... }
    fn cache_namespace(&self) -> String { ... }
    fn preferred_max_batch_texts(&self) -> usize { ... }
}
Expand description

Trait for text embedding providers.

Implementations:

  • HttpEmbedder — llama-server /v1/embeddings endpoint
  • StubEmbedder — hash-based pseudo-embeddings (for testing)

Required Methods§

Source

fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>>

Embed a batch of texts into f32 vectors.

Returns one vector per input text. All vectors have the same dimensionality.

Source

fn dimension(&self) -> usize

Get the embedding dimensionality.

Source

fn is_available(&self) -> bool

Whether this embedder is available (model loaded, server reachable).

Source

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

Name of this embedder backend.

Provided Methods§

Source

fn embed(&self, text: &str) -> Result<Vec<f32>>

Embed a single text.

Source

fn embed_query(&self, query: &str) -> Result<Vec<f32>>

Embed a single query (alias for embed).

Source

fn cache_namespace(&self) -> String

Identity used to namespace the persistent embedding cache.

Vectors for identical text differ across models, quantization, and endpoints, so the cache key must carry this — switching models must never serve stale vectors. Default is the backend name.

Source

fn preferred_max_batch_texts(&self) -> usize

Preferred batch granularity in TEXTS for batch write paths.

The write path’s char-chunking exists for HTTP token limits; local engines have no such limit and want bigger batches so session pools fan out efficiently (4-7 texts per call starves each shard). usize::MAX = char-chunking only (the HTTP shape).

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§