-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_search.rs
More file actions
83 lines (68 loc) · 2.31 KB
/
Copy pathcode_search.rs
File metadata and controls
83 lines (68 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Code search example
//!
//! This example demonstrates code-aware chunking and search
use knowledge_vault::{
embeddings::{DocType, EmbeddingPipeline},
Chunker, KnowledgeVault, PlaceholderEmbedder,
};
use std::path::PathBuf;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let vault = KnowledgeVault::open(PathBuf::from("code_vault.db"), 384)?;
println!("✓ Opened code vault");
// Sample Rust code
let code = r#"
pub struct VectorStore {
data: Vec<Vec<f32>>,
dimension: usize,
}
impl VectorStore {
pub fn new(dimension: usize) -> Self {
Self {
data: Vec::new(),
dimension,
}
}
pub fn add(&mut self, embedding: Vec<f32>) -> Result<(), String> {
if embedding.len() != self.dimension {
return Err("Dimension mismatch".to_string());
}
self.data.push(embedding);
Ok(())
}
pub fn search(&self, query: &[f32], k: usize) -> Vec<usize> {
// Calculate similarities
let mut similarities: Vec<(usize, f32)> = self.data
.iter()
.enumerate()
.map(|(i, emb)| (i, cosine_similarity(query, emb)))
.collect();
// Sort by similarity
similarities.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap());
// Return top k
similarities.into_iter().take(k).map(|(i, _)| i).collect()
}
}
fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
dot / (norm_a * norm_b)
}
"#;
// Use document chunker for code-aware splitting
let pipeline = EmbeddingPipeline::new(PathBuf::from("/tmp/model"))?;
// Chunk as code
let chunks = pipeline.chunk_document(code, DocType::Code);
println!("✓ Chunked code into {} pieces", chunks.len());
// Display chunks
for (i, chunk) in chunks.iter().take(3).enumerate() {
println!("\n--- Chunk {} ---", i + 1);
println!("Language: {:?}", chunk.metadata.language);
println!("Content:\n{}", chunk.content);
}
std::fs::remove_file("code_vault.db")?;
println!("\n✓ Cleaned up");
Ok(())
}