Skip to main content

context_forge/semantic/
mod.rs

1//! Embedding abstraction for semantic search.
2//!
3//! The [`Embedder`] trait is always available. The [`FasEmbedder`] concrete
4//! implementation (backed by fastembed + ONNX Runtime) is compiled only when
5//! the `semantic` feature is enabled.
6
7/// Sync embedding interface.
8///
9/// ONNX inference is CPU-bound and cannot yield to the async runtime.
10/// Callers must wrap calls in [`tokio::task::spawn_blocking`].
11pub trait Embedder: Send + Sync {
12    /// Embed a single text into a dense float vector.
13    ///
14    /// # Errors
15    ///
16    /// Returns an error if the embedding model fails.
17    fn embed(&self, text: &str) -> crate::Result<Vec<f32>>;
18
19    /// Embed a batch of texts. Default implementation calls [`Self::embed`]
20    /// once per text; concrete types may override for batched inference.
21    ///
22    /// # Errors
23    ///
24    /// Returns an error if any embedding fails.
25    fn embed_batch(&self, texts: &[&str]) -> crate::Result<Vec<Vec<f32>>> {
26        texts.iter().map(|t| self.embed(t)).collect()
27    }
28}
29
30#[cfg(feature = "semantic")]
31mod fastembed_impl;
32#[cfg(feature = "semantic")]
33pub use fastembed_impl::FasEmbedder;