Skip to content

Commit 8357f68

Browse files
cosziotimvisee
andcommitted
Update client for 1.14 (#218)
* update protos, fix strictmode * add formula builder and expression constructors * add builder for decay params expression * add helper fns for StrictModeConfigBuilder * add builder for StrictModeMultivectorConfigBuilder * add builder for StrictModeSparseConfig * clippy * additional helpers * validate docs snippets * clippy * helper: set all defaults in formula * move snippets to query_points.rs * no option for mandatory fields * pass HardwareUsage ownership * builders for score variables * Use new score function in tests --------- Co-authored-by: timvisee <[email protected]>
1 parent 014f149 commit 8357f68

22 files changed

+1158
-52
lines changed

proto/collections.proto

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,25 @@ message StrictModeConfig {
342342
optional uint64 max_collection_payload_size_bytes = 13;
343343
optional uint64 filter_max_conditions = 14;
344344
optional uint64 condition_max_size = 15;
345+
optional StrictModeMultivectorConfig multivector_config = 16;
346+
optional StrictModeSparseConfig sparse_config = 17;
347+
optional uint64 max_points_count = 18;
348+
}
349+
350+
message StrictModeSparseConfig {
351+
map<string, StrictModeSparse> sparse_config = 1;
352+
}
353+
354+
message StrictModeSparse {
355+
optional uint64 max_length = 10;
356+
}
357+
358+
message StrictModeMultivectorConfig {
359+
map<string, StrictModeMultivector> multivector_config = 1;
360+
}
361+
362+
message StrictModeMultivector {
363+
optional uint64 max_vectors = 1;
345364
}
346365

347366
message CreateCollection {

proto/points.proto

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ enum RecommendStrategy {
425425
// examples, its score is then chosen from the `max(max_pos_score, max_neg_score)`.
426426
// If the `max_neg_score` is chosen then it is squared and negated.
427427
BestScore = 1;
428+
429+
// Uses custom search objective. Compares against all inputs, sums all the scores.
430+
// Scores against positive vectors are added, against negatives are subtracted.
431+
SumScores = 2;
428432
}
429433

430434
message LookupLocation {
@@ -572,6 +576,70 @@ enum Sample {
572576
Random = 0;
573577
}
574578

579+
message Formula {
580+
Expression expression = 1;
581+
map<string, Value> defaults = 2;
582+
}
583+
584+
message Expression {
585+
oneof variant {
586+
float constant = 1;
587+
string variable = 2; // Payload key or reference to score.
588+
Condition condition = 3; // Payload condition. If true, becomes 1.0; otherwise 0.0
589+
GeoDistance geo_distance = 4; // Geographic distance in meters
590+
string datetime = 5; // Date-time constant
591+
string datetime_key = 6; // Payload key with date-time values
592+
MultExpression mult = 7; // Multiply
593+
SumExpression sum = 8; // Sum
594+
DivExpression div = 9; // Divide
595+
Expression neg = 10; // Negate
596+
Expression abs = 11; // Absolute value
597+
Expression sqrt = 12; // Square root
598+
PowExpression pow = 13; // Power
599+
Expression exp = 14; // Exponential
600+
Expression log10 = 15; // Logarithm
601+
Expression ln = 16; // Natural logarithm
602+
DecayParamsExpression exp_decay = 17; // Exponential decay
603+
DecayParamsExpression gauss_decay = 18; // Gaussian decay
604+
DecayParamsExpression lin_decay = 19; // Linear decay
605+
}
606+
}
607+
608+
message GeoDistance {
609+
GeoPoint origin = 1;
610+
string to = 2;
611+
}
612+
613+
message MultExpression {
614+
repeated Expression mult = 1;
615+
}
616+
617+
message SumExpression {
618+
repeated Expression sum = 1;
619+
}
620+
621+
message DivExpression {
622+
Expression left = 1;
623+
Expression right = 2;
624+
optional float by_zero_default = 3;
625+
}
626+
627+
message PowExpression {
628+
Expression base = 1;
629+
Expression exponent = 2;
630+
}
631+
632+
message DecayParamsExpression {
633+
// The variable to decay
634+
Expression x = 1;
635+
// The target value to start decaying from. Defaults to 0.
636+
optional Expression target = 2;
637+
// The scale factor of the decay, in terms of `x`. Defaults to 1.0. Must be a non-zero positive number.
638+
optional float scale = 3;
639+
// The midpoint of the decay. Defaults to 0.5. Output will be this value when `|x - target| == scale`.
640+
optional float midpoint = 4;
641+
}
642+
575643
message Query {
576644
oneof variant {
577645
VectorInput nearest = 1; // Find the nearest neighbors to this vector.
@@ -581,6 +649,7 @@ message Query {
581649
OrderBy order_by = 5; // Order the points by a payload field.
582650
Fusion fusion = 6; // Fuse the results of multiple prefetches.
583651
Sample sample = 7; // Sample points from the collection.
652+
Formula formula = 8; // Score boosting via an arbitrary formula
584653
}
585654
}
586655

@@ -761,6 +830,7 @@ message UpdateBatchPoints {
761830
message PointsOperationResponse {
762831
UpdateResult result = 1;
763832
double time = 2; // Time spent to process
833+
optional HardwareUsage usage = 3;
764834
}
765835

766836
message UpdateResult {
@@ -864,6 +934,7 @@ message ScrollResponse {
864934
optional PointId next_page_offset = 1; // Use this offset for the next query
865935
repeated RetrievedPoint result = 2;
866936
double time = 3; // Time spent to process
937+
optional HardwareUsage usage = 4;
867938
}
868939

869940
message CountResult {
@@ -882,6 +953,7 @@ message RetrievedPoint {
882953
message GetResponse {
883954
repeated RetrievedPoint result = 1;
884955
double time = 2; // Time spent to process
956+
optional HardwareUsage usage = 3;
885957
}
886958

887959
message RecommendResponse {
@@ -994,6 +1066,8 @@ message FieldCondition {
9941066
ValuesCount values_count = 6; // Check number of values for a specific field
9951067
GeoPolygon geo_polygon = 7; // Check if geo point is within a given polygon
9961068
DatetimeRange datetime_range = 8; // Check if datetime is within a given range
1069+
optional bool is_empty = 9; // Check if field is empty
1070+
optional bool is_null = 10; // Check if field is null
9971071
}
9981072

9991073
message Match {
@@ -1098,6 +1172,10 @@ message GeoPoint {
10981172

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

src/builders/formula_builder.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::collections::HashMap;
2+
3+
use crate::qdrant::*;
4+
5+
/// Builder for the Formula struct, which represents a scoring formula for points.
6+
///
7+
/// The Formula struct is used to define custom scoring expressions and default values.
8+
pub struct FormulaBuilder {
9+
/// The expression that defines how to score points.
10+
pub(crate) expression: Expression,
11+
/// Default values to use for undefined variables in the expression.
12+
pub(crate) defaults: HashMap<String, Value>,
13+
}
14+
15+
impl FormulaBuilder {
16+
/// Sets the expression for the formula.
17+
pub fn new<E: Into<Expression>>(expression: E) -> Self {
18+
Self {
19+
expression: expression.into(),
20+
defaults: Default::default(),
21+
}
22+
}
23+
24+
pub fn expression<E: Into<Expression>>(self, value: E) -> Self {
25+
let mut new = self;
26+
new.expression = value.into();
27+
new
28+
}
29+
30+
/// Sets all default values for the formula.
31+
pub fn defaults(mut self, defaults: HashMap<String, Value>) -> Self {
32+
self.defaults = defaults;
33+
self
34+
}
35+
36+
/// Adds a single default value to the formula's defaults.
37+
pub fn add_default<K: Into<String>, V: Into<Value>>(self, key: K, value: V) -> Self {
38+
let mut new = self;
39+
new.defaults.insert(key.into(), value.into());
40+
new
41+
}
42+
43+
fn build_inner(self) -> Result<Formula, std::convert::Infallible> {
44+
Ok(Formula {
45+
expression: Some(self.expression),
46+
defaults: self.defaults,
47+
})
48+
}
49+
}
50+
51+
impl From<FormulaBuilder> for Formula {
52+
fn from(value: FormulaBuilder) -> Self {
53+
value
54+
.build_inner()
55+
.unwrap_or_else(|_| panic!("Failed to convert {0} to {1}", "FormulaBuilder", "Formula"))
56+
}
57+
}
58+
59+
impl FormulaBuilder {
60+
/// Builds the desired Formula type.
61+
pub fn build(self) -> Formula {
62+
self.build_inner()
63+
.unwrap_or_else(|_| panic!("Failed to build {0} into {1}", "FormulaBuilder", "Formula"))
64+
}
65+
}

src/builders/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ pub use binary_quantization_builder::BinaryQuantizationBuilder;
3434
mod strict_mode_config_builder;
3535
pub use strict_mode_config_builder::StrictModeConfigBuilder;
3636

37+
mod strict_mode_multivector_config_builder;
38+
pub use strict_mode_multivector_config_builder::StrictModeMultivectorConfigBuilder;
39+
40+
mod strict_mode_sparse_config_builder;
41+
pub use strict_mode_sparse_config_builder::StrictModeSparseConfigBuilder;
42+
3743
mod update_collection_builder;
3844
pub use update_collection_builder::UpdateCollectionBuilder;
3945

@@ -199,6 +205,12 @@ pub use query_batch_points_builder::QueryBatchPointsBuilder;
199205
mod query_point_groups_builder;
200206
pub use query_point_groups_builder::QueryPointGroupsBuilder;
201207

208+
mod formula_builder;
209+
pub use formula_builder::FormulaBuilder;
210+
211+
mod decay_params_expression_builder;
212+
pub use decay_params_expression_builder::DecayParamsExpressionBuilder;
213+
202214
mod facet_counts_builder;
203215
pub use facet_counts_builder::FacetCountsBuilder;
204216

0 commit comments

Comments
 (0)