pub struct Embedding { /* private fields */ }Expand description
A dense vector embedding for use with pgvector.
This wraps pgvector::Vector and provides additional methods for
ORM integration, validation, and conversion.
§Examples
use prax_pgvector::Embedding;
// From a Vec<f32>
let embedding = Embedding::new(vec![0.1, 0.2, 0.3]);
// From a slice
let embedding = Embedding::from_slice(&[0.1, 0.2, 0.3]);
// Access dimensions
assert_eq!(embedding.len(), 3);
assert_eq!(embedding.as_slice()[0], 0.1);Implementations§
Source§impl Embedding
impl Embedding
Sourcepub fn from_slice(slice: &[f32]) -> Self
pub fn from_slice(slice: &[f32]) -> Self
Create an embedding from a float slice.
Sourcepub fn zeros(dimensions: usize) -> Self
pub fn zeros(dimensions: usize) -> Self
Create a zero vector with the given number of dimensions.
Sourcepub fn try_new(dimensions: Vec<f32>) -> VectorResult<Self>
pub fn try_new(dimensions: Vec<f32>) -> VectorResult<Self>
Create a validated embedding, ensuring it’s non-empty.
§Errors
Returns VectorError::EmptyVector if the input is empty.
Sourcepub fn validate_dimensions(&self, expected: usize) -> VectorResult<()>
pub fn validate_dimensions(&self, expected: usize) -> VectorResult<()>
Validate that this embedding has the expected number of dimensions.
§Errors
Returns VectorError::DimensionMismatch if the dimensions don’t match.
Sourcepub fn into_inner(self) -> Vector
pub fn into_inner(self) -> Vector
Get the inner pgvector type.
Sourcepub fn normalize(&self) -> Option<Self>
pub fn normalize(&self) -> Option<Self>
Normalize this vector to unit length (L2 normalization).
Returns None if the vector is a zero vector.
Sourcepub fn dot_product(&self, other: &Self) -> VectorResult<f32>
pub fn dot_product(&self, other: &Self) -> VectorResult<f32>
Compute the dot product with another embedding.
§Errors
Returns VectorError::DimensionMismatch if the dimensions differ.
Sourcepub fn cosine_similarity(&self, other: &Self) -> VectorResult<f32>
pub fn cosine_similarity(&self, other: &Self) -> VectorResult<f32>
Compute the cosine similarity with another embedding.
Returns a value between -1.0 and 1.0.
§Errors
Returns VectorError::DimensionMismatch if the dimensions differ.
Sourcepub fn l2_distance(&self, other: &Self) -> VectorResult<f32>
pub fn l2_distance(&self, other: &Self) -> VectorResult<f32>
Compute the Euclidean (L2) distance to another embedding.
§Errors
Returns VectorError::DimensionMismatch if the dimensions differ.
Sourcepub fn to_sql_literal(&self) -> String
pub fn to_sql_literal(&self) -> String
Generate the PostgreSQL literal representation.
This produces a string like '[0.1,0.2,0.3]'::vector.
Sourcepub fn to_sql_literal_typed(&self) -> String
pub fn to_sql_literal_typed(&self) -> String
Generate the PostgreSQL literal with explicit dimension.
This produces a string like '[0.1,0.2,0.3]'::vector(3).