pub struct SemanticService {
pub provider: Box<dyn EmbeddingProvider>,
/* private fields */
}Expand description
Main semantic embedding service for the memory system
Coordinates embedding generation, storage, and semantic search across episodes and patterns. Integrates with the existing storage backends.
Fields§
§provider: Box<dyn EmbeddingProvider>Embedding provider for text-to-vector conversion
Implementations§
Source§impl SemanticService
impl SemanticService
Sourcepub fn new(
provider: Box<dyn EmbeddingProvider>,
storage: Box<dyn EmbeddingStorageBackend>,
config: EmbeddingConfig,
) -> Self
pub fn new( provider: Box<dyn EmbeddingProvider>, storage: Box<dyn EmbeddingStorageBackend>, config: EmbeddingConfig, ) -> Self
Create a new semantic service with the specified provider and storage
Sourcepub fn config(&self) -> &EmbeddingConfig
pub fn config(&self) -> &EmbeddingConfig
Get the embedding configuration
Sourcepub async fn with_local_provider(
storage: Box<dyn EmbeddingStorageBackend>,
config: EmbeddingConfig,
) -> Result<Self>
pub async fn with_local_provider( storage: Box<dyn EmbeddingStorageBackend>, config: EmbeddingConfig, ) -> Result<Self>
Get the embedding provider type
Sourcepub async fn default(storage: Box<dyn EmbeddingStorageBackend>) -> Result<Self>
pub async fn default(storage: Box<dyn EmbeddingStorageBackend>) -> Result<Self>
Create a semantic service with default local provider
Sourcepub async fn with_fallback(
storage: Box<dyn EmbeddingStorageBackend>,
config: EmbeddingConfig,
) -> Result<Self>
pub async fn with_fallback( storage: Box<dyn EmbeddingStorageBackend>, config: EmbeddingConfig, ) -> Result<Self>
Create a semantic service with automatic provider fallback
Tries providers in order: Local → OpenAI → Mock (with warnings)
This ensures maximum reliability by falling back to simpler options if preferred ones fail.
Sourcepub async fn embed_episode(&self, episode: &Episode) -> Result<Vec<f32>>
pub async fn embed_episode(&self, episode: &Episode) -> Result<Vec<f32>>
Generate and store embedding for an episode
Creates a semantic representation of the episode by combining:
- Task description
- Context information (domain, language, framework)
- Key execution steps
- Outcome summary
Sourcepub async fn embed_pattern(&self, pattern: &Pattern) -> Result<Vec<f32>>
pub async fn embed_pattern(&self, pattern: &Pattern) -> Result<Vec<f32>>
Generate and store embedding for a pattern
Creates a semantic representation based on:
- Pattern description
- Context where the pattern was extracted
- Pattern metadata and effectiveness metrics
Sourcepub async fn find_similar_episodes(
&self,
query: &str,
context: &TaskContext,
limit: usize,
) -> Result<Vec<SimilaritySearchResult<Episode>>>
pub async fn find_similar_episodes( &self, query: &str, context: &TaskContext, limit: usize, ) -> Result<Vec<SimilaritySearchResult<Episode>>>
Find semantically similar episodes for a query
Uses vector similarity to find episodes that are semantically related to the query, going beyond keyword matching to understand meaning.
Sourcepub async fn find_similar_patterns(
&self,
context: &TaskContext,
limit: usize,
) -> Result<Vec<SimilaritySearchResult<Pattern>>>
pub async fn find_similar_patterns( &self, context: &TaskContext, limit: usize, ) -> Result<Vec<SimilaritySearchResult<Pattern>>>
Find semantically similar patterns for a context
Identifies patterns that are semantically relevant to the given context, enabling better pattern reuse and recommendation.
Sourcepub async fn text_similarity(&self, text1: &str, text2: &str) -> Result<f32>
pub async fn text_similarity(&self, text1: &str, text2: &str) -> Result<f32>
Calculate similarity between two texts
Sourcepub async fn find_episodes_by_embedding(
&self,
embedding: Vec<f32>,
limit: usize,
threshold: f32,
) -> Result<Vec<SimilaritySearchResult<Episode>>>
pub async fn find_episodes_by_embedding( &self, embedding: Vec<f32>, limit: usize, threshold: f32, ) -> Result<Vec<SimilaritySearchResult<Episode>>>
Find episodes similar to a pre-computed embedding vector
This method allows searching with a pre-computed embedding, useful when the embedding has been generated externally or cached.
§Arguments
embedding- Pre-computed embedding vector to search withlimit- Maximum number of results to returnthreshold- Minimum similarity score (0.0-1.0)
§Returns
Vector of similar episodes with their similarity scores
Sourcepub async fn find_patterns_by_embedding(
&self,
embedding: Vec<f32>,
limit: usize,
threshold: f32,
) -> Result<Vec<SimilaritySearchResult<Pattern>>>
pub async fn find_patterns_by_embedding( &self, embedding: Vec<f32>, limit: usize, threshold: f32, ) -> Result<Vec<SimilaritySearchResult<Pattern>>>
Find patterns similar to a pre-computed embedding vector
This method allows searching with a pre-computed embedding, useful when the embedding has been generated externally or cached.
§Arguments
embedding- Pre-computed embedding vector to search withlimit- Maximum number of results to returnthreshold- Minimum similarity score (0.0-1.0)
§Returns
Vector of similar patterns with their similarity scores
Sourcepub async fn get_embeddings_batch(
&self,
episode_ids: &[Uuid],
) -> Result<Vec<Option<Vec<f32>>>>
pub async fn get_embeddings_batch( &self, episode_ids: &[Uuid], ) -> Result<Vec<Option<Vec<f32>>>>
Get embeddings for multiple episodes in batch
This method retrieves embeddings for multiple episode IDs efficiently. For backends that don’t support batch operations, it falls back to individual lookups.
Auto Trait Implementations§
impl Freeze for SemanticService
impl !RefUnwindSafe for SemanticService
impl Send for SemanticService
impl Sync for SemanticService
impl Unpin for SemanticService
impl UnsafeUnpin for SemanticService
impl !UnwindSafe for SemanticService
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> 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<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.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.