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