pub struct CachedEmbeddingService<S> { /* private fields */ }Expand description
Unstable: caching strategy and constructor API may change; foundation-internal use only.
Caching wrapper around an embedding service.
Wraps any EmbeddingService implementation with LRU caching. Identical
texts (with the same model) will return cached embeddings instead of
recomputing.
§Example
use lattice_embed::{
CachedEmbeddingService, NativeEmbeddingService, EmbeddingService,
EmbeddingModel, EmbeddingCache,
};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let inner = Arc::new(NativeEmbeddingService::new());
let cached = CachedEmbeddingService::new(inner, 1000);
// First call - computes and caches
let emb1 = cached.embed_one("Hello", EmbeddingModel::default()).await?;
// Second call - returns from cache
let emb2 = cached.embed_one("Hello", EmbeddingModel::default()).await?;
assert_eq!(emb1, emb2);
Ok(())
}Implementations§
Source§impl<S: EmbeddingService> CachedEmbeddingService<S>
impl<S: EmbeddingService> CachedEmbeddingService<S>
Sourcepub fn new(inner: Arc<S>, cache_capacity: usize) -> Self
pub fn new(inner: Arc<S>, cache_capacity: usize) -> Self
Unstable: constructor signature may change when cache config becomes a struct.
§Arguments
inner- The underlying embedding servicecache_capacity- Maximum number of embeddings to cache
Sourcepub fn with_default_cache(inner: Arc<S>) -> Self
pub fn with_default_cache(inner: Arc<S>) -> Self
Unstable: constructor signature may change when cache config becomes a struct.
Sourcepub fn cache_stats(&self) -> CacheStats
pub fn cache_stats(&self) -> CacheStats
Unstable: returns internal CacheStats type which is itself Unstable.
Sourcepub fn clear_cache(&self)
pub fn clear_cache(&self)
Unstable: internal cache management; API subject to change.
Trait Implementations§
Source§impl<S: EmbeddingService + 'static> EmbeddingService for CachedEmbeddingService<S>
impl<S: EmbeddingService + 'static> EmbeddingService for CachedEmbeddingService<S>
Source§fn embed<'life0, 'life1, 'async_trait>(
&'life0 self,
texts: &'life1 [String],
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn embed<'life0, 'life1, 'async_trait>(
&'life0 self,
texts: &'life1 [String],
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<Vec<f32>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Stable: generate embeddings for multiple texts. Read more
Source§fn supports_model(&self, model: EmbeddingModel) -> bool
fn supports_model(&self, model: EmbeddingModel) -> bool
Stable: check if the service supports a given model.
Source§fn embed_one<'life0, 'life1, 'async_trait>(
&'life0 self,
text: &'life1 str,
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<f32>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn embed_one<'life0, 'life1, 'async_trait>(
&'life0 self,
text: &'life1 str,
model: EmbeddingModel,
) -> Pin<Box<dyn Future<Output = Result<Vec<f32>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Stable: generate an embedding for a single text. Read more
Source§fn model_config(&self, model: EmbeddingModel) -> ModelConfig
fn model_config(&self, model: EmbeddingModel) -> ModelConfig
Unstable: returns the effective
ModelConfig for a given model on this service. Read moreAuto Trait Implementations§
impl<S> Freeze for CachedEmbeddingService<S>
impl<S> !RefUnwindSafe for CachedEmbeddingService<S>
impl<S> Send for CachedEmbeddingService<S>
impl<S> Sync for CachedEmbeddingService<S>
impl<S> Unpin for CachedEmbeddingService<S>
impl<S> UnsafeUnpin for CachedEmbeddingService<S>
impl<S> UnwindSafe for CachedEmbeddingService<S>where
S: RefUnwindSafe,
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