Skip to main content

SemanticCache

Struct SemanticCache 

Source
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: CacheConfig

Cache configuration.

§distance_threshold: f32

Distance threshold used for semantic hits.

§vector_dimensions: usize

Prompt embedding dimensions stored in Redis.

§dtype: VectorDataType

Vector element data type used for the index schema.

§index: SearchIndex

Underlying search index.

Implementations§

Source§

impl SemanticCache

Source

pub fn new( config: CacheConfig, distance_threshold: f32, vector_dimensions: usize, ) -> Result<Self>

Creates a new semantic cache with the default reserved schema.

Source

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.

Source

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.

Source

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.

Source

pub fn with_vectorizer<V>(self, vectorizer: V) -> Self
where V: Vectorizer + 'static,

Attaches a synchronous vectorizer used when callers pass prompts instead of vectors.

Source

pub fn with_default_vectorizer(self) -> Result<Self>

Attaches the default HuggingFace local vectorizer.

This uses the AllMiniLML6V2 model from fastembed which runs locally via ONNX Runtime and requires no API key. The model is downloaded from HuggingFace Hub on first use.

§Errors

Returns an error if the model cannot be loaded.

Source

pub fn set_vectorizer<V>(&mut self, vectorizer: V)
where V: Vectorizer + 'static,

Replaces the vectorizer used for prompt embedding.

Source

pub fn ttl(&self) -> Option<u64>

Returns the configured default TTL for semantic cache entries.

Source

pub fn set_ttl(&mut self, ttl_seconds: Option<u64>)

Sets or clears the default TTL for semantic cache entries.

Source

pub fn set_threshold(&mut self, distance_threshold: f32) -> Result<()>

Updates the semantic distance threshold.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn update(&self, key: &str, fields: Map<String, Value>) -> Result<()>

Updates cached fields for a stored semantic cache entry and refreshes TTL.

Source

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.

Source

pub fn clear(&self) -> Result<usize>

Clears all semantic cache entries while preserving the index.

Source

pub async fn aclear(&self) -> Result<usize>

Clears all semantic cache entries asynchronously while preserving the index.

Source

pub fn delete(&self) -> Result<()>

Deletes the semantic cache index and its documents.

Source

pub async fn adelete(&self) -> Result<()>

Deletes the semantic cache index asynchronously and its documents.

Source

pub fn drop_ids(&self, ids: &[String]) -> Result<()>

Drops stored entries by their entry ids.

Source

pub fn drop_keys(&self, keys: &[String]) -> Result<()>

Drops stored entries by their Redis keys.

Source

pub async fn adrop_ids(&self, ids: &[String]) -> Result<()>

Drops stored entries asynchronously by their entry ids.

Source

pub async fn adrop_keys(&self, keys: &[String]) -> Result<()>

Drops stored entries asynchronously by their Redis keys.

Trait Implementations§

Source§

impl Clone for SemanticCache

Source§

fn clone(&self) -> SemanticCache

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SemanticCache

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more