Skip to content

Commit a8bf329

Browse files
committed
Add dense, sparse and multi dense vector builders and conversions
1 parent e5dec4c commit a8bf329

File tree

5 files changed

+213
-35
lines changed

5 files changed

+213
-35
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::qdrant::*;
2+
3+
pub struct DenseVectorBuilder {
4+
pub(crate) values: Vec<f32>,
5+
}
6+
7+
impl DenseVectorBuilder {
8+
pub fn new(values: impl Into<Vec<f32>>) -> Self {
9+
Self {
10+
values: values.into(),
11+
}
12+
}
13+
14+
#[allow(unused_mut)]
15+
pub fn values(mut self, values: impl Into<Vec<f32>>) -> Self {
16+
self.values = values.into();
17+
self
18+
}
19+
20+
/// Builds the desired type. Can often be omitted.
21+
pub fn build(self) -> DenseVector {
22+
DenseVector { data: self.values }
23+
}
24+
}
25+
26+
impl From<Vec<f32>> for DenseVector {
27+
fn from(values: Vec<f32>) -> Self {
28+
DenseVectorBuilder::new(values).build()
29+
}
30+
}
31+
32+
impl From<DenseVector> for Vector {
33+
fn from(dense_vector: DenseVector) -> Self {
34+
crate::qdrant::vector::Vector::from(dense_vector).into()
35+
}
36+
}
37+
38+
impl From<DenseVectorBuilder> for Vector {
39+
fn from(dense_vector: DenseVectorBuilder) -> Self {
40+
crate::qdrant::vector::Vector::from(dense_vector.build()).into()
41+
}
42+
}
43+
44+
impl From<DenseVector> for crate::qdrant::vector::Vector {
45+
fn from(dense_vector: DenseVector) -> Self {
46+
Self::Dense(dense_vector)
47+
}
48+
}

src/builders/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,12 @@ pub use count_points_builder::*;
219219

220220
pub mod has_vector_condition_builder;
221221
pub use has_vector_condition_builder::HasVectorConditionBuilder;
222+
223+
pub mod dense_vector_builder;
224+
pub use dense_vector_builder::DenseVectorBuilder;
225+
226+
pub mod sparse_vector_builder;
227+
pub use sparse_vector_builder::SparseVectorBuilder;
228+
229+
pub mod multi_dense_vector_builder;
230+
pub use multi_dense_vector_builder::MultiDenseVectorBuilder;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use crate::qdrant::*;
2+
3+
#[derive(Default)]
4+
pub struct MultiDenseVectorBuilder {
5+
pub(crate) vectors: Vec<DenseVector>,
6+
}
7+
8+
impl MultiDenseVectorBuilder {
9+
pub fn new(vectors: impl Into<Vec<DenseVector>>) -> Self {
10+
Self {
11+
vectors: vectors.into(),
12+
}
13+
}
14+
15+
pub fn single(vector: impl Into<DenseVector>) -> Self {
16+
Self::new(vec![vector.into()])
17+
}
18+
19+
#[allow(unused_mut)]
20+
pub fn vectors(mut self, vectors: impl Into<Vec<DenseVector>>) -> Self {
21+
self.vectors = vectors.into();
22+
self
23+
}
24+
25+
#[allow(unused_mut)]
26+
pub fn add_vector(mut self, vector: impl Into<DenseVector>) -> Self {
27+
self.vectors.push(vector.into());
28+
self
29+
}
30+
31+
/// Builds the desired type. Can often be omitted.
32+
pub fn build(self) -> MultiDenseVector {
33+
MultiDenseVector {
34+
vectors: self.vectors,
35+
}
36+
}
37+
}
38+
39+
impl From<Vec<Vec<f32>>> for MultiDenseVector {
40+
fn from(vectors: Vec<Vec<f32>>) -> Self {
41+
Self::from(
42+
vectors
43+
.into_iter()
44+
.map(DenseVector::from)
45+
.collect::<Vec<_>>(),
46+
)
47+
}
48+
}
49+
50+
impl From<Vec<DenseVector>> for MultiDenseVector {
51+
fn from(vectors: Vec<DenseVector>) -> Self {
52+
MultiDenseVectorBuilder::new(vectors).build()
53+
}
54+
}
55+
56+
impl From<MultiDenseVector> for Vector {
57+
fn from(dense_vector: MultiDenseVector) -> Self {
58+
crate::qdrant::vector::Vector::from(dense_vector).into()
59+
}
60+
}
61+
62+
impl From<MultiDenseVectorBuilder> for Vector {
63+
fn from(dense_vector: MultiDenseVectorBuilder) -> Self {
64+
crate::qdrant::vector::Vector::from(dense_vector.build()).into()
65+
}
66+
}
67+
68+
impl From<MultiDenseVector> for crate::qdrant::vector::Vector {
69+
fn from(dense_vector: MultiDenseVector) -> Self {
70+
Self::MultiDense(dense_vector)
71+
}
72+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::qdrant::*;
2+
3+
#[derive(Default)]
4+
pub struct SparseVectorBuilder {
5+
pub(crate) indices: Vec<u32>,
6+
pub(crate) values: Vec<f32>,
7+
}
8+
9+
impl SparseVectorBuilder {
10+
pub fn new(indices: impl Into<Vec<u32>>, values: impl Into<Vec<f32>>) -> Self {
11+
Self {
12+
indices: indices.into(),
13+
values: values.into(),
14+
}
15+
}
16+
17+
#[allow(unused_mut)]
18+
pub fn indices(mut self, indices: impl Into<Vec<u32>>) -> Self {
19+
self.indices = indices.into();
20+
self
21+
}
22+
23+
#[allow(unused_mut)]
24+
pub fn values(mut self, values: impl Into<Vec<f32>>) -> Self {
25+
self.values = values.into();
26+
self
27+
}
28+
29+
/// Builds the desired type. Can often be omitted.
30+
pub fn build(self) -> SparseVector {
31+
SparseVector {
32+
indices: self.indices,
33+
values: self.values,
34+
}
35+
}
36+
}
37+
38+
impl From<(Vec<u32>, Vec<f32>)> for SparseVector {
39+
fn from((indices, values): (Vec<u32>, Vec<f32>)) -> Self {
40+
SparseVectorBuilder::new(indices, values).build()
41+
}
42+
}
43+
44+
impl From<SparseVector> for Vector {
45+
fn from(dense_vector: SparseVector) -> Self {
46+
crate::qdrant::vector::Vector::from(dense_vector).into()
47+
}
48+
}
49+
50+
impl From<SparseVectorBuilder> for Vector {
51+
fn from(dense_vector: SparseVectorBuilder) -> Self {
52+
crate::qdrant::vector::Vector::from(dense_vector.build()).into()
53+
}
54+
}
55+
56+
impl From<SparseVector> for crate::qdrant::vector::Vector {
57+
fn from(dense_vector: SparseVector) -> Self {
58+
Self::Sparse(dense_vector)
59+
}
60+
}

src/qdrant_client/builders/vectors.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,27 @@
1-
use crate::qdrant::{DenseVector, MultiDenseVector, NamedVectors, SparseVector, Vector};
1+
use crate::qdrant::{
2+
DenseVectorBuilder, MultiDenseVector, NamedVectors, SparseVectorBuilder, Vector,
3+
};
24
use crate::QdrantError;
35

46
impl Vector {
7+
#[inline]
58
pub fn new(values: Vec<f32>) -> Self {
69
Self::new_dense(values)
710
}
811

9-
pub fn new_dense(values: Vec<f32>) -> Self {
10-
Vector {
11-
vector: Some(crate::qdrant::vector::Vector::Dense(DenseVector {
12-
data: values,
13-
})),
14-
// Deprecated
15-
data: vec![],
16-
indices: None,
17-
vectors_count: None,
18-
}
12+
#[inline]
13+
pub fn new_dense(values: impl Into<Vec<f32>>) -> Self {
14+
DenseVectorBuilder::new(values.into()).build().into()
1915
}
2016

17+
#[inline]
2118
pub fn new_sparse(indices: impl Into<Vec<u32>>, values: impl Into<Vec<f32>>) -> Self {
22-
Vector {
23-
vector: Some(crate::qdrant::vector::Vector::Sparse(SparseVector {
24-
values: values.into(),
25-
indices: indices.into(),
26-
})),
27-
// Deprecated
28-
data: vec![],
29-
indices: None,
30-
vectors_count: None,
31-
}
19+
SparseVectorBuilder::new(indices, values).build().into()
3220
}
3321

34-
pub fn new_multi(values: Vec<Vec<f32>>) -> Self {
35-
let vectors = values
36-
.into_iter()
37-
.map(|data| DenseVector { data })
38-
.collect();
39-
Vector {
40-
vector: Some(crate::qdrant::vector::Vector::MultiDense(
41-
MultiDenseVector { vectors },
42-
)),
43-
// Deprecated
44-
data: vec![],
45-
indices: None,
46-
vectors_count: None,
47-
}
22+
#[inline]
23+
pub fn new_multi(vectors: impl Into<Vec<Vec<f32>>>) -> Self {
24+
MultiDenseVector::from(vectors.into()).into()
4825
}
4926

5027
pub fn try_into_dense(self) -> Result<Vec<f32>, QdrantError> {
@@ -120,3 +97,15 @@ impl NamedVectors {
12097
self
12198
}
12299
}
100+
101+
impl From<crate::qdrant::vector::Vector> for Vector {
102+
fn from(vector: crate::qdrant::vector::Vector) -> Self {
103+
Vector {
104+
vector: Some(vector),
105+
// Deprecated
106+
data: vec![],
107+
indices: None,
108+
vectors_count: None,
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)