|
| 1 | +//! Opt-in semantic embeddings (Cargo feature `embeddings`). |
| 2 | +//! |
| 3 | +//! Pure-Rust static code embeddings via `model2vec-rs` (default |
| 4 | +//! `minishlab/potion-code-16M`, 256-dim), stored and searched in `sqlite-vec`. |
| 5 | +//! The feature is off by default so the musl static binary stays lean; this |
| 6 | +//! module exposes the *same* surface in both builds — when the feature is off, |
| 7 | +//! every entry point is a no-op and semantic search degrades to FTS. |
| 8 | +
|
| 9 | +use rusqlite::Connection; |
| 10 | + |
| 11 | +/// True when the crate was built with the `embeddings` feature. |
| 12 | +pub const ENABLED: bool = cfg!(feature = "embeddings"); |
| 13 | + |
| 14 | +/// The text embedded for a symbol: name + signature + docstring. |
| 15 | +pub fn symbol_doc(name: &str, signature: &str, docstring: &str) -> String { |
| 16 | + let mut s = String::with_capacity(name.len() + signature.len() + docstring.len() + 2); |
| 17 | + s.push_str(name); |
| 18 | + if !signature.is_empty() { |
| 19 | + s.push(' '); |
| 20 | + s.push_str(signature); |
| 21 | + } |
| 22 | + if !docstring.is_empty() { |
| 23 | + s.push(' '); |
| 24 | + s.push_str(docstring); |
| 25 | + } |
| 26 | + s |
| 27 | +} |
| 28 | + |
| 29 | +// --------------------------------------------------------------------------- |
| 30 | +// Feature ON: real model2vec-rs + sqlite-vec implementation. |
| 31 | +// --------------------------------------------------------------------------- |
| 32 | +#[cfg(feature = "embeddings")] |
| 33 | +mod imp { |
| 34 | + use super::*; |
| 35 | + use model2vec_rs::model::StaticModel; |
| 36 | + |
| 37 | + /// Register the sqlite-vec extension for every subsequent connection. Must be |
| 38 | + /// called once, before opening any connection that uses the `vec0` table. |
| 39 | + pub fn register_extension() { |
| 40 | + unsafe { |
| 41 | + #[allow(clippy::missing_transmute_annotations)] |
| 42 | + rusqlite::ffi::sqlite3_auto_extension(Some(std::mem::transmute( |
| 43 | + sqlite_vec::sqlite3_vec_init as *const (), |
| 44 | + ))); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Create the KNN table for `dim`-dimensional cosine vectors (idempotent). |
| 49 | + pub fn create_embedding_table(conn: &Connection, dim: usize) -> rusqlite::Result<()> { |
| 50 | + conn.execute_batch(&format!( |
| 51 | + "CREATE VIRTUAL TABLE IF NOT EXISTS embedding_vecs USING vec0( |
| 52 | + symbol_id INTEGER PRIMARY KEY, |
| 53 | + embedding FLOAT[{dim}] distance_metric=cosine |
| 54 | + );" |
| 55 | + )) |
| 56 | + } |
| 57 | + |
| 58 | + /// A loaded static embedding model. |
| 59 | + pub struct Embedder { |
| 60 | + model: StaticModel, |
| 61 | + dim: usize, |
| 62 | + } |
| 63 | + |
| 64 | + impl Embedder { |
| 65 | + /// Load `model_id` (a HuggingFace repo id or local path). Output is |
| 66 | + /// L2-normalised so cosine distance behaves well. |
| 67 | + pub fn load(model_id: &str, dim: usize) -> anyhow::Result<Self> { |
| 68 | + let model = StaticModel::from_pretrained(model_id, None, Some(true), None) |
| 69 | + .map_err(|e| anyhow::anyhow!("load embedding model '{model_id}': {e}"))?; |
| 70 | + Ok(Self { model, dim }) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn dim(&self) -> usize { |
| 74 | + self.dim |
| 75 | + } |
| 76 | + |
| 77 | + pub fn embed_one(&self, text: &str) -> Vec<f32> { |
| 78 | + self.model.encode_single(text) |
| 79 | + } |
| 80 | + |
| 81 | + pub fn embed_batch(&self, texts: &[String]) -> Vec<Vec<f32>> { |
| 82 | + self.model.encode(texts) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + fn vec_to_blob(v: &[f32]) -> Vec<u8> { |
| 87 | + let mut b = Vec::with_capacity(v.len() * 4); |
| 88 | + for f in v { |
| 89 | + b.extend_from_slice(&f.to_le_bytes()); |
| 90 | + } |
| 91 | + b |
| 92 | + } |
| 93 | + |
| 94 | + pub fn store_embedding(conn: &Connection, symbol_id: i64, vec: &[f32]) -> rusqlite::Result<()> { |
| 95 | + conn.execute( |
| 96 | + "INSERT OR REPLACE INTO embedding_vecs(symbol_id, embedding) VALUES (?1, ?2)", |
| 97 | + rusqlite::params![symbol_id, vec_to_blob(vec)], |
| 98 | + )?; |
| 99 | + Ok(()) |
| 100 | + } |
| 101 | + |
| 102 | + /// Embed every symbol that has no embedding yet; returns how many were added. |
| 103 | + pub fn embed_pending(conn: &Connection, embedder: &Embedder) -> rusqlite::Result<usize> { |
| 104 | + let rows: Vec<(i64, String, String, String)> = { |
| 105 | + let mut stmt = conn.prepare( |
| 106 | + "SELECT id, name, signature, docstring FROM symbols \ |
| 107 | + WHERE id NOT IN (SELECT symbol_id FROM embedding_vecs)", |
| 108 | + )?; |
| 109 | + stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)))? |
| 110 | + .collect::<rusqlite::Result<Vec<_>>>()? |
| 111 | + }; |
| 112 | + if rows.is_empty() { |
| 113 | + return Ok(0); |
| 114 | + } |
| 115 | + let docs: Vec<String> = rows |
| 116 | + .iter() |
| 117 | + .map(|(_, n, sig, doc)| symbol_doc(n, sig, doc)) |
| 118 | + .collect(); |
| 119 | + let vecs = embedder.embed_batch(&docs); |
| 120 | + for ((id, ..), v) in rows.iter().zip(vecs.iter()) { |
| 121 | + store_embedding(conn, *id, v)?; |
| 122 | + } |
| 123 | + Ok(rows.len()) |
| 124 | + } |
| 125 | + |
| 126 | + /// Nearest `k` symbol ids to `query` by cosine distance (ascending). |
| 127 | + pub fn knn(conn: &Connection, query: &[f32], k: usize) -> rusqlite::Result<Vec<(i64, f64)>> { |
| 128 | + let mut stmt = conn.prepare( |
| 129 | + "SELECT symbol_id, distance FROM embedding_vecs \ |
| 130 | + WHERE embedding MATCH ?1 AND k = ?2 ORDER BY distance", |
| 131 | + )?; |
| 132 | + let rows = stmt |
| 133 | + .query_map(rusqlite::params![vec_to_blob(query), k as i64], |r| { |
| 134 | + Ok((r.get::<_, i64>(0)?, r.get::<_, f64>(1)?)) |
| 135 | + })? |
| 136 | + .collect::<rusqlite::Result<Vec<_>>>()?; |
| 137 | + Ok(rows) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// --------------------------------------------------------------------------- |
| 142 | +// Feature OFF: identical surface, every operation a no-op. |
| 143 | +// --------------------------------------------------------------------------- |
| 144 | +#[cfg(not(feature = "embeddings"))] |
| 145 | +mod imp { |
| 146 | + use super::*; |
| 147 | + |
| 148 | + pub fn register_extension() {} |
| 149 | + |
| 150 | + pub fn create_embedding_table(_conn: &Connection, _dim: usize) -> rusqlite::Result<()> { |
| 151 | + Ok(()) |
| 152 | + } |
| 153 | + |
| 154 | + /// Stub embedder — `load` always fails, so callers keep `None` and degrade. |
| 155 | + pub struct Embedder; |
| 156 | + |
| 157 | + impl Embedder { |
| 158 | + pub fn load(_model_id: &str, _dim: usize) -> anyhow::Result<Self> { |
| 159 | + anyhow::bail!("embeddings feature not enabled at build time") |
| 160 | + } |
| 161 | + pub fn dim(&self) -> usize { |
| 162 | + 0 |
| 163 | + } |
| 164 | + pub fn embed_one(&self, _text: &str) -> Vec<f32> { |
| 165 | + Vec::new() |
| 166 | + } |
| 167 | + pub fn embed_batch(&self, _texts: &[String]) -> Vec<Vec<f32>> { |
| 168 | + Vec::new() |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + pub fn store_embedding(_c: &Connection, _id: i64, _v: &[f32]) -> rusqlite::Result<()> { |
| 173 | + Ok(()) |
| 174 | + } |
| 175 | + pub fn embed_pending(_c: &Connection, _e: &Embedder) -> rusqlite::Result<usize> { |
| 176 | + Ok(0) |
| 177 | + } |
| 178 | + pub fn knn(_c: &Connection, _q: &[f32], _k: usize) -> rusqlite::Result<Vec<(i64, f64)>> { |
| 179 | + Ok(Vec::new()) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +pub use imp::{ |
| 184 | + Embedder, create_embedding_table, embed_pending, knn, register_extension, store_embedding, |
| 185 | +}; |
| 186 | + |
| 187 | +#[cfg(test)] |
| 188 | +mod tests { |
| 189 | + use super::*; |
| 190 | + |
| 191 | + #[test] |
| 192 | + fn symbol_doc_joins_parts() { |
| 193 | + assert_eq!( |
| 194 | + symbol_doc("run", "fn run()", "does a thing"), |
| 195 | + "run fn run() does a thing" |
| 196 | + ); |
| 197 | + assert_eq!(symbol_doc("run", "", ""), "run"); |
| 198 | + } |
| 199 | + |
| 200 | + #[cfg(feature = "embeddings")] |
| 201 | + #[test] |
| 202 | + fn vec0_knn_with_synthetic_vectors() { |
| 203 | + use rusqlite::Connection; |
| 204 | + register_extension(); |
| 205 | + let conn = Connection::open_in_memory().unwrap(); |
| 206 | + crate::db::schema::init_db(&conn).unwrap(); |
| 207 | + create_embedding_table(&conn, 3).unwrap(); |
| 208 | + |
| 209 | + // Three unit-ish vectors; query is closest to id 2. |
| 210 | + store_embedding(&conn, 1, &[1.0, 0.0, 0.0]).unwrap(); |
| 211 | + store_embedding(&conn, 2, &[0.0, 1.0, 0.0]).unwrap(); |
| 212 | + store_embedding(&conn, 3, &[0.0, 0.0, 1.0]).unwrap(); |
| 213 | + |
| 214 | + let hits = knn(&conn, &[0.1, 0.9, 0.0], 2).unwrap(); |
| 215 | + assert_eq!(hits.len(), 2); |
| 216 | + assert_eq!(hits[0].0, 2, "nearest should be id 2"); |
| 217 | + } |
| 218 | +} |
0 commit comments