Navigable HNSW vector index, int8 scalar quantization, stemmed Okapi BM25 full-text search, Reciprocal Rank Fusion, and SQLite persistence.
Pure-Veko embedded multi-layer HNSW vector store, symmetric int8 scalar quantization, Okapi BM25 full-text search, Reciprocal Rank Fusion (RRF) hybrid RAG, and Zero-FFI SQLite 3 persistence.
vec_rag provides an in-process Retrieval-Augmented Generation (RAG) and hybrid search engine written entirely in Veko with zero external dependencies or native FFI requirements:
vec_rag/chunker: Semantic prose (chunk-text), Markdown ATX heading-aware (chunk-markdown), and declaration-aware source code (chunk-code) splitters with configurable max_chars and sliding overlap_chars, preserving hierarchical heading breadcrumbs and exact byte offsets (start_off, end_off).vec_rag/bm25: Alphanumeric tokenizer, deterministic English suffix stemmer-lite, stopword filter, and Okapi BM25 (k1 = 1.5, b = 0.75) inverted index (new-index, add-doc, remove-doc, search-bm25).vec_rag: Dense block[f64] vector math (dot-product, cosine-similarity, l2-distance-sq, normalize-vec), symmetric int8 scalar quantization (quantize-i8, dequantize-i8, dot-i8) cutting embedding memory by 4x-8x, multi-layer HNSW navigable small-world graph (new-store, upsert, delete-doc, search-vector with metadata filtering), RRF k = 60 hybrid ranking (search-hybrid), and transactional JSON / Zero-FFI db/sqlite persistence (save-sqlite, load-sqlite).veko pkg add vec_rag@1.0.0Import the unified package and submodules:
use [
rag = vec_rag
bm25 = vec_rag/bm25
chunker = vec_rag/chunker
]0..4. Queries greedily descend upper layers (max_level .. 1) in $O(\log N)$ hops before running a bounded ef_search beam expansion at layer 0, with bidirectional degree pruning (M on upper layers, 2M on layer 0).int8 Quantization: quantize-i8 maps normalized f64 coordinates into compact 1-byte signed int8 codes ([-127..127]) stored inside bytes, enabling integer-only inner-loop dot products (dot-i8) scaled once on exit.search-hybrid executes semantic HNSW search and lexical Okapi BM25 search over the same document store, fusing rankings via $\text{RRF}(d) = \frac{1}{k + r_{\text{vec}}(d)} + \frac{1}{k + r_{\text{bm25}}(d)}$ (k = 60) so exact error codes, identifiers, and conceptual paraphrases rank together without score calibration drift.save-sqlite and load-sqlite persist embeddings, quantized int8 BLOBs, and JSON metadata inside standard SQLite format 3 B-tree files via db/sqlite, running identically across VM, Wasm 3.0, and Native targets.vec_rag/chunker| Type / Signature | Description |
|---|---|
Chunk = struct { doc_id str, chunk_index i64, heading str = '', text str, start_off i64, end_off i64 } | Semantic document chunk with section heading breadcrumb and byte span offsets. |
chunk-text = fn [doc_id str, text str, max_chars i64, overlap_chars i64 /heading heading str = ''] block[Chunk] | Splits UTF-8 prose on paragraph, line, sentence, or word boundaries with sliding overlap. |
chunk-markdown = fn [doc_id str, md_text str, max_chars i64, overlap_chars i64] block[Chunk] | Splits Markdown along #..###### ATX headings (ignoring # inside code fences) with hierarchical breadcrumbs. |
chunk-code = fn [doc_id str, src_text str, max_chars i64, overlap_chars i64] block[Chunk] | Splits source files along top-level function/type declarations. |
vec_rag/bm25| Type / Signature | Description |
|---|---|
Bm25Hit = struct { doc_id str, score f64 } | Ranked lexical hit from Okapi BM25 search. |
Bm25Index | Mutable Okapi BM25 inverted index (k1 = 1.5, b = 0.75). |
stem-word = fn [word str] str | Deterministic English suffix stemmer-lite. |
tokenize = fn [text str] block[str] | Lowercases, filters English stopwords, and stems alphanumeric tokens. |
new-index = fn [/k1 k1 f64 = 1.5 /b b f64 = 0.75] Bm25Index | Creates an empty Okapi BM25 inverted index. |
add-doc = fn [mut idx Bm25Index, doc_id str, text str] enum | Indexes or updates doc_id in idx. |
remove-doc = fn [mut idx Bm25Index, doc_id str] bool | Removes doc_id and its term postings from idx. |
search-bm25 = fn [idx Bm25Index, query str, top_k i64] block[Bm25Hit] | Returns top top_k documents ranked by Okapi BM25 score. |
vec_rag| Type / Signature | Description |
|---|---|
QuantizedVec = struct { scale f64, dims i64, codes bytes } | Compact 1-byte-per-dimension symmetric int8 vector representation. |
SearchHit = struct { id str, text str, score f64, vec_score f64, bm25_score f64, metadata map } | Unified result returned by search-vector and search-hybrid. |
VecStore | Multi-layer HNSW vector graph paired with an Okapi BM25 index. |
dot-product = fn [a block[f64], b block[f64]] f64 | Exact inner product of two equal-length vectors. |
l2-distance-sq = fn [a block[f64], b block[f64]] f64 | Squared Euclidean distance between a and b. |
normalize-vec = fn [v block[f64]] block[f64] | Returns a unit L2-normalized vector. |
cosine-similarity = fn [a block[f64], b block[f64]] f64 | Cosine similarity in [-1.0, 1.0]. |
quantize-i8 = fn [v block[f64]] QuantizedVec | Quantizes block[f64] into int8 codes + scale. |
dequantize-i8 = fn [q QuantizedVec] block[f64] | Reconstructs block[f64] from QuantizedVec. |
dot-i8 = fn [a QuantizedVec, b QuantizedVec] f64 | Integer-accumulated inner product over quantized int8 vectors. |
new-store = fn [dims i64 /m m_links i64 = 8 /ef ef_search i64 = 32] VecStore | Creates a new HNSW + BM25 hybrid store. |
upsert = fn [mut store VecStore, doc_id str, text str, vec block[f64] /meta metadata map = {}] enum | Inserts or updates a document in both HNSW and BM25 indexes. |
delete-doc = fn [mut store VecStore, doc_id str] bool | Deletes doc_id from HNSW layers and BM25 postings. |
search-vector = fn [store VecStore, query_vec block[f64], top_k i64 /filter filter_meta map = {} /quantized use_i8 bool = false] block[SearchHit] | Executes multi-layer HNSW nearest-neighbor search with optional metadata predicates. |
search-hybrid = fn [store VecStore, query_text str, query_vec block[f64], top_k i64 /filter filter_meta map = {} /rrf_k rrf_k i64 = 60] block[SearchHit] | Fuses HNSW vector search and Okapi BM25 via Reciprocal Rank Fusion. |
to-json = fn [store VecStore] str / from-json = fn [raw str] VecStore | Portable JSON serialization and index reconstruction. |
save-sqlite = fn [store VecStore, db_path str] enum / load-sqlite = fn [db_path str] VecStore | Persists and reloads VecStore using Zero-FFI db/sqlite. |
use [rag = vec_rag, chunker = vec_rag/chunker]
chunks = chunker/chunk-markdown 'guide.md' '# Networking\nTLS 1.3 uses X25519MLKEM768.\n' 256 32
mut store = rag/new-store 4
_ = rag/upsert/meta mut store 'c-0' chunks[0].text [0.9 0.1 0.0 0.2] { section = chunks[0].heading }
_ = rag/upsert/meta mut store 'c-1' 'SQLite B-tree pages store relational rows.' [0.0 0.9 0.3 0.0] { section = 'Storage' }
hits = rag/search-hybrid store 'post-quantum X25519MLKEM768' [0.85 0.15 0.0 0.1] 2
println `Top hit: {hits[0].id} (RRF={hits[0].score})`veko check --strict-types packages/vec_rag/src/chunker.vk
veko check --strict-types packages/vec_rag/src/bm25.vk
veko check packages/vec_rag/src/mod.vk
veko check packages/vec_rag/examples/demo.vk
veko run -C all packages/vec_rag/examples/demo.vkhttps://veko-lang.org/pkg/vec_rag/1.0.0.vkp5f7f86231902c4dfc7ddd21cafc9717106415fb054b9c42d9f32ab32057c7dda7b6865efb31c5bb1c20c4cfa98b628087e04d7bf927fd2c28babded210ba395ded25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70{"name":"vec_rag","version":"1.0.0","tier":3.0,"tier_label":"Tier 3: AI, Vector RAG & DataFrames","category":"Embedded Vector & Hybrid RAG","description":"Navigable HNSW vector index, int8 scalar quantization, stemmed Okapi BM25 full-text search, Reciprocal Rank Fusion, and SQLite persistence.","modules":["vec_rag/bm25","vec_rag/chunker","vec_rag"],"homepage":"https://veko-lang.org/packages/vec_rag"}| Bundle Entry Path | Size | Leaf SHA-256 Digest |
|---|---|---|
| README.md | 7 KB | 3395a444bee15ad72b628e968b30f86559c2a41412f5d226283358b34e08a108 |
| bm25.vk | 8 KB | b681c96e37d8ca493d900d8a86cbcf305aaf93f541c59cf4da0dc2b2ce34674e |
| chunker.vk | 9 KB | 5974ea6f1055965e12ac533f69db8d6a00f79803c7f117e39d1711fc183812d9 |
| examples/demo.vk | 8 KB | 1b2f5e1cc7b1c6df33e23e4ebaed2320eb2eabf1a0bae8686a2d0d287cbc57e8 |
| mod.vk | 23 KB | bb125238b50d51b3c7a99cc3e037168a660224432d5e5cfbabd7840c91648e6f |