pub struct SemanticRouter { /* private fields */ }Expand description
Semantic router combining CID-based and vector-based search
Provides intelligent content discovery through vector similarity search over content embeddings.
Implementations§
Source§impl SemanticRouter
impl SemanticRouter
Sourcepub fn new(config: RouterConfig) -> Result<Self>
pub fn new(config: RouterConfig) -> Result<Self>
Create a new semantic router with the given configuration
Sourcepub fn with_defaults() -> Result<Self>
pub fn with_defaults() -> Result<Self>
Create a new router with default configuration
Sourcepub fn add(&self, cid: &Cid, embedding: &[f32]) -> Result<()>
pub fn add(&self, cid: &Cid, embedding: &[f32]) -> Result<()>
Add content with its embedding to the router
§Arguments
cid- Content identifierembedding- Vector embedding of the content
Sourcepub fn add_batch(&self, items: &[(Cid, Vec<f32>)]) -> Result<()>
pub fn add_batch(&self, items: &[(Cid, Vec<f32>)]) -> Result<()>
Add multiple content items in batch
More efficient than adding one by one
§Arguments
items- Vector of (CID, embedding) pairs
Sourcepub async fn query(
&self,
query_embedding: &[f32],
k: usize,
) -> Result<Vec<SearchResult>>
pub async fn query( &self, query_embedding: &[f32], k: usize, ) -> Result<Vec<SearchResult>>
Query for content by semantic similarity
§Arguments
query_embedding- Query vectork- Number of results to return
Sourcepub async fn query_auto(
&self,
query_embedding: &[f32],
k: usize,
) -> Result<Vec<SearchResult>>
pub async fn query_auto( &self, query_embedding: &[f32], k: usize, ) -> Result<Vec<SearchResult>>
Query with auto-tuned ef_search parameter
Automatically determines the optimal ef_search based on k and index size
§Arguments
query_embedding- Query vectork- Number of results to return
Sourcepub async fn query_with_ef(
&self,
query_embedding: &[f32],
k: usize,
ef_search: usize,
) -> Result<Vec<SearchResult>>
pub async fn query_with_ef( &self, query_embedding: &[f32], k: usize, ef_search: usize, ) -> Result<Vec<SearchResult>>
Query with custom ef_search parameter
§Arguments
query_embedding- Query vectork- Number of results to returnef_search- Search parameter (higher = more accurate but slower)
Sourcepub async fn query_with_filter(
&self,
query_embedding: &[f32],
k: usize,
filter: QueryFilter,
) -> Result<Vec<SearchResult>>
pub async fn query_with_filter( &self, query_embedding: &[f32], k: usize, filter: QueryFilter, ) -> Result<Vec<SearchResult>>
Query with filtering options
§Arguments
query_embedding- Query vectork- Number of results to returnfilter- Query filter options
Sourcepub fn clear_cache(&self)
pub fn clear_cache(&self)
Clear the query result cache
Sourcepub fn cache_stats(&self) -> CacheStats
pub fn cache_stats(&self) -> CacheStats
Get cache statistics
Sourcepub fn stats(&self) -> RouterStats
pub fn stats(&self) -> RouterStats
Get statistics about the router
Sourcepub fn optimization_recommendations(&self) -> OptimizationRecommendations
pub fn optimization_recommendations(&self) -> OptimizationRecommendations
Get optimization recommendations
Returns recommended HNSW parameters for the current index size
Sourcepub async fn save_index<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub async fn save_index<P: AsRef<Path>>(&self, path: P) -> Result<()>
Save the semantic index to a file
Serializes the entire HNSW index including all vectors and CID mappings to a file for later loading.
§Arguments
path- Path to save the index file
Sourcepub async fn load_index<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub async fn load_index<P: AsRef<Path>>(&self, path: P) -> Result<()>
Load a semantic index from a file
Loads a previously saved HNSW index from disk, replacing the current index.
§Arguments
path- Path to the saved index file
Sourcepub async fn query_with_aggregations(
&self,
query_embedding: &[f32],
k: usize,
filter: QueryFilter,
) -> Result<(Vec<SearchResult>, SearchAggregations)>
pub async fn query_with_aggregations( &self, query_embedding: &[f32], k: usize, filter: QueryFilter, ) -> Result<(Vec<SearchResult>, SearchAggregations)>
Query with aggregations
Returns both results and aggregated statistics
§Arguments
query_embedding- Query vectork- Number of results to returnfilter- Query filter options
Sourcepub async fn query_batch(
&self,
query_embeddings: &[Vec<f32>],
k: usize,
) -> Result<Vec<Vec<SearchResult>>>
pub async fn query_batch( &self, query_embeddings: &[Vec<f32>], k: usize, ) -> Result<Vec<Vec<SearchResult>>>
Sourcepub async fn query_batch_with_filter(
&self,
query_embeddings: &[Vec<f32>],
k: usize,
filter: QueryFilter,
) -> Result<Vec<Vec<SearchResult>>>
pub async fn query_batch_with_filter( &self, query_embeddings: &[Vec<f32>], k: usize, filter: QueryFilter, ) -> Result<Vec<Vec<SearchResult>>>
Batch query with filtering options
Processes multiple queries in parallel with filtering applied to each.
§Arguments
query_embeddings- Multiple query vectorsk- Number of results to return per queryfilter- Query filter options (applied to all queries)
§Returns
Vector of search results, one for each query in the same order
Sourcepub async fn query_batch_with_ef(
&self,
query_embeddings: &[Vec<f32>],
k: usize,
ef_search: usize,
) -> Result<Vec<Vec<SearchResult>>>
pub async fn query_batch_with_ef( &self, query_embeddings: &[Vec<f32>], k: usize, ef_search: usize, ) -> Result<Vec<Vec<SearchResult>>>
Batch query with custom ef_search parameter
Processes multiple queries in parallel with custom search parameter.
§Arguments
query_embeddings- Multiple query vectorsk- Number of results to return per queryef_search- Search parameter (higher = more accurate but slower)
§Returns
Vector of search results, one for each query in the same order
Sourcepub fn batch_stats(&self, batch_results: &[Vec<SearchResult>]) -> BatchStats
pub fn batch_stats(&self, batch_results: &[Vec<SearchResult>]) -> BatchStats
Get batch query statistics
Returns aggregated statistics across a batch of queries
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SemanticRouter
impl RefUnwindSafe for SemanticRouter
impl Send for SemanticRouter
impl Sync for SemanticRouter
impl Unpin for SemanticRouter
impl UnwindSafe for SemanticRouter
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.