pub struct Embedder { /* private fields */ }Expand description
Text embedding generator using nomic-embed-text-v1.5
Automatically downloads the model from HuggingFace Hub on first use. Detects GPU availability and uses CUDA/TensorRT when available.
§Example
use cqs::Embedder;
let mut embedder = Embedder::new()?;
let embedding = embedder.embed_query("parse configuration file")?;
println!("Embedding dimension: {}", embedding.len()); // 768Implementations§
Source§impl Embedder
impl Embedder
Sourcepub fn new() -> Result<Self, EmbedderError>
pub fn new() -> Result<Self, EmbedderError>
Create a new embedder, downloading the model if necessary
Automatically detects GPU and uses CUDA/TensorRT when available. Falls back to CPU if no GPU is found.
Note: ONNX session is lazy-loaded on first embedding request (~500ms).
Sourcepub fn new_cpu() -> Result<Self, EmbedderError>
pub fn new_cpu() -> Result<Self, EmbedderError>
Create a CPU-only embedder
Use this for single-query embedding where CPU is faster than GPU due to CUDA context setup overhead. GPU only helps for batch embedding.
Sourcepub fn token_count(&self, text: &str) -> Result<usize, EmbedderError>
pub fn token_count(&self, text: &str) -> Result<usize, EmbedderError>
Count tokens in a text
Sourcepub fn split_into_windows(
&self,
text: &str,
max_tokens: usize,
overlap: usize,
) -> Result<Vec<(String, u32)>, EmbedderError>
pub fn split_into_windows( &self, text: &str, max_tokens: usize, overlap: usize, ) -> Result<Vec<(String, u32)>, EmbedderError>
Split text into overlapping windows of max_tokens with overlap tokens of context. Returns Vec of (window_content, window_index). If text fits in max_tokens, returns single window with index 0.
Sourcepub fn embed_documents(
&mut self,
texts: &[&str],
) -> Result<Vec<Embedding>, EmbedderError>
pub fn embed_documents( &mut self, texts: &[&str], ) -> Result<Vec<Embedding>, EmbedderError>
Embed documents (code chunks). Adds “passage: “ prefix for E5.
Sourcepub fn embed_query(&mut self, text: &str) -> Result<Embedding, EmbedderError>
pub fn embed_query(&mut self, text: &str) -> Result<Embedding, EmbedderError>
Embed a query. Adds “query: “ prefix for E5. Uses LRU cache for repeated queries.
Sourcepub fn provider(&self) -> ExecutionProvider
pub fn provider(&self) -> ExecutionProvider
Get the execution provider being used
Sourcepub fn batch_size(&self) -> usize
pub fn batch_size(&self) -> usize
Get the batch size
Sourcepub fn warm(&mut self) -> Result<(), EmbedderError>
pub fn warm(&mut self) -> Result<(), EmbedderError>
Warm up the model with a dummy inference
Auto Trait Implementations§
impl !Freeze for Embedder
impl RefUnwindSafe for Embedder
impl Send for Embedder
impl Sync for Embedder
impl Unpin for Embedder
impl UnwindSafe for Embedder
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
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