Skip to content

Commit 31d97c5

Browse files
committed
fix(example/surrealdb): calculate threshold from results
Uses the model results to calculate the threshold in order to guarantee exactly one result returned when including a threshold.
1 parent ff97041 commit 31d97c5

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

rig-integrations/rig-surrealdb/examples/vector_search_surreal.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,18 @@ async fn main() -> Result<(), anyhow::Error> {
7878
println!("{} results for query: {}", results.len(), query);
7979
for (distance, _id, doc) in results.iter() {
8080
println!("Result distance {distance} for word: {doc}");
81-
82-
// expected output
83-
// Result distance 0.693218142100547 for word: glarb-glarb
84-
// Result distance 0.2529120980283861 for word: linglingdong
8581
}
8682

87-
println!("Attempting vector search with cosine similarity threshold of 0.5 and query: {query}");
83+
// Use the midpoint as similarity threshold to guarantee exactly one result is returned.
84+
let midpoint = (results[0].0 + results[1].0) / 2.0;
85+
86+
println!(
87+
"Attempting vector search with cosine similarity threshold of {midpoint} and query: {query}"
88+
);
8889
let req = VectorSearchRequest::builder()
8990
.query(query)
9091
.samples(2)
91-
.threshold(0.5)
92+
.threshold(midpoint)
9293
.build()?;
9394

9495
let results = vector_store.top_n::<WordDefinition>(req).await?;

0 commit comments

Comments
 (0)