Skip to main content

qdrant_client/builders/
decay_params_expression_builder.rs

1use crate::qdrant::*;
2
3/// Builder for the DecayParamsExpression struct, which represents parameters for decay functions.
4///
5/// Decay functions (exponential, Gaussian, linear) are used in scoring to create a decay effect
6/// based on distance from a target value.
7#[must_use]
8#[derive(Clone)]
9pub struct DecayParamsExpressionBuilder {
10    /// The variable to decay
11    pub(crate) x: Expression,
12    /// The target value to start decaying from. Defaults to 0.
13    pub(crate) target: Option<Expression>,
14    /// The scale factor of the decay, in terms of `x`. Defaults to 1.0. Must be a non-zero positive number.
15    pub(crate) scale: Option<f32>,
16    /// The midpoint of the decay. Defaults to 0.5. Output will be this value when `|x - target| == scale`.
17    pub(crate) midpoint: Option<f32>,
18}
19
20impl DecayParamsExpressionBuilder {
21    /// Creates a new DecayParamsExpressionBuilder with the variable to decay.
22    /// This is the only required parameter.
23    pub fn new<E: Into<Expression>>(x: E) -> Self {
24        Self {
25            x: x.into(),
26            target: None,
27            scale: None,
28            midpoint: None,
29        }
30    }
31
32    /// Sets the variable to decay. This is the value that will be compared to the target.
33    pub fn x<E: Into<Expression>>(self, x: E) -> Self {
34        let mut new = self;
35        new.x = x.into();
36        new
37    }
38
39    /// Sets the target value to start decaying from. Defaults to 0 if not specified.
40    /// The decay is at its maximum (1.0) when x equals the target.
41    pub fn target<E: Into<Expression>>(self, target: E) -> Self {
42        let mut new = self;
43        new.target = Some(target.into());
44        new
45    }
46
47    /// Sets the scale factor of the decay, in terms of `x`. Defaults to 1.0 if not specified.
48    /// Must be a non-zero positive number.
49    /// This controls how quickly the function decays away from the target value.
50    pub fn scale(self, scale: f32) -> Self {
51        let mut new = self;
52        new.scale = Some(scale);
53        new
54    }
55
56    /// Sets the midpoint of the decay. Defaults to 0.5 if not specified.
57    /// The output will be this value when the distance between x and target equals the scale.
58    pub fn midpoint(self, midpoint: f32) -> Self {
59        let mut new = self;
60        new.midpoint = Some(midpoint);
61        new
62    }
63
64    fn build_inner(self) -> Result<DecayParamsExpression, std::convert::Infallible> {
65        Ok(DecayParamsExpression {
66            x: Some(Box::new(self.x)),
67            target: self.target.map(Box::new),
68            scale: self.scale,
69            midpoint: self.midpoint,
70        })
71    }
72}
73
74impl From<DecayParamsExpressionBuilder> for DecayParamsExpression {
75    fn from(value: DecayParamsExpressionBuilder) -> Self {
76        value.build_inner().unwrap_or_else(|_| {
77            panic!(
78                "Failed to convert {0} to {1}",
79                "DecayParamsExpressionBuilder", "DecayParamsExpression"
80            )
81        })
82    }
83}
84
85impl DecayParamsExpressionBuilder {
86    /// Builds the DecayParamsExpression from this builder.
87    pub fn build(self) -> DecayParamsExpression {
88        self.build_inner().unwrap_or_else(|_| {
89            panic!(
90                "Failed to build {0} into {1}",
91                "DecayParamsExpressionBuilder", "DecayParamsExpression"
92            )
93        })
94    }
95}