Skip to main content

qdrant_client/builders/
vector_params_builder.rs

1use crate::grpc_macros::convert_option;
2use crate::qdrant::*;
3
4#[must_use]
5#[derive(Clone)]
6pub struct VectorParamsBuilder {
7    /// Size of the vectors
8    pub(crate) size: Option<u64>,
9    /// Distance function used for comparing vectors
10    pub(crate) distance: Option<i32>,
11    /// Configuration of vector HNSW graph. If omitted - the collection configuration will be used
12    pub(crate) hnsw_config: Option<Option<HnswConfigDiff>>,
13    /// Configuration of vector quantization config. If omitted - the collection configuration will be used
14    quantization_config: Option<quantization_config::Quantization>,
15    /// If true - serve vectors from disk. If set to false, the vectors will be loaded in RAM.
16    pub(crate) on_disk: Option<Option<bool>>,
17    /// Data type of the vectors
18    pub(crate) datatype: Option<Option<i32>>,
19    /// Configuration for multi-vector search
20    pub(crate) multivector_config: Option<Option<MultiVectorConfig>>,
21}
22
23impl VectorParamsBuilder {
24    /// Size of the vectors
25    pub fn size(self, value: u64) -> Self {
26        let mut new = self;
27        new.size = Option::Some(value);
28        new
29    }
30    /// Distance function used for comparing vectors
31    pub fn distance<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
32        let mut new = self;
33        new.distance = Option::Some(value.into());
34        new
35    }
36    /// Configuration of vector HNSW graph. If omitted - the collection configuration will be used
37    pub fn hnsw_config<VALUE: core::convert::Into<HnswConfigDiff>>(self, value: VALUE) -> Self {
38        let mut new = self;
39        new.hnsw_config = Option::Some(Option::Some(value.into()));
40        new
41    }
42    /// Configuration of vector quantization config. If omitted - the collection configuration will be used
43    pub fn quantization_config<VALUE: core::convert::Into<quantization_config::Quantization>>(
44        self,
45        value: VALUE,
46    ) -> Self {
47        let mut new = self;
48        new.quantization_config = Option::Some(value.into());
49        new
50    }
51    /// If true - serve vectors from disk. If set to false, the vectors will be loaded in RAM.
52    pub fn on_disk(self, value: bool) -> Self {
53        let mut new = self;
54        new.on_disk = Option::Some(Option::Some(value));
55        new
56    }
57    /// Data type of the vectors
58    pub fn datatype<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
59        let mut new = self;
60        new.datatype = Option::Some(Option::Some(value.into()));
61        new
62    }
63    /// Configuration for multi-vector search
64    pub fn multivector_config<VALUE: core::convert::Into<MultiVectorConfig>>(
65        self,
66        value: VALUE,
67    ) -> Self {
68        let mut new = self;
69        new.multivector_config = Option::Some(Option::Some(value.into()));
70        new
71    }
72
73    fn build_inner(self) -> Result<VectorParams, VectorParamsBuilderError> {
74        Ok(VectorParams {
75            size: self.size.unwrap_or_default(),
76            distance: self.distance.unwrap_or_default(),
77            hnsw_config: self.hnsw_config.unwrap_or_default(),
78            quantization_config: { convert_option(&self.quantization_config) },
79            on_disk: self.on_disk.unwrap_or_default(),
80            datatype: self.datatype.unwrap_or_default(),
81            multivector_config: self.multivector_config.unwrap_or_default(),
82        })
83    }
84    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
85    fn create_empty() -> Self {
86        Self {
87            size: core::default::Default::default(),
88            distance: core::default::Default::default(),
89            hnsw_config: core::default::Default::default(),
90            quantization_config: core::default::Default::default(),
91            on_disk: core::default::Default::default(),
92            datatype: core::default::Default::default(),
93            multivector_config: core::default::Default::default(),
94        }
95    }
96}
97
98impl From<VectorParamsBuilder> for VectorParams {
99    fn from(value: VectorParamsBuilder) -> Self {
100        value.build_inner().unwrap_or_else(|_| {
101            panic!(
102                "Failed to convert {0} to {1}",
103                "VectorParamsBuilder", "VectorParams"
104            )
105        })
106    }
107}
108
109impl VectorParamsBuilder {
110    /// Builds the desired type. Can often be omitted.
111    pub fn build(self) -> VectorParams {
112        self.build_inner().unwrap_or_else(|_| {
113            panic!(
114                "Failed to build {0} into {1}",
115                "VectorParamsBuilder", "VectorParams"
116            )
117        })
118    }
119}
120
121impl VectorParamsBuilder {
122    pub(crate) fn empty() -> Self {
123        Self::create_empty()
124    }
125}
126
127#[non_exhaustive]
128#[derive(Debug)]
129pub enum VectorParamsBuilderError {
130    /// Uninitialized field
131    UninitializedField(&'static str),
132    /// Custom validation error
133    ValidationError(String),
134}
135
136// Implementing the Display trait for better error messages
137impl std::fmt::Display for VectorParamsBuilderError {
138    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
139        match self {
140            Self::UninitializedField(field) => {
141                write!(f, "`{field}` must be initialized")
142            }
143            Self::ValidationError(error) => write!(f, "{error}"),
144        }
145    }
146}
147
148// Implementing the Error trait
149impl std::error::Error for VectorParamsBuilderError {}
150
151// Implementing From trait for conversion from UninitializedFieldError
152impl From<derive_builder::UninitializedFieldError> for VectorParamsBuilderError {
153    fn from(error: derive_builder::UninitializedFieldError) -> Self {
154        Self::UninitializedField(error.field_name())
155    }
156}
157
158// Implementing From trait for conversion from String
159impl From<String> for VectorParamsBuilderError {
160    fn from(error: String) -> Self {
161        Self::ValidationError(error)
162    }
163}