Skip to content

Commit bc02fce

Browse files
committed
feat: created vector search
1 parent 874f6df commit bc02fce

6 files changed

Lines changed: 220 additions & 310 deletions

File tree

kq-cli/src/main.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,12 @@ struct SearchArgs {
8585
/// Use full-text search (default, on by default)
8686
#[arg(long, default_value_t = true)]
8787
fts: bool,
88-
8988
/// Path to the knowledge repository (auto-detected by default)
9089
#[arg(long, value_name = "PATH")]
9190
repo: Option<PathBuf>,
92-
93-
/// Use vector search (requires embedding model)
94-
#[arg(long, default_value_t = false)]
95-
vector: bool,
91+
/// Use vector search (requires embedding model)
92+
#[arg(long, default_value_t = false)]
93+
vector: bool,
9694
}
9795

9896
#[derive(Parser)]
@@ -418,6 +416,28 @@ fn main() -> Result<()> {
418416
return Ok(());
419417
}
420418

419+
if args.vector {
420+
let mut model = kq_embeddings::EmbeddingModel::new(
421+
&repo_path.join(".kq/model-cache")
422+
)?;
423+
match model.load() {
424+
Ok(()) => {
425+
let emb = model.embed(&args.query)?;
426+
let results = kq_core::vector::hybrid_search(
427+
&conn, &args.query, &emb, args.limit, args.limit
428+
)?;
429+
for r in &results {
430+
println!(" {:6.2} {}", r.score, r.path);
431+
}
432+
}
433+
Err(e) => {
434+
eprintln!("Vector search unavailable: {e}");
435+
eprintln!("Model will be downloaded on first vector search.");
436+
}
437+
}
438+
return Ok(());
439+
}
440+
421441
let results = kq_core::search::search_fts(&conn, &args.query, args.limit)?;
422442

423443
if results.is_empty() {

kq-core/src/db.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn init_db(db_path: &Path) -> Result<()> {
2929
}
3030

3131
let conn = Connection::open(db_path)?;
32+
crate::vector::register_vec_on_connection(&conn)?;
3233
create_schema(&conn)?;
3334
DB.set(Mutex::new(conn))
3435
.map_err(|_| anyhow::anyhow!("Database already initialized (race)"))?;
@@ -135,6 +136,8 @@ pub fn create_schema(conn: &Connection) -> Result<()> {
135136
CREATE INDEX IF NOT EXISTS idx_code_anchors_anchor ON code_anchors(anchor);
136137
",
137138
)?;
139+
// Initialize vector embedding tables
140+
crate::vector::init_vector(conn)?;
138141
Ok(())
139142
}
140143

0 commit comments

Comments
 (0)