-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dense, sparse and multi dense vector builders and conversions
- Loading branch information
Showing
5 changed files
with
213 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::qdrant::*; | ||
|
||
pub struct DenseVectorBuilder { | ||
pub(crate) values: Vec<f32>, | ||
} | ||
|
||
impl DenseVectorBuilder { | ||
pub fn new(values: impl Into<Vec<f32>>) -> Self { | ||
Self { | ||
values: values.into(), | ||
} | ||
} | ||
|
||
#[allow(unused_mut)] | ||
pub fn values(mut self, values: impl Into<Vec<f32>>) -> Self { | ||
self.values = values.into(); | ||
self | ||
} | ||
|
||
/// Builds the desired type. Can often be omitted. | ||
pub fn build(self) -> DenseVector { | ||
DenseVector { data: self.values } | ||
} | ||
} | ||
|
||
impl From<Vec<f32>> for DenseVector { | ||
fn from(values: Vec<f32>) -> Self { | ||
DenseVectorBuilder::new(values).build() | ||
} | ||
} | ||
|
||
impl From<DenseVector> for Vector { | ||
fn from(dense_vector: DenseVector) -> Self { | ||
crate::qdrant::vector::Vector::from(dense_vector).into() | ||
} | ||
} | ||
|
||
impl From<DenseVectorBuilder> for Vector { | ||
fn from(dense_vector: DenseVectorBuilder) -> Self { | ||
crate::qdrant::vector::Vector::from(dense_vector.build()).into() | ||
} | ||
} | ||
|
||
impl From<DenseVector> for crate::qdrant::vector::Vector { | ||
fn from(dense_vector: DenseVector) -> Self { | ||
Self::Dense(dense_vector) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::qdrant::*; | ||
|
||
#[derive(Default)] | ||
pub struct MultiDenseVectorBuilder { | ||
pub(crate) vectors: Vec<DenseVector>, | ||
} | ||
|
||
impl MultiDenseVectorBuilder { | ||
pub fn new(vectors: impl Into<Vec<DenseVector>>) -> Self { | ||
Self { | ||
vectors: vectors.into(), | ||
} | ||
} | ||
|
||
pub fn single(vector: impl Into<DenseVector>) -> Self { | ||
Self::new(vec![vector.into()]) | ||
} | ||
|
||
#[allow(unused_mut)] | ||
pub fn vectors(mut self, vectors: impl Into<Vec<DenseVector>>) -> Self { | ||
self.vectors = vectors.into(); | ||
self | ||
} | ||
|
||
#[allow(unused_mut)] | ||
pub fn add_vector(mut self, vector: impl Into<DenseVector>) -> Self { | ||
self.vectors.push(vector.into()); | ||
self | ||
} | ||
|
||
/// Builds the desired type. Can often be omitted. | ||
pub fn build(self) -> MultiDenseVector { | ||
MultiDenseVector { | ||
vectors: self.vectors, | ||
} | ||
} | ||
} | ||
|
||
impl From<Vec<Vec<f32>>> for MultiDenseVector { | ||
fn from(vectors: Vec<Vec<f32>>) -> Self { | ||
Self::from( | ||
vectors | ||
.into_iter() | ||
.map(DenseVector::from) | ||
.collect::<Vec<_>>(), | ||
) | ||
} | ||
} | ||
|
||
impl From<Vec<DenseVector>> for MultiDenseVector { | ||
fn from(vectors: Vec<DenseVector>) -> Self { | ||
MultiDenseVectorBuilder::new(vectors).build() | ||
} | ||
} | ||
|
||
impl From<MultiDenseVector> for Vector { | ||
fn from(dense_vector: MultiDenseVector) -> Self { | ||
crate::qdrant::vector::Vector::from(dense_vector).into() | ||
} | ||
} | ||
|
||
impl From<MultiDenseVectorBuilder> for Vector { | ||
fn from(dense_vector: MultiDenseVectorBuilder) -> Self { | ||
crate::qdrant::vector::Vector::from(dense_vector.build()).into() | ||
} | ||
} | ||
|
||
impl From<MultiDenseVector> for crate::qdrant::vector::Vector { | ||
fn from(dense_vector: MultiDenseVector) -> Self { | ||
Self::MultiDense(dense_vector) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::qdrant::*; | ||
|
||
#[derive(Default)] | ||
pub struct SparseVectorBuilder { | ||
pub(crate) indices: Vec<u32>, | ||
pub(crate) values: Vec<f32>, | ||
} | ||
|
||
impl SparseVectorBuilder { | ||
pub fn new(indices: impl Into<Vec<u32>>, values: impl Into<Vec<f32>>) -> Self { | ||
Self { | ||
indices: indices.into(), | ||
values: values.into(), | ||
} | ||
} | ||
|
||
#[allow(unused_mut)] | ||
pub fn indices(mut self, indices: impl Into<Vec<u32>>) -> Self { | ||
self.indices = indices.into(); | ||
self | ||
} | ||
|
||
#[allow(unused_mut)] | ||
pub fn values(mut self, values: impl Into<Vec<f32>>) -> Self { | ||
self.values = values.into(); | ||
self | ||
} | ||
|
||
/// Builds the desired type. Can often be omitted. | ||
pub fn build(self) -> SparseVector { | ||
SparseVector { | ||
indices: self.indices, | ||
values: self.values, | ||
} | ||
} | ||
} | ||
|
||
impl From<(Vec<u32>, Vec<f32>)> for SparseVector { | ||
fn from((indices, values): (Vec<u32>, Vec<f32>)) -> Self { | ||
SparseVectorBuilder::new(indices, values).build() | ||
} | ||
} | ||
|
||
impl From<SparseVector> for Vector { | ||
fn from(dense_vector: SparseVector) -> Self { | ||
crate::qdrant::vector::Vector::from(dense_vector).into() | ||
} | ||
} | ||
|
||
impl From<SparseVectorBuilder> for Vector { | ||
fn from(dense_vector: SparseVectorBuilder) -> Self { | ||
crate::qdrant::vector::Vector::from(dense_vector.build()).into() | ||
} | ||
} | ||
|
||
impl From<SparseVector> for crate::qdrant::vector::Vector { | ||
fn from(dense_vector: SparseVector) -> Self { | ||
Self::Sparse(dense_vector) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters