Skip to main content

Embedding

Struct Embedding 

Source
pub struct Embedding {
    pub id: Uuid,
    pub chunk_id: Uuid,
    pub vector: Vec<i16>,
    pub model_hash: [u8; 32],
    pub dim: u16,
    pub l2_norm: f32,
    pub embedding_version: u32,
}
Expand description

An embedding vector derived from a chunk

Stores the vector in i16 format (quantized) for determinism and storage efficiency. The embedding ID is derived deterministically via BLAKE3-16 from chunk_id + model_hash.

Fields§

§id: Uuid

Unique identifier for this embedding (BLAKE3-16 of chunk_id || model_hash)

§chunk_id: Uuid

Parent chunk ID

§vector: Vec<i16>

The embedding vector (i16 quantized, scale = 32767)

§model_hash: [u8; 32]

Hash of the model weights used to generate this embedding

§dim: u16

Dimensionality of the vector

§l2_norm: f32

Precomputed L2 norm of the quantized vector (for similarity computation) Per CP-001: stored for efficient cosine similarity without recomputation

§embedding_version: u32

Version of the embedding generation process (default 0)

Implementations§

Source§

impl Embedding

Source

pub fn new( chunk_id: Uuid, vector_f32: &[f32], model_hash: [u8; 32], embedding_version: u32, ) -> Self

Create a new embedding from an f32 vector.

Per CP-010 §3.4-3.5:

  1. Normalize f32 vector to unit length
  2. Quantize to i16 with round_ties_even (scale = 32767)

The embedding ID is deterministic: BLAKE3-16(chunk_id || model_hash || embedding_version).

Source

pub fn from_quantized( chunk_id: Uuid, vector: Vec<i16>, model_hash: [u8; 32], embedding_version: u32, ) -> Self

Create an embedding directly from pre-quantized i16 values.

Used when loading from storage where quantization already occurred.

Source

pub fn from_quantized_with_norm( chunk_id: Uuid, vector: Vec<i16>, model_hash: [u8; 32], l2_norm: f32, embedding_version: u32, ) -> Self

Create an embedding from pre-quantized values with a precomputed L2 norm.

Used when loading from storage where the norm was already stored.

Source

pub fn to_f32(&self) -> Vec<f32>

Convert the quantized vector back to f32 (approximate).

Source

pub fn integer_dot_product(&self, other: &[i16]) -> i64

Compute integer dot product between this embedding and another i16 vector.

Per CP-003 §4.5: all similarity computations use integer math. Returns i64 to avoid overflow (1536 dims * 32767^2 fits in i64).

Source

pub fn norm_squared(&self) -> i64

Compute the squared L2 norm of the quantized vector (integer).

This avoids sqrt and floating-point entirely.

Source

pub fn norm_f32(&self) -> f32

Compute L2 norm as f32 (for display/diagnostics only, not canonical).

Source

pub fn cosine_similarity(&self, other: &Embedding) -> f32

Compute cosine similarity using integer math.

Returns f32 for convenience, but the dot product and norms are computed entirely in integer arithmetic first.

Trait Implementations§

Source§

impl Clone for Embedding

Source§

fn clone(&self) -> Embedding

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Embedding

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Embedding

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Embedding

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Embedding

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Embedding

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,