MCPcopy Index your code
hub / github.com/InfinitiBit/graphbit / search

Method search

core/src/memory/vector.rs:44–71  ·  view source on GitHub ↗

Search for the `top_k` most similar entries to `query_embedding`, returning `(MemoryId, similarity_score)` pairs above `threshold`.

(
        &self,
        query_embedding: &[f32],
        top_k: usize,
        threshold: f64,
    )

Source from the content-addressed store, hash-verified

42 /// Search for the `top_k` most similar entries to `query_embedding`,
43 /// returning `(MemoryId, similarity_score)` pairs above `threshold`.
44 pub async fn search(
45 &self,
46 query_embedding: &[f32],
47 top_k: usize,
48 threshold: f64,
49 ) -> GraphBitResult<Vec<(MemoryId, f64)>> {
50 let entries = self.entries.read().await;
51
52 let mut scored: Vec<(MemoryId, f64)> = entries
53 .iter()
54 .filter_map(|entry| {
55 let sim = EmbeddingService::cosine_similarity(query_embedding, &entry.embedding)
56 .ok()?;
57 let sim_f64 = f64::from(sim);
58 if sim_f64 >= threshold {
59 Some((entry.memory_id.clone(), sim_f64))
60 } else {
61 None
62 }
63 })
64 .collect();
65
66 // Sort descending by score.
67 scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
68 scored.truncate(top_k);
69
70 Ok(scored)
71 }
72
73 /// Remove entries for a specific memory.
74 pub async fn remove(&self, memory_id: &MemoryId) {

Callers 4

test_vector_index_updateFunction · 0.45
test_vector_index_clearFunction · 0.45

Calls 1

cloneMethod · 0.80

Tested by 4

test_vector_index_updateFunction · 0.36
test_vector_index_clearFunction · 0.36