qdrant_client/qdrant_client/builders/
vectors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::qdrant::{
    DenseVectorBuilder, MultiDenseVector, NamedVectors, SparseVectorBuilder, Vector,
};
use crate::QdrantError;

impl Vector {
    #[inline]
    pub fn new(values: Vec<f32>) -> Self {
        Self::new_dense(values)
    }

    #[inline]
    pub fn new_dense(values: impl Into<Vec<f32>>) -> Self {
        DenseVectorBuilder::new(values.into()).build().into()
    }

    #[inline]
    pub fn new_sparse(indices: impl Into<Vec<u32>>, values: impl Into<Vec<f32>>) -> Self {
        SparseVectorBuilder::new(indices, values).build().into()
    }

    #[inline]
    pub fn new_multi(vectors: impl Into<Vec<Vec<f32>>>) -> Self {
        MultiDenseVector::from(vectors.into()).into()
    }

    pub fn try_into_dense(self) -> Result<Vec<f32>, QdrantError> {
        if self.indices.is_some() {
            return Err(QdrantError::ConversionError(
                "Cannot convert sparse vector to dense".to_string(),
            ));
        }

        if self.vectors_count.is_some() && self.vectors_count.unwrap() > 1 {
            return Err(QdrantError::ConversionError(
                "Cannot convert multi vector to dense".to_string(),
            ));
        }

        Ok(self.data)
    }

    pub fn try_into_sparse(self) -> Result<(Vec<u32>, Vec<f32>), QdrantError> {
        if self.indices.is_none() {
            return Err(QdrantError::ConversionError(
                "Cannot convert dense vector to sparse".to_string(),
            ));
        }

        if self.vectors_count.is_some() && self.vectors_count.unwrap() > 1 {
            return Err(QdrantError::ConversionError(
                "Cannot convert multi vector to sparse".to_string(),
            ));
        }

        let indices = self.indices.unwrap().data;

        if indices.len() != self.data.len() {
            return Err(QdrantError::ConversionError(format!(
                "Malformed sparse vector: indices length {} is not equal to data length {}",
                indices.len(),
                self.data.len()
            )));
        }

        Ok((indices, self.data))
    }

    pub fn try_into_multi(self) -> Result<Vec<Vec<f32>>, QdrantError> {
        if self.vectors_count.is_none() {
            return Err(QdrantError::ConversionError(
                "Cannot convert single vector to multi".to_string(),
            ));
        }

        let vectors_count = self.vectors_count.unwrap();

        if self.data.len() % vectors_count as usize != 0 {
            return Err(QdrantError::ConversionError(format!(
                "Malformed multi vector: data length {} is not divisible by vectors count {}",
                self.data.len(),
                vectors_count
            )));
        }

        Ok(self
            .data
            .chunks(self.data.len() / self.vectors_count.unwrap() as usize)
            .map(|v| v.to_vec())
            .collect())
    }
}

impl NamedVectors {
    pub fn add_vector(mut self, name: impl Into<String>, vector: impl Into<Vector>) -> Self {
        self.vectors.insert(name.into(), vector.into());
        self
    }
}

impl From<crate::qdrant::vector::Vector> for Vector {
    fn from(vector: crate::qdrant::vector::Vector) -> Self {
        Vector {
            vector: Some(vector),
            // Deprecated
            data: vec![],
            indices: None,
            vectors_count: None,
        }
    }
}