pub struct CandleEmbedder {
device: Device,
model: Arc<RwLock<Option<BertModel>>>,
tokenizer: Arc<RwLock<Option<Tokenizer>>>,
config: Arc<RwLock<Option<Config>>>,
cache_dir: PathBuf,
initialized: Arc<RwLock<bool>>,
}Expand description
GTE-small embedder using Candle.
Fields§
§device: DeviceDevice to run inference on (CPU or CUDA)
model: Arc<RwLock<Option<BertModel>>>Loaded model
tokenizer: Arc<RwLock<Option<Tokenizer>>>Tokenizer
config: Arc<RwLock<Option<Config>>>Model configuration
cache_dir: PathBufCache directory for models
initialized: Arc<RwLock<bool>>Whether model is initialized
Implementations§
Source§impl CandleEmbedder
impl CandleEmbedder
Sourcepub fn with_device(cache_dir: PathBuf, device: Device) -> Self
pub fn with_device(cache_dir: PathBuf, device: Device) -> Self
Create with specific device.
Sourcepub async fn init(&self) -> Result<(), EmbedError>
pub async fn init(&self) -> Result<(), EmbedError>
Initialize the model (download if needed, load into memory).
Sourcefn mean_pooling(
&self,
token_embeddings: &Tensor,
attention_mask: &Tensor,
) -> Result<Tensor, EmbedError>
fn mean_pooling( &self, token_embeddings: &Tensor, attention_mask: &Tensor, ) -> Result<Tensor, EmbedError>
Mean pooling with attention mask.
Sourcefn normalize(&self, embeddings: &Tensor) -> Result<Tensor, EmbedError>
fn normalize(&self, embeddings: &Tensor) -> Result<Tensor, EmbedError>
L2 normalize embeddings.
Sourceasync fn encode_batch(
&self,
texts: &[&str],
normalize: bool,
) -> Result<Vec<EmbeddingOutput>, EmbedError>
async fn encode_batch( &self, texts: &[&str], normalize: bool, ) -> Result<Vec<EmbeddingOutput>, EmbedError>
Encode a batch of texts.
Trait Implementations§
Source§impl Embedder for CandleEmbedder
impl Embedder for CandleEmbedder
Source§fn model_name(&self) -> &str
fn model_name(&self) -> &str
Model name/identifier.
Source§fn max_tokens(&self) -> usize
fn max_tokens(&self) -> usize
Maximum tokens per input.
Source§fn modalities(&self) -> &[Modality]
fn modalities(&self) -> &[Modality]
Supported modalities.
Source§fn embed_text<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
texts: &'life1 [&'life2 str],
config: &'life3 EmbeddingConfig,
) -> Pin<Box<dyn Future<Output = Result<Vec<EmbeddingOutput>, EmbedError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn embed_text<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
texts: &'life1 [&'life2 str],
config: &'life3 EmbeddingConfig,
) -> Pin<Box<dyn Future<Output = Result<Vec<EmbeddingOutput>, EmbedError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Embed text content.
Source§fn embed_query<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
query: &'life1 str,
config: &'life2 EmbeddingConfig,
) -> Pin<Box<dyn Future<Output = Result<EmbeddingOutput, EmbedError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn embed_query<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
query: &'life1 str,
config: &'life2 EmbeddingConfig,
) -> Pin<Box<dyn Future<Output = Result<EmbeddingOutput, EmbedError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Embed a query (may use different instruction).
Source§fn embed_image<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_image_data: &'life1 [u8],
_config: &'life2 EmbeddingConfig,
) -> Pin<Box<dyn Future<Output = Result<EmbeddingOutput, EmbedError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn embed_image<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_image_data: &'life1 [u8],
_config: &'life2 EmbeddingConfig,
) -> Pin<Box<dyn Future<Output = Result<EmbeddingOutput, EmbedError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
Embed image content.
Auto Trait Implementations§
impl Freeze for CandleEmbedder
impl !RefUnwindSafe for CandleEmbedder
impl Send for CandleEmbedder
impl Sync for CandleEmbedder
impl Unpin for CandleEmbedder
impl !UnwindSafe for CandleEmbedder
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
Mutably borrows from an owned value. Read more
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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>
Converts
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>
Converts
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