pub struct VectorSpaceService { /* private fields */ }Expand description
The main service for vector space operations.
This service provides a thread-safe interface for:
- Adding and removing embeddings
- Nearest neighbor search
- Building similarity graphs
Implementations§
Source§impl VectorSpaceService
impl VectorSpaceService
Sourcepub fn new(config: HnswConfig) -> Self
pub fn new(config: HnswConfig) -> Self
Create a new vector space service with the given configuration.
Sourcepub fn from_index(index: HnswIndex, config: HnswConfig) -> Self
pub fn from_index(index: HnswIndex, config: HnswConfig) -> Self
Create a service from an existing index.
Sourcepub fn dimensions(&self) -> usize
pub fn dimensions(&self) -> usize
Get the index dimensions.
Sourcepub fn config(&self) -> &HnswConfig
pub fn config(&self) -> &HnswConfig
Get a reference to the configuration.
Sourcepub async fn add_embedding(
&self,
id: EmbeddingId,
vector: Vec<f32>,
) -> Result<(), VectorError>
pub async fn add_embedding( &self, id: EmbeddingId, vector: Vec<f32>, ) -> Result<(), VectorError>
Add a single embedding to the index.
The vector will be normalized if the configuration specifies normalization.
Sourcepub async fn add_embeddings_batch(
&self,
items: Vec<(EmbeddingId, Vec<f32>)>,
) -> Result<usize, VectorError>
pub async fn add_embeddings_batch( &self, items: Vec<(EmbeddingId, Vec<f32>)>, ) -> Result<usize, VectorError>
Add multiple embeddings in a batch.
This is more efficient than multiple single adds due to amortized locking overhead.
Sourcepub async fn find_neighbors(
&self,
query: &[f32],
k: usize,
) -> Result<Vec<Neighbor>, VectorError>
pub async fn find_neighbors( &self, query: &[f32], k: usize, ) -> Result<Vec<Neighbor>, VectorError>
Find the k nearest neighbors to a query vector.
Sourcepub async fn find_neighbors_with_options(
&self,
query: &[f32],
options: SearchOptions,
) -> Result<Vec<Neighbor>, VectorError>
pub async fn find_neighbors_with_options( &self, query: &[f32], options: SearchOptions, ) -> Result<Vec<Neighbor>, VectorError>
Find neighbors with custom search options.
Sourcepub async fn find_neighbors_with_filter<F>(
&self,
query: &[f32],
k: usize,
filter: F,
) -> Result<Vec<Neighbor>, VectorError>
pub async fn find_neighbors_with_filter<F>( &self, query: &[f32], k: usize, filter: F, ) -> Result<Vec<Neighbor>, VectorError>
Find neighbors using a filter predicate.
The filter function receives an EmbeddingId and returns true if the embedding should be included in results.
Sourcepub async fn remove_embedding(
&self,
id: &EmbeddingId,
) -> Result<(), VectorError>
pub async fn remove_embedding( &self, id: &EmbeddingId, ) -> Result<(), VectorError>
Remove an embedding from the index.
Sourcepub fn contains(&self, id: &EmbeddingId) -> bool
pub fn contains(&self, id: &EmbeddingId) -> bool
Check if an embedding exists in the index.
Sourcepub fn get_vector(&self, id: &EmbeddingId) -> Option<Vec<f32>>
pub fn get_vector(&self, id: &EmbeddingId) -> Option<Vec<f32>>
Get a vector by its ID.
Sourcepub async fn build_similarity_edges(
&self,
id: EmbeddingId,
vector: &[f32],
k: usize,
min_similarity: f32,
) -> Result<Vec<SimilarityEdge>, VectorError>
pub async fn build_similarity_edges( &self, id: EmbeddingId, vector: &[f32], k: usize, min_similarity: f32, ) -> Result<Vec<SimilarityEdge>, VectorError>
Build similarity edges for an embedding.
This finds the k nearest neighbors and creates edges to them.
Sourcepub async fn compute_pairwise_similarities(
&self,
vectors: &[(EmbeddingId, Vec<f32>)],
) -> Result<Vec<(EmbeddingId, EmbeddingId, f32)>, VectorError>
pub async fn compute_pairwise_similarities( &self, vectors: &[(EmbeddingId, Vec<f32>)], ) -> Result<Vec<(EmbeddingId, EmbeddingId, f32)>, VectorError>
Compute pairwise similarities for a set of embeddings.
Sourcepub async fn clear(&self) -> Result<(), VectorError>
pub async fn clear(&self) -> Result<(), VectorError>
Clear all embeddings from the index.
Sourcepub async fn load(path: &Path, config: HnswConfig) -> Result<Self, VectorError>
pub async fn load(path: &Path, config: HnswConfig) -> Result<Self, VectorError>
Load an index from a file.
Sourcepub fn stats(&self) -> IndexStatistics
pub fn stats(&self) -> IndexStatistics
Get index statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for VectorSpaceService
impl !RefUnwindSafe for VectorSpaceService
impl Send for VectorSpaceService
impl Sync for VectorSpaceService
impl Unpin for VectorSpaceService
impl !UnwindSafe for VectorSpaceService
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