qdrant_client/builders/
vector_params_builder.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::grpc_macros::convert_option;
use crate::qdrant::*;

pub struct VectorParamsBuilder {
    /// Size of the vectors
    pub(crate) size: Option<u64>,
    /// Distance function used for comparing vectors
    pub(crate) distance: Option<i32>,
    /// Configuration of vector HNSW graph. If omitted - the collection configuration will be used
    pub(crate) hnsw_config: Option<Option<HnswConfigDiff>>,
    /// Configuration of vector quantization config. If omitted - the collection configuration will be used
    quantization_config: Option<quantization_config::Quantization>,
    /// If true - serve vectors from disk. If set to false, the vectors will be loaded in RAM.
    pub(crate) on_disk: Option<Option<bool>>,
    /// Data type of the vectors
    pub(crate) datatype: Option<Option<i32>>,
    /// Configuration for multi-vector search
    pub(crate) multivector_config: Option<Option<MultiVectorConfig>>,
}

impl VectorParamsBuilder {
    /// Size of the vectors
    #[allow(unused_mut)]
    pub fn size(self, value: u64) -> Self {
        let mut new = self;
        new.size = Option::Some(value);
        new
    }
    /// Distance function used for comparing vectors
    #[allow(unused_mut)]
    pub fn distance<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
        let mut new = self;
        new.distance = Option::Some(value.into());
        new
    }
    /// Configuration of vector HNSW graph. If omitted - the collection configuration will be used
    #[allow(unused_mut)]
    pub fn hnsw_config<VALUE: core::convert::Into<HnswConfigDiff>>(self, value: VALUE) -> Self {
        let mut new = self;
        new.hnsw_config = Option::Some(Option::Some(value.into()));
        new
    }
    /// Configuration of vector quantization config. If omitted - the collection configuration will be used
    #[allow(unused_mut)]
    pub fn quantization_config<VALUE: core::convert::Into<quantization_config::Quantization>>(
        self,
        value: VALUE,
    ) -> Self {
        let mut new = self;
        new.quantization_config = Option::Some(value.into());
        new
    }
    /// If true - serve vectors from disk. If set to false, the vectors will be loaded in RAM.
    #[allow(unused_mut)]
    pub fn on_disk(self, value: bool) -> Self {
        let mut new = self;
        new.on_disk = Option::Some(Option::Some(value));
        new
    }
    /// Data type of the vectors
    #[allow(unused_mut)]
    pub fn datatype<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
        let mut new = self;
        new.datatype = Option::Some(Option::Some(value.into()));
        new
    }
    /// Configuration for multi-vector search
    #[allow(unused_mut)]
    pub fn multivector_config<VALUE: core::convert::Into<MultiVectorConfig>>(
        self,
        value: VALUE,
    ) -> Self {
        let mut new = self;
        new.multivector_config = Option::Some(Option::Some(value.into()));
        new
    }

    fn build_inner(self) -> Result<VectorParams, VectorParamsBuilderError> {
        Ok(VectorParams {
            size: self.size.unwrap_or_default(),
            distance: self.distance.unwrap_or_default(),
            hnsw_config: self.hnsw_config.unwrap_or_default(),
            quantization_config: { convert_option(&self.quantization_config) },
            on_disk: self.on_disk.unwrap_or_default(),
            datatype: self.datatype.unwrap_or_default(),
            multivector_config: self.multivector_config.unwrap_or_default(),
        })
    }
    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
    fn create_empty() -> Self {
        Self {
            size: core::default::Default::default(),
            distance: core::default::Default::default(),
            hnsw_config: core::default::Default::default(),
            quantization_config: core::default::Default::default(),
            on_disk: core::default::Default::default(),
            datatype: core::default::Default::default(),
            multivector_config: core::default::Default::default(),
        }
    }
}

impl From<VectorParamsBuilder> for VectorParams {
    fn from(value: VectorParamsBuilder) -> Self {
        value.build_inner().unwrap_or_else(|_| {
            panic!(
                "Failed to convert {0} to {1}",
                "VectorParamsBuilder", "VectorParams"
            )
        })
    }
}

impl VectorParamsBuilder {
    /// Builds the desired type. Can often be omitted.
    pub fn build(self) -> VectorParams {
        self.build_inner().unwrap_or_else(|_| {
            panic!(
                "Failed to build {0} into {1}",
                "VectorParamsBuilder", "VectorParams"
            )
        })
    }
}

impl VectorParamsBuilder {
    pub(crate) fn empty() -> Self {
        Self::create_empty()
    }
}

#[non_exhaustive]
#[derive(Debug)]
pub enum VectorParamsBuilderError {
    /// Uninitialized field
    UninitializedField(&'static str),
    /// Custom validation error
    ValidationError(String),
}

// Implementing the Display trait for better error messages
impl std::fmt::Display for VectorParamsBuilderError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::UninitializedField(field) => {
                write!(f, "`{}` must be initialized", field)
            }
            Self::ValidationError(error) => write!(f, "{}", error),
        }
    }
}

// Implementing the Error trait
impl std::error::Error for VectorParamsBuilderError {}

// Implementing From trait for conversion from UninitializedFieldError
impl From<derive_builder::UninitializedFieldError> for VectorParamsBuilderError {
    fn from(error: derive_builder::UninitializedFieldError) -> Self {
        Self::UninitializedField(error.field_name())
    }
}

// Implementing From trait for conversion from String
impl From<String> for VectorParamsBuilderError {
    fn from(error: String) -> Self {
        Self::ValidationError(error)
    }
}