pub struct OnnxEmbeddingEngine { /* private fields */ }Expand description
ONNX-based embedding engine for local inference
Wraps ONNX Runtime session and HuggingFace tokenizer. Based on examples/embeddings.rs with proper tokenization for Python parity.
Implementations§
Source§impl OnnxEmbeddingEngine
impl OnnxEmbeddingEngine
Sourcepub fn new(config: OnnxEmbeddingConfig) -> EmbeddingResult<Self>
pub fn new(config: OnnxEmbeddingConfig) -> EmbeddingResult<Self>
Create a new ONNX embedding engine
Initializes ONNX Runtime, loads the model, and downloads/caches the tokenizer.
§Arguments
config- Engine configuration with model path and tokenizer model ID
§Returns
- Initialized engine or error
§Errors
- Returns error if model file not found, ONNX Runtime init fails, or tokenizer download fails
§Example
let config = OnnxEmbeddingConfig::bge_small("./target/models");
let engine = OnnxEmbeddingEngine::new(config)?;Sourcepub async fn with_auto_download(
config: OnnxEmbeddingConfig,
) -> EmbeddingResult<Self>
pub async fn with_auto_download( config: OnnxEmbeddingConfig, ) -> EmbeddingResult<Self>
Create a new ONNX embedding engine with automatic model downloading
Downloads model and tokenizer from HuggingFace Hub if not found locally. This is the recommended constructor for most use cases.
§Arguments
config- Engine configuration with model path and tokenizer model ID
§Returns
- Initialized engine or error
§Errors
- Returns error if download fails, ONNX Runtime init fails, or tokenizer load fails
§Example
let config = OnnxEmbeddingConfig::bge_small("./target/models");
let engine = OnnxEmbeddingEngine::with_auto_download(config).await?;Trait Implementations§
Source§impl EmbeddingEngine for OnnxEmbeddingEngine
impl EmbeddingEngine for OnnxEmbeddingEngine
Source§fn embed<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
texts: &'life1 [&'life2 str],
) -> Pin<Box<dyn Future<Output = EmbeddingResult<Vec<Vec<f32>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn embed<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
texts: &'life1 [&'life2 str],
) -> Pin<Box<dyn Future<Output = EmbeddingResult<Vec<Vec<f32>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Embed texts, splitting into config.batch_size sub-batches so the ONNX
session never receives an unbounded batch. A transformer’s activation
memory scales with batch × seq_len²; embedding a whole corpus in one
call (several thousand chunks) would allocate tens of GB and OOM.
Sub-batching keeps peak memory flat regardless of how many texts are
passed.
Source§fn dimension(&self) -> usize
fn dimension(&self) -> usize
Source§fn batch_size(&self) -> usize
fn batch_size(&self) -> usize
Source§fn max_sequence_length(&self) -> usize
fn max_sequence_length(&self) -> usize
Auto Trait Implementations§
impl Freeze for OnnxEmbeddingEngine
impl RefUnwindSafe for OnnxEmbeddingEngine
impl Send for OnnxEmbeddingEngine
impl Sync for OnnxEmbeddingEngine
impl Unpin for OnnxEmbeddingEngine
impl UnsafeUnpin for OnnxEmbeddingEngine
impl UnwindSafe for OnnxEmbeddingEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more