Skip to main content

EmbeddingSpace

Trait EmbeddingSpace 

Source
pub trait EmbeddingSpace:
    Clone
    + PartialEq
    + Eq
    + Debug
    + Send
    + Sync {
    type EmbeddingData: Embedding;
    type DistanceValue: Distance;
    type Prepared: Clone;

    // Required methods
    fn space_id(&self) -> &'static str;
    fn distance(
        &self,
        lhs: &Self::EmbeddingData,
        rhs: &Self::EmbeddingData,
    ) -> Self::DistanceValue;
    fn prepare(&self, embedding: &Self::EmbeddingData) -> Self::Prepared;
    fn distance_prepared(
        &self,
        prepared: &Self::Prepared,
        target: &Self::EmbeddingData,
    ) -> Self::DistanceValue;
    fn length() -> usize;
    fn infinite_mapping(native_distance: &Self::DistanceValue) -> f32;
    fn slice_distance(a: &[f32], b: &[f32]) -> f32;

    // Provided methods
    fn create_embedding(
        data: Vec<<Self::EmbeddingData as Embedding>::Scalar>,
    ) -> Self::EmbeddingData { ... }
    fn create_distance(dist: f32) -> Self::DistanceValue { ... }
    fn zero_vector() -> Self::EmbeddingData { ... }
    fn zero_distance() -> Self::DistanceValue { ... }
}
Expand description

An embedding space defines the embedding type, distance metric, and space properties.

Required Associated Types§

Source

type EmbeddingData: Embedding

Source

type DistanceValue: Distance

Source

type Prepared: Clone

Precomputed state for efficient distance queries.

Required Methods§

Source

fn space_id(&self) -> &'static str

Source

fn distance( &self, lhs: &Self::EmbeddingData, rhs: &Self::EmbeddingData, ) -> Self::DistanceValue

Compute distance between two embeddings.

Source

fn prepare(&self, embedding: &Self::EmbeddingData) -> Self::Prepared

Prepare a query embedding for efficient repeated distance computations.

Source

fn distance_prepared( &self, prepared: &Self::Prepared, target: &Self::EmbeddingData, ) -> Self::DistanceValue

Compute distance using prepared query state.

Source

fn length() -> usize

Source

fn infinite_mapping(native_distance: &Self::DistanceValue) -> f32

Maps a finite distance range to an infinite range (e.g., tan(pi * x / 4))

Source

fn slice_distance(a: &[f32], b: &[f32]) -> f32

Compute the space’s distance metric on raw scalar slices of arbitrary length.

This is the same distance function as distance() but operates on raw f32 slices rather than typed EmbeddingData. Used when the caller has subvectors or centroid slices that don’t match the full embedding dimension.

Provided Methods§

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§