Skip to main content

EmbeddingVector

Struct EmbeddingVector 

Source
pub struct EmbeddingVector<State> { /* private fields */ }
Expand description

An embedding vector tagged with a normalization-state marker.

The type parameter encodes whether the vector is L2-normalized:

  • EmbeddingVector<Unnormalized> — raw model output; must be normalized before passing to cosine-distance Qdrant searches.
  • EmbeddingVector<Normalized> — unit-length vector, safe to pass directly to Qdrant gRPC cosine queries.

Using Normalized as a required parameter type at the Qdrant search boundary turns dimension/normalization mismatches into compile-time errors rather than silent near-zero similarity scores (see bugs #3421, #3382, #3420, #3422).

§Construction

use zeph_common::math::{EmbeddingVector, Normalized, Unnormalized};

// Wrap a raw model vector and normalize it.
let raw = EmbeddingVector::<Unnormalized>::new(vec![3.0_f32, 4.0]);
let normalized = raw.normalize();
let slice = normalized.as_slice();
// A normalized vector has unit L2 length.
let norm: f32 = slice.iter().map(|x| x * x).sum::<f32>().sqrt();
assert!((norm - 1.0).abs() < 1e-6);

// Trust-caller constructor for models that always return unit vectors.
let trusted = EmbeddingVector::<Normalized>::new_normalized(vec![0.6_f32, 0.8]);
assert_eq!(trusted.as_slice(), &[0.6_f32, 0.8]);

Implementations§

Source§

impl EmbeddingVector<Unnormalized>

Source

pub fn new(inner: Vec<f32>) -> Self

Wrap a raw embedding vector from a model or storage.

The returned vector is tagged Unnormalized. Call normalize before passing it to functions that require Normalized.

§Examples
use zeph_common::math::{EmbeddingVector, Unnormalized};

let v = EmbeddingVector::<Unnormalized>::new(vec![1.0_f32, 0.0]);
assert_eq!(v.as_slice(), &[1.0_f32, 0.0]);
Source

pub fn normalize(self) -> EmbeddingVector<Normalized>

L2-normalize this vector and return an EmbeddingVector<Normalized>.

If the vector is a zero vector (L2 norm is zero), all elements are set to zero to avoid division by zero; the result is technically invalid for cosine search but is safe and consistent with the behavior of cosine_similarity.

§Examples
use zeph_common::math::{EmbeddingVector, Unnormalized};

let raw = EmbeddingVector::<Unnormalized>::new(vec![3.0_f32, 4.0]);
let norm = raw.normalize();
let sum_sq: f32 = norm.as_slice().iter().map(|x| x * x).sum();
assert!((sum_sq - 1.0).abs() < 1e-6, "must be unit length");
Source§

impl EmbeddingVector<Normalized>

Source

pub fn new_normalized(inner: Vec<f32>) -> Self

Construct a normalized embedding vector, trusting the caller that inner is already L2-unit-length.

Use this constructor only when the source guarantees unit-length output (e.g., a model that always normalizes, or a vector loaded from a store known to hold normalized data). Incorrect use does not cause UB but will produce wrong cosine scores.

§Examples
use zeph_common::math::{EmbeddingVector, Normalized};

// Suppose the model always returns unit vectors.
let v = EmbeddingVector::<Normalized>::new_normalized(vec![0.6_f32, 0.8]);
assert_eq!(v.as_slice(), &[0.6_f32, 0.8]);
Source§

impl<State> EmbeddingVector<State>

Source

pub fn as_slice(&self) -> &[f32]

Return a borrowed slice of the vector elements.

§Examples
use zeph_common::math::{EmbeddingVector, Unnormalized};

let v = EmbeddingVector::<Unnormalized>::new(vec![1.0_f32, 2.0]);
assert_eq!(v.as_slice(), &[1.0_f32, 2.0]);
Source

pub fn into_inner(self) -> Vec<f32>

Consume the wrapper and return the underlying Vec<f32>.

§Examples
use zeph_common::math::{EmbeddingVector, Unnormalized};

let v = EmbeddingVector::<Unnormalized>::new(vec![1.0_f32, 2.0]);
assert_eq!(v.into_inner(), vec![1.0_f32, 2.0]);
Source

pub fn len(&self) -> usize

Return the number of dimensions in this vector.

§Examples
use zeph_common::math::{EmbeddingVector, Unnormalized};

let v = EmbeddingVector::<Unnormalized>::new(vec![1.0_f32, 2.0, 3.0]);
assert_eq!(v.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the vector has no elements.

§Examples
use zeph_common::math::{EmbeddingVector, Unnormalized};

let v = EmbeddingVector::<Unnormalized>::new(vec![]);
assert!(v.is_empty());

Trait Implementations§

Source§

impl<State: Clone> Clone for EmbeddingVector<State>

Source§

fn clone(&self) -> EmbeddingVector<State>

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<State: Debug> Debug for EmbeddingVector<State>

Source§

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

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

impl From<Vec<f32>> for EmbeddingVector<Unnormalized>

Source§

fn from(v: Vec<f32>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<State> Freeze for EmbeddingVector<State>

§

impl<State> RefUnwindSafe for EmbeddingVector<State>
where State: RefUnwindSafe,

§

impl<State> Send for EmbeddingVector<State>
where State: Send,

§

impl<State> Sync for EmbeddingVector<State>
where State: Sync,

§

impl<State> Unpin for EmbeddingVector<State>
where State: Unpin,

§

impl<State> UnsafeUnpin for EmbeddingVector<State>

§

impl<State> UnwindSafe for EmbeddingVector<State>
where State: UnwindSafe,

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