flexvec: SQL Vector Retrieval with Programmatic Embedding Modulation
Organizations: Independent Researcher, Vancouver, BC
Abstract
As AI agents become the primary consumers of retrieval APIs, there is an opportunity to expose more of the retrieval pipeline to the caller. flexvec is a retrieval kernel that exposes the embedding matrix and score array as a programmable surface, allowing arithmetic operations on both before selection. We refer to composing operations on this surface at query time as Programmatic Embedding Modulation (PEM). This paper describes a set of such operations and integrates them into a SQL interface via a query materializer that facilitates composable query primitives. On a production corpus of 240,000 chunks, three composed modulations execute in 19 ms end-to-end on a desktop CPU without approximate indexing. At one million chunks, the same operations execute in 82 ms.
Figures & tables
| Token | Operation | Code | Cost | Ref |
|---|---|---|---|---|
| suppress:X | Subtract similarity to suppression direction | scores -= w * (M @ embed(X)) | 1 matmul | — |
| decay:N | Reciprocal decay with -day half-life | scores *= 1/(1 + days/N) | elementwise | — |
| centroid:ids | Shift query toward mean of examples | q = ·q + (1- )·mean(E[ids]); q /= q | 1 mean | [ 1 ] |
| from:A to:B | Blend directional similarity | scores = 0.5·sim + 0.5·(M @ (embed(B)-embed(A))) | 1 matmul | [ 2 ] |
| diverse | MMR iterative selection ( =0.7) | score = ·rel - (1- )·max_sim | pairwise | [ 3 ] |
| Operation | Time | Scope |
|---|---|---|
| Matrix multiply (240K 128) | 5ms | Phase 2 only |
| Scoring + 3 modulations + MMR | 12ms | Phase 2 only |
| Full pipeline (pre-filter + scoring + compose) | 19ms | All phases |
| FTS5 keyword search | 18ms | — |
| Hybrid (keyword JOIN vec_ops) | 63ms | All phases |
| Pre-filter | Candidates | 3 mods + MMR |
|---|---|---|
| Full corpus | 240,000 | 12ms |
| Non-tool, last 30 days | 61,000 | 12ms |
| Non-tool, last 7 days | 11,000 | 0.6ms |
| Non-tool, last 24 hours | 3,000 | 0.4ms |
| One project, 30 days | 37,000 | 5ms |
| Corpus size | Base matmul | Full pipeline | Memory |
|---|---|---|---|
| 250K | 5ms | 19ms | 122MB |
| 500K | 7ms | 37ms | 244MB |
| 750K | 15ms | 73ms | 366MB |
| 1M | 17ms | 82ms | 514MB |
| Modulation | Effect | nDCG@10 retention |
|---|---|---|
| diverse | ILS reduction 10–40% across all corpora | See Table 6 |
| suppress:X | RBO vs baseline 0.19–0.41 | — |
| decay:7 | Results 42–50 days more recent on average | — |
| centroid:ids | Centroid similarity +0.05 to +0.12 | — |
| from:A to:B | RBO vs baseline 0.08–0.25 | — |
| Corpus | Baseline nDCG@10 | diverse nDCG@10 | Retention | ILS reduction |
|---|---|---|---|---|
| SciFact | 0.60 | 0.56 | 93% | 12% |
| FiQA | 0.41 | 0.33 | 80% | 28% |
| SCIDOCS | 0.18 | 0.15 | 83% | 18% |
| NFCorpus | 0.13 | 0.08 | 59% | 40% |
| Baseline | Suppressed | |
|---|---|---|
| 1 | UI style iteration 0.70 | Identity layer architecture 0.18 |
| 2 | Marketing tagline draft 0.67 | Server lifecycle debugging 0.14 |
| 3 | Documentation site structure 0.65 | Background worker failure analysis 0.07 |
| 4 | Product positioning discussion 0.65 | Rendering pipeline implementation 0.05 |
| 5 | Community post draft 0.65 | Platform detection and branching logic 0.03 |
Dense Retrieval
Dense retrieval uses learned vector representations to find relevant documents, passages, products, or tools by semantic similarity.
See all 152 papers