Skip to content

Retrieval Pipeline ​

Knowledge-base retrieval is a rescue-style fusion of a lexical leg (FTS5 BM25) and a semantic leg (local vectors). Design principle: when the two legs differ greatly in precision, a gate lets the strong signal lead and the weak signal rescue only when needed, rather than an equal-weight blend.

Pipeline overview ​

Chunking: structure-aware ​

Imported documents are split by structure (heading hierarchy, paragraphs), not a fixed-size sliding window. Each chunk keeps a heading (its section title) and text (body) — this split is the prerequisite for heading weighting below. Chunk ids come from a counter (nextChunkId); deleting a document removes its chunks and vectors with it.

The private library is whole-document replacement: import deletes old chunks and rebuilds, so the index always holds only the current version. The shared library has a version table but indexes only the current version.

Lexical leg: FTS5 dual column + 10× heading weight ​

kb_fts is an FTS5 virtual table split into heading / text columns:

sql
bm25(kb_fts, 0.0, 10.0, 1.0)

A heading hit weighs 10×, beating chunks that merely repeat a word frequently in the body. Heading weighting is the single largest source of retrieval quality; heading-set recall@5 reaches 81%.

Bilingual tokenizer ​

SQLite's built-in unicode61 does not segment CJK. The retrieval layer supplies a custom tokenizer:

  • ASCII words: kept when length ≥ 2 and not a stopword;
  • CJK: both single characters and adjacent bigrams are collected, so a query hits by whole word and by sub-word alike;
  • the same tokenization feeds indexing and querying, keeping both sides consistent.

Query expansion and construction ​

  • A built-in SYNONYMS table expands the query (expandQuery); disable with IFG_QUERY_EXPANSION=0;
  • expanded tokens are deduped and capped at 24, joined into a quoted-OR FTS5 MATCH expression (injection- and syntax-safe);
  • the lexical leg takes LIMIT 80 candidates; BM25 score is max(0, -rank).

The semantic leg: a rescue signal ​

The vector leg only participates when the lexical leg is untrustworthy:

  • Compute the query tokens' coverage in the best lexical hit;
  • Coverage ≥ 0.7 → pure lexical, the vector leg doesn't run;
  • Coverage < 0.7 and embeddings are a real model (onnx / command) → the vector leg joins RRF fusion.

Hash-fallback embeddings never participate in fusion — they are essentially lexical/character features and add no signal on semantic rewrites.

The vector scan is a bounded top-200 compact scan: no full-table sort, no per-chunk object allocation; candidates are hydrated only for the union of both legs. A single retrieval is ~10 ms (14k chunks).

Fusion: weighted RRF, not an equal blend ​

When both legs participate, fusion is weighted Reciprocal Rank Fusion (fuseHybrid):

text
score(chunk) = 0.6 / (60 + rank_fts) + 0.4 / (60 + rank_vec)
  • k = 60: the RRF smoothing constant, damping the absolute advantage of top ranks;
  • lexical weight 0.6 > vector 0.4: the lexical leg is more precise and leads ranking;
  • chunks with lexical BM25 ≥ 0.5 get an extra +0.01, pushing exact heading hits to the front;
  • multiple chunks from one document decay by ×0.88^count, so a long document cannot flood the results;
  • fused scores are normalized to the top score, then Top-K is taken (cap 20).

Candidate volume is bounded: lexical ≤ 80 + vector ≤ 200. Private and shared libraries are retrieved separately and merged by permission.

Pooling: decided by model metadata ​

The bge family is trained with CLS pooling. The pooling method is decided by metadata written at install/activate (ifg-embed.json); the path heuristic is only a fallback for old installs — ensuring each model uses the pooling it was trained with, so vector semantics are correct. Model install, inference and background rebuild are covered in Embedding & Vector Index. On the same corpus with correct pooling:

Eval setrecall@5MRR
Heading set88.3%—
Rewrite set33.3%0.167

Index fingerprint ​

Index staleness is judged by a fingerprint (engine + width + pooling). At the same width but a different pooling or engine, old vectors and new queries are incomparable and must be rebuilt. How rebuild avoids blocking the event loop is in Background Reindex.

Benchmark set: generated by the product itself ​

POST /api/v1/kb/eval/golden auto-extracts 60 heading self-retrieval queries from the current corpus (unique headings, reasonable length, excluding page-number/TOC/trailing-punctuation noise), scores and stores them — all local.

  • Query = section heading, target = that section itself → measures the lexical leg's ranking quality.
  • Heavily duplicated headings are excluded to avoid a lottery.
  • The semantic leg is measured with a hand-written rewrite-question annotation set — humans write the targets to reflect real intent.

Extraction constraints: 60 total, ≤ 20 per collection, chunk body ≥ 200 chars, unique headings, leaf heading length 4–30 chars.

Metric definitions ​

MetricFormula
recall@Krelevant chunks found / total relevant chunks (topK=5)
MRRmean of 1 / (first relevant rank + 1) per query
hitRateshare of queries with at least one relevant hit

The benchmark depends on no fixed in-repo data; rerun it after changing the corpus. For design rationale and model choice see Decisions & Benchmarks.

Local-first · inference egresses on demand · fully traceable