Skip to main content

Embed

Trait Embed 

Source
pub trait Embed: Send + Sync {
    // Required method
    fn embed(&self, text: &str) -> Result<Vec<f32>>;

    // Provided methods
    fn embed_query(&self, text: &str) -> Result<Vec<f32>> { ... }
    fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>> { ... }
    fn is_degraded(&self) -> bool { ... }
}
Expand description

v0.7.0 L0.7 — minimal dyn-compatible trait that abstracts “produces embedding vectors” away from the concrete Embedder enum.

Introduced to unblock Tier B coverage closure on the MCP tool handlers (reflect, check_duplicate, store, recall, etc.): before this trait existed, those handlers took Option<&Embedder>, which forced every test exercising the Some(...) arm to construct a real candle/Ollama embedder — banned by the test playbook §4 “real LLM never in cargo test”. With dyn Embed the production Embedder AND the test-only MockEmbedder (in [test_support]) both satisfy the same handler signature, so unit tests can substitute the mock and cover the embedder-bearing branches without a network or model load.

Implementations are required to be Send + Sync so the trait object is safe to hand across tokio::task::spawn_blocking boundaries (as the daemon’s B3 family-embedding precompute does).

Bug memory: _v070_grand_slam/layer_0_7/bugs_surfaced/8f3443c5.

Required Methods§

Source

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

Produce a single embedding vector for text.

§Errors

Implementor-specific. The production Embedder returns anyhow::Error from candle / tokenizers / OllamaClient for I/O, tokenisation, or model-forward failures. The MockEmbedder never errors.

Provided Methods§

Source

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

Produce a single embedding vector for text used as a search query. Default implementation delegates to Embed::embed, which is correct for symmetric embedders (and the test MockEmbedder); the production Embedder overrides it so the asymmetric Ollama nomic backend applies the search_query: task prefix (#1520).

§Errors

Same as Embed::embed.

Source

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

Produce embedding vectors for a batch of texts. Default implementation calls Embed::embed in a loop; implementors may override to do native batching.

§Errors

Propagates the first per-text error from Embed::embed.

Source

fn is_degraded(&self) -> bool

#1598 / #1594 — true when the embedder’s most recent remote call failed (live-degraded posture). Default false (correct for local / mock embedders); the production Embedder overrides it for the remote variant so the capabilities surface reports a dead endpoint truthfully.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl Embed for Embedder

v0.7.0 L0.7 — Embed trait impl that delegates to the inherent Embedder::embed / Embedder::embed_batch methods. The inherent methods stay on Embedder verbatim so existing callers that hold a concrete &Embedder keep their fast path; the trait impl is purely additive and enables dyn Embed substitution for handler signatures (see Embed docs).