Skip to main content

SemanticMemory

Struct SemanticMemory 

Source
pub struct SemanticMemory<E, V>
where E: Embedder, V: VectorStore,
{ /* private fields */ }
Expand description

Embedder + VectorStore + Namespace bundle.

The embedder produces vectors at add and search time; the vector store holds them. Both must agree on dimension() — checked at construction.

Implementations§

Source§

impl<E, V> SemanticMemory<E, V>
where E: Embedder, V: VectorStore,

Source

pub fn new( embedder: Arc<E>, vector_store: Arc<V>, namespace: Namespace, ) -> Result<Self>

Construct from owned components, validating dimension parity.

Returns Error::Config if the embedder and vector store report different dimensions.

Source

pub const fn namespace(&self) -> &Namespace

Borrow the bound namespace.

Source

pub async fn add( &self, ctx: &ExecutionContext, document: Document, ) -> Result<()>

Embed document.content and add it to the vector store. The embedder’s usage metadata (when surfaced) is dropped here — callers that need to charge cost meters per-embed should use the embedder directly and then call VectorStore::add.

Source

pub async fn add_batch( &self, ctx: &ExecutionContext, documents: Vec<Document>, ) -> Result<()>

Add many documents at once — uses Embedder::embed_batch to amortise embedder calls then VectorStore::add_batch to amortise index writes.

Returns Error::Config if the embedder produces a vector count that doesn’t match the input documents — silent truncation via zip would drop documents without surfacing the embedder bug.

Source

pub async fn delete(&self, ctx: &ExecutionContext, doc_id: &str) -> Result<()>

Delete a previously-indexed document by id.

Source

pub async fn update( &self, ctx: &ExecutionContext, doc_id: &str, document: Document, ) -> Result<()>

Update a previously-indexed document. Re-embeds the document’s content via the embedder and asks the vector store to swap vector + metadata under the same id.

Source

pub async fn search( &self, ctx: &ExecutionContext, query: &str, top_k: usize, ) -> Result<Vec<Document>>

Embed query and search the vector store for the top top_k matches.

Source

pub async fn search_filtered( &self, ctx: &ExecutionContext, query: &str, top_k: usize, filter: &VectorFilter, ) -> Result<Vec<Document>>

Embed query and search with a metadata filter. Backends without filter support return Error::Config per the VectorStore::search_filtered contract.

Source

pub async fn search_with_rerank<R: Reranker>( &self, ctx: &ExecutionContext, query: &str, top_k: usize, candidates: usize, reranker: &R, ) -> Result<Vec<RerankedDocument>>

Two-stage retrieval: over-fetch candidates from the vector store, then rerank down to top_k via the supplied Reranker. The over-fetch factor is the operator’s lever for trading recall against rerank latency — passing candidates == top_k makes the reranker no-op-shaped, while candidates >> top_k exposes more candidates to the reranker’s scoring. Returns RerankedDocuments so callers retain both the retrieval and rerank scores for explainability.

Source

pub async fn count( &self, ctx: &ExecutionContext, filter: Option<&VectorFilter>, ) -> Result<usize>

Count documents in the bound namespace. Pass-through to VectorStore::count — backends without count support surface Error::Config.

Source

pub async fn list( &self, ctx: &ExecutionContext, filter: Option<&VectorFilter>, limit: usize, offset: usize, ) -> Result<Vec<Document>>

Enumerate documents in the bound namespace. Pass-through to VectorStore::list — backends without enumeration support surface Error::Config.

Trait Implementations§

Source§

impl<E, V> SemanticMemoryBackend for SemanticMemory<E, V>
where E: Embedder, V: VectorStore,

Source§

fn namespace(&self) -> &Namespace

Borrow the bound Namespace. Tools and orchestration code that route queries by tenant or scope read this to validate the backend is wired to the expected slice without downcasting to the concrete generic type.
Source§

fn dimension(&self) -> usize

Vector dimension the backend embeds and indexes at. Lets schedulers verify a query embedder matches before issuing a search, and lets dashboards report index width per tenant.
Source§

fn search<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, query: &'life2 str, top_k: usize, ) -> Pin<Box<dyn Future<Output = Result<Vec<Document>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Embed query and return the top top_k matches.
Source§

fn search_filtered<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, query: &'life2 str, top_k: usize, filter: &'life3 VectorFilter, ) -> Pin<Box<dyn Future<Output = Result<Vec<Document>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Embed query, fetch candidates, push down filter if the backend supports it; otherwise the underlying VectorStore returns Error::Config.
Source§

fn add<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, document: Document, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Embed document.content and add the document to the index.
Source§

fn add_batch<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, documents: Vec<Document>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Add many documents at once. Default implementations defer to the embedder’s batch path then to the vector store’s batch path so backends that support either can amortise round-trips.
Source§

fn delete<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, doc_id: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Delete a previously-indexed document by its backend id.
Source§

fn update<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, doc_id: &'life2 str, document: Document, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Replace an existing document’s vector and metadata atomically when the backend supports it; otherwise non-atomic via delete + add.
Source§

fn search_with_rerank_dyn<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, query: &'life2 str, top_k: usize, candidates: usize, reranker: &'life3 dyn Reranker, ) -> Pin<Box<dyn Future<Output = Result<Vec<RerankedDocument>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Two-stage retrieval: over-fetch candidates then rerank down to top_k. The reranker is supplied as a trait object so the backend trait stays object-safe (the concrete SemanticMemory::search_with_rerank also accepts monomorphic R: Reranker for users who prefer static dispatch). Returns RerankedDocuments so callers can inspect the reranker’s score alongside the retrieval score.
Source§

fn count<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, filter: Option<&'life2 VectorFilter>, ) -> Pin<Box<dyn Future<Output = Result<usize>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Count documents in the bound namespace, optionally narrowed by a metadata filter. Pass-through to VectorStore::count — backends without count support surface Error::Config.
Source§

fn list<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, ctx: &'life1 ExecutionContext, filter: Option<&'life2 VectorFilter>, limit: usize, offset: usize, ) -> Pin<Box<dyn Future<Output = Result<Vec<Document>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Enumerate documents in the bound namespace. Pass-through to VectorStore::list — backends without enumeration support surface Error::Config.

Auto Trait Implementations§

§

impl<E, V> Freeze for SemanticMemory<E, V>

§

impl<E, V> RefUnwindSafe for SemanticMemory<E, V>

§

impl<E, V> Send for SemanticMemory<E, V>

§

impl<E, V> Sync for SemanticMemory<E, V>

§

impl<E, V> Unpin for SemanticMemory<E, V>

§

impl<E, V> UnsafeUnpin for SemanticMemory<E, V>

§

impl<E, V> UnwindSafe for SemanticMemory<E, V>

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> 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> 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, 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<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