pub struct SemanticCache {
pub config: CacheConfig,
pub distance_threshold: f32,
pub vector_dimensions: usize,
pub dtype: VectorDataType,
pub index: SearchIndex,
/* private fields */
}Expand description
Semantic cache backed by a Redis Search vector index.
Fields§
§config: CacheConfigCache configuration.
distance_threshold: f32Distance threshold used for semantic hits.
vector_dimensions: usizePrompt embedding dimensions stored in Redis.
dtype: VectorDataTypeVector element data type used for the index schema.
index: SearchIndexUnderlying search index.
Implementations§
Source§impl SemanticCache
impl SemanticCache
Sourcepub fn new(
config: CacheConfig,
distance_threshold: f32,
vector_dimensions: usize,
) -> Result<Self>
pub fn new( config: CacheConfig, distance_threshold: f32, vector_dimensions: usize, ) -> Result<Self>
Creates a new semantic cache with the default reserved schema.
Sourcepub fn with_dtype(
config: CacheConfig,
distance_threshold: f32,
vector_dimensions: usize,
dtype: VectorDataType,
) -> Result<Self>
pub fn with_dtype( config: CacheConfig, distance_threshold: f32, vector_dimensions: usize, dtype: VectorDataType, ) -> Result<Self>
Creates a new semantic cache with a specific vector data type.
Sourcepub fn with_filterable_fields(
config: CacheConfig,
distance_threshold: f32,
vector_dimensions: usize,
filterable_fields: &[Value],
) -> Result<Self>
pub fn with_filterable_fields( config: CacheConfig, distance_threshold: f32, vector_dimensions: usize, filterable_fields: &[Value], ) -> Result<Self>
Creates a new semantic cache with additional filterable schema fields.
Sourcepub fn with_options(
config: CacheConfig,
distance_threshold: f32,
vector_dimensions: usize,
dtype: VectorDataType,
filterable_fields: &[Value],
) -> Result<Self>
pub fn with_options( config: CacheConfig, distance_threshold: f32, vector_dimensions: usize, dtype: VectorDataType, filterable_fields: &[Value], ) -> Result<Self>
Creates a new semantic cache with full control over dtype and filterable fields.
Sourcepub fn with_vectorizer<V>(self, vectorizer: V) -> Selfwhere
V: Vectorizer + 'static,
pub fn with_vectorizer<V>(self, vectorizer: V) -> Selfwhere
V: Vectorizer + 'static,
Attaches a synchronous vectorizer used when callers pass prompts instead of vectors.
Sourcepub fn with_default_vectorizer(self) -> Result<Self>
pub fn with_default_vectorizer(self) -> Result<Self>
Sourcepub fn set_vectorizer<V>(&mut self, vectorizer: V)where
V: Vectorizer + 'static,
pub fn set_vectorizer<V>(&mut self, vectorizer: V)where
V: Vectorizer + 'static,
Replaces the vectorizer used for prompt embedding.
Sourcepub fn ttl(&self) -> Option<u64>
pub fn ttl(&self) -> Option<u64>
Returns the configured default TTL for semantic cache entries.
Sourcepub fn set_ttl(&mut self, ttl_seconds: Option<u64>)
pub fn set_ttl(&mut self, ttl_seconds: Option<u64>)
Sets or clears the default TTL for semantic cache entries.
Sourcepub fn set_threshold(&mut self, distance_threshold: f32) -> Result<()>
pub fn set_threshold(&mut self, distance_threshold: f32) -> Result<()>
Updates the semantic distance threshold.
Sourcepub fn store(
&self,
prompt: &str,
response: &str,
vector: Option<&[f32]>,
metadata: Option<Value>,
filters: Option<Map<String, Value>>,
ttl_seconds: Option<u64>,
) -> Result<String>
pub fn store( &self, prompt: &str, response: &str, vector: Option<&[f32]>, metadata: Option<Value>, filters: Option<Map<String, Value>>, ttl_seconds: Option<u64>, ) -> Result<String>
Stores a semantic cache entry and returns its Redis key.
Sourcepub async fn astore(
&self,
prompt: &str,
response: &str,
vector: Option<&[f32]>,
metadata: Option<Value>,
filters: Option<Map<String, Value>>,
ttl_seconds: Option<u64>,
) -> Result<String>
pub async fn astore( &self, prompt: &str, response: &str, vector: Option<&[f32]>, metadata: Option<Value>, filters: Option<Map<String, Value>>, ttl_seconds: Option<u64>, ) -> Result<String>
Stores a semantic cache entry asynchronously and returns its Redis key.
Sourcepub fn check(
&self,
prompt: Option<&str>,
vector: Option<&[f32]>,
num_results: usize,
return_fields: Option<&[&str]>,
filter_expression: Option<FilterExpression>,
distance_threshold: Option<f32>,
) -> Result<Vec<Map<String, Value>>>
pub fn check( &self, prompt: Option<&str>, vector: Option<&[f32]>, num_results: usize, return_fields: Option<&[&str]>, filter_expression: Option<FilterExpression>, distance_threshold: Option<f32>, ) -> Result<Vec<Map<String, Value>>>
Checks the semantic cache for similar prompts or a supplied vector.
Sourcepub async fn acheck(
&self,
prompt: Option<&str>,
vector: Option<&[f32]>,
num_results: usize,
return_fields: Option<&[&str]>,
filter_expression: Option<FilterExpression>,
distance_threshold: Option<f32>,
) -> Result<Vec<Map<String, Value>>>
pub async fn acheck( &self, prompt: Option<&str>, vector: Option<&[f32]>, num_results: usize, return_fields: Option<&[&str]>, filter_expression: Option<FilterExpression>, distance_threshold: Option<f32>, ) -> Result<Vec<Map<String, Value>>>
Checks the semantic cache for similar prompts or a supplied vector asynchronously.
Sourcepub fn update(&self, key: &str, fields: Map<String, Value>) -> Result<()>
pub fn update(&self, key: &str, fields: Map<String, Value>) -> Result<()>
Updates cached fields for a stored semantic cache entry and refreshes TTL.
Sourcepub async fn aupdate(&self, key: &str, fields: Map<String, Value>) -> Result<()>
pub async fn aupdate(&self, key: &str, fields: Map<String, Value>) -> Result<()>
Updates cached fields asynchronously for a stored semantic cache entry and refreshes TTL.
Sourcepub fn clear(&self) -> Result<usize>
pub fn clear(&self) -> Result<usize>
Clears all semantic cache entries while preserving the index.
Sourcepub async fn aclear(&self) -> Result<usize>
pub async fn aclear(&self) -> Result<usize>
Clears all semantic cache entries asynchronously while preserving the index.
Sourcepub async fn adelete(&self) -> Result<()>
pub async fn adelete(&self) -> Result<()>
Deletes the semantic cache index asynchronously and its documents.
Sourcepub fn drop_keys(&self, keys: &[String]) -> Result<()>
pub fn drop_keys(&self, keys: &[String]) -> Result<()>
Drops stored entries by their Redis keys.
Sourcepub async fn adrop_ids(&self, ids: &[String]) -> Result<()>
pub async fn adrop_ids(&self, ids: &[String]) -> Result<()>
Drops stored entries asynchronously by their entry ids.
Sourcepub async fn adrop_keys(&self, keys: &[String]) -> Result<()>
pub async fn adrop_keys(&self, keys: &[String]) -> Result<()>
Drops stored entries asynchronously by their Redis keys.
Trait Implementations§
Source§impl Clone for SemanticCache
impl Clone for SemanticCache
Source§fn clone(&self) -> SemanticCache
fn clone(&self) -> SemanticCache
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SemanticCache
impl !RefUnwindSafe for SemanticCache
impl Send for SemanticCache
impl Sync for SemanticCache
impl Unpin for SemanticCache
impl UnsafeUnpin for SemanticCache
impl !UnwindSafe for SemanticCache
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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