Skip to content

Commit 999b779

Browse files
djannotclaude
andauthored
feat(retrieval): eval harness, hybrid search, reranking and Qdrant keyword index (#125)
* feat(retrieval): eval harness, hybrid search, reranking and Qdrant keyword index Measure retrieval quality and act on it: - `doc2vec eval`: recall@k / MRR of a SQLite database against questions with known relevant URLs. Modes: dense (the MCP query), bm25 (in-memory FTS5 over stored chunk text), hybrid (RRF), plus --per-page and --rerank deepinfra, matching the MCP server's settings. - MCP server: optional one chunk per page (ONE_CHUNK_PER_PAGE), hybrid vector + BM25 search (HYBRID_SEARCH) and DeepInfra reranking (RERANKER=deepinfra, default nvidia/Nemotron-3.5-Rerank-8B-BF16 over 40 candidates). All off by default; existing deployments are unchanged. Reranker and keyword failures fall back to the retrieval order. - Qdrant keyword index (`keyword_index: true` per source): chunks also store a `bm25` sparse vector computed by Qdrant (qdrant/bm25, IDF modifier). Existing collections get the vector added (Qdrant 1.18+) and are backfilled from stored text on the next sync, resumably and without failing the sync. The MCP server queries it with HYBRID_SEARCH=true. Fixes: - Qdrant point ids came from the chunk text, so identical chunks on two pages collapsed into one point: one page lost the chunk and both were re-embedded on every sync. Ids now come from page URL + chunk position. Website and S3 sources on Qdrant run one full pass to restore lost chunks; unchanged pages are not re-embedded. - MCP Qdrant vector search moves from `search` (removed in @qdrant/js-client-rest 1.19) to the Query API; client upgraded to 1.19. - Hybrid fusion matched chunks by chunk_id alone, merging the same text on two pages; it now matches on id + URL. Measured on 1,206 support questions over two docs sites (text-embedding-3-large): recall@4 63.8% today, 68.2% one chunk per page, 70.8% hybrid, 79.5% with Nemotron-3.5-Rerank-8B. See RETRIEVAL_ROADMAP.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Denis Jannot <denis.jannot@solo.io> * fix(mcp): override undici to a patched version for the Qdrant client @qdrant/js-client-rest 1.19.0 pins undici 7.29.0, which is affected by three advisories fixed in 7.29.1 (WebSocket permessage-deflate DoS, orphaned RetryHandler body DoS, response splitting via the retry interceptor). Override it to ^7.29.1 (resolves to 7.30.1); the client's vector search, keyword search and scroll were re-checked against Qdrant 1.19.2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Denis Jannot <denis.jannot@solo.io> --------- Signed-off-by: Denis Jannot <denis.jannot@solo.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent afa6945 commit 999b779

19 files changed

Lines changed: 1956 additions & 116 deletions

‎README.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ Configuration is managed through two files:
267267
* `qdrant_url`: (Optional) URL of your Qdrant instance. Defaults to `http://localhost:6333`.
268268
* `qdrant_port`: (Optional) Port for the Qdrant REST API. Defaults to `443` if `qdrant_url` starts with `https`, otherwise `6333`.
269269
* `collection_name`: (Optional) Name of the Qdrant collection to use. Defaults to `<product_name>_<version>` (lowercased, spaces replaced with underscores).
270+
* `keyword_index`: (Optional) `true` to also store a BM25 keyword vector per chunk, so the MCP server can run hybrid search (`HYBRID_SEARCH=true`). Needs Qdrant 1.18 or later. See [Qdrant keyword index](#qdrant-keyword-index).
270271
271272
Optional embedding configuration:
272273
* `embedding.provider`: Provider for embeddings (`openai` or `azure`).
@@ -485,6 +486,34 @@ The script will then:
485486
7. Cleanup obsolete chunks.
486487
8. Output detailed logs.
487488
489+
### Evaluating retrieval quality
490+
491+
`doc2vec eval` measures how well a SQLite database answers real questions, so a change to the embedding model, chunking or ranking can be judged by numbers. Each case is a question plus the URLs that should come back; the command embeds the question with the same model as the database, runs the same nearest-neighbour query as the MCP server, and reports recall@1/4/10 (share of questions with a relevant chunk in the top k) and MRR (mean reciprocal rank of the first relevant chunk).
492+
493+
```bash
494+
doc2vec eval cases.jsonl --db ./istio.db --config config.yaml --top-k 10 --out report.json
495+
```
496+
497+
Cases can be `.jsonl` (one per line), `.json` (an array) or `.yaml`:
498+
499+
```json
500+
{"id": "mtls-1", "query": "how do I enable strict mTLS for one namespace?", "relevant_urls": ["https://istio.io/latest/docs/tasks/security/authentication/mtls-migration/"], "product_name": "istio", "version": "latest"}
501+
```
502+
503+
`db`, `product_name` and `version` are optional per case. URLs match ignoring `#fragments`, trailing slashes and case. `--config` reads only the `embedding` section; without it the `OPENAI_*` / `AZURE_OPENAI_*` env vars are used.
504+
505+
Search methods can be compared on the same database without re-ingesting it:
506+
507+
- `--mode dense` (default): nearest chunks by embedding, the MCP server's query.
508+
- `--mode bm25`: keyword ranking with SQLite FTS5, built in memory from the stored chunk text.
509+
- `--mode hybrid`: dense and BM25 lists fused with Reciprocal Rank Fusion.
510+
- `--per-page`: keep only the best chunk of each page, so the top results cover more pages.
511+
- `--rerank deepinfra`: rerank the top `--rerank-candidates` chunks (default 40) with `--rerank-model` (default `nvidia/Nemotron-3.5-Rerank-8B-BF16`); needs `DEEPINFRA_API_KEY`.
512+
513+
These match the MCP server's retrieval settings (`HYBRID_SEARCH`, `ONE_CHUNK_PER_PAGE`, `RERANKER`; see `mcp/README.md`), so a configuration can be measured before it is deployed.
514+
515+
Results of comparing embedding models, search methods and rerankers are in [RETRIEVAL_ROADMAP.md](RETRIEVAL_ROADMAP.md).
516+
488517
## Docker Images and the Browser
489518
490519
Website sources are rendered with a real browser (Puppeteer). The `Dockerfile` builds two images so you can choose where that browser runs:
@@ -675,6 +704,21 @@ Server-sent events: `GET /api/events` (run and config updates) and `GET /api/run
675704
* Uses `@qdrant/js-client-rest`.
676705
* Requires `qdrant_url`, `qdrant_port`, `collection_name` and potentially `QDRANT_API_KEY`.
677706
707+
#### Qdrant point ids
708+
709+
Each chunk is stored as one point whose id comes from its page URL and its position on the page. Earlier versions derived the id from the chunk text, so the same text on two pages (a shared "Before you begin" section, for example) became a single point: one page lost the chunk, and both pages looked changed and were re-embedded on every sync.
710+
711+
After upgrading, the first sync of each **website** or **S3** source on Qdrant processes every page once (bypassing the lastmod/ETag or timestamp shortcuts) and rewrites only the pages whose chunks were merged; unchanged pages are not re-embedded. Local directories compare every file on each sync and repair themselves on the first run. GitHub issues, Zendesk and code chunks carry the issue, ticket or file in their text, so merges there are rare; any affected item is repaired the next time it changes. SQLite was never affected.
712+
713+
#### Qdrant keyword index
714+
715+
With `keyword_index: true`, each chunk is stored with two vectors: the embedding (the collection's unnamed vector, as before) and a sparse `bm25` vector that Qdrant computes from the chunk text with its built-in `qdrant/bm25` model (IDF applied at query time). The MCP server queries both when `HYBRID_SEARCH=true`.
716+
717+
* **New collections** are created with the `bm25` sparse vector.
718+
* **Existing collections** get it added on the next sync (Qdrant 1.18+ vector schema update), and every chunk stored before then is backfilled from its stored text. Embeddings are not recomputed. The backfill only reads chunks that lack the vector, so it resumes if interrupted and costs one empty scroll once done.
719+
* On a Qdrant older than 1.18 the sync logs a warning and stores chunks without the keyword vector.
720+
* Turning the option off later stops writing the vector; the existing ones stay unused.
721+
678722
## PDF Processing
679723

680724
Doc2Vec includes built-in support for processing PDF files in both local directories and websites. PDF files are automatically detected by their `.pdf` extension and processed using [Mozilla's PDF.js](https://github.com/mozilla/pdf.js) library.

‎RETRIEVAL_ROADMAP.md‎

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# Retrieval Roadmap
2+
3+
Ideas for improving retrieval quality, ranked by expected gain per effort, and
4+
the baseline they are measured against. Measure every change with
5+
`doc2vec eval` (see README, "Evaluating retrieval quality") before adopting it.
6+
7+
## Baseline (2026-10-21)
8+
9+
Eval set built from real support-assistant conversations: each case is either
10+
the user's first question or one of the assistant's own search queries,
11+
labelled with the docs pages the assistant cited in its answer (silver labels:
12+
a cited page is relevant, an uncited one is not necessarily irrelevant). The
13+
eval cases themselves are private and not part of the repository; only
14+
aggregate scores are reported here.
15+
16+
Databases: `solo-enterprise-for-kgateway` 2.3.x docs (339 pages, 2,649 chunks)
17+
and `solo-enterprise-for-agentgateway` kubernetes/latest docs (391 pages, 3,113
18+
chunks); `text-embedding-3-large`, 3072 dims, sqlite-vec, same query as the MCP
19+
server. Agentgateway labels cited under the old `/latest/` paths were mapped
20+
through their 301 redirects.
21+
22+
| Docs | Case set | Cases | recall@1 | recall@4 | recall@10 | MRR |
23+
|---|---|---|---|---|---|---|
24+
| kgateway | User questions | 84 | 41.7% | 59.5% | 72.6% | 0.506 |
25+
| kgateway | Agent search queries | 443 | 45.8% | 69.3% | 79.9% | 0.567 |
26+
| agentgateway | User questions | 116 | 42.2% | 62.9% | 74.1% | 0.526 |
27+
| agentgateway | Agent search queries | 563 | 42.3% | 63.6% | 74.8% | 0.526 |
28+
29+
Where the first relevant chunk lands (top-50 retrieval):
30+
31+
| Docs | Case set | rank 1 | 2–4 | 5–10 | 11–50 | >50 |
32+
|---|---|---|---|---|---|---|
33+
| kgateway | User questions | 42% | 18% | 13% | 17% | 11% |
34+
| kgateway | Agent search queries | 46% | 23% | 11% | 13% | 7% |
35+
| agentgateway | User questions | 42% | 21% | 11% | 14% | 12% |
36+
| agentgateway | Agent search queries | 42% | 21% | 11% | 16% | 9% |
37+
38+
About a quarter of cases have the right page in the top 50 but not the top 4,
39+
which is what a reranker recovers. The top 4 chunks also hold on average 1.2
40+
chunks from a page already shown.
41+
42+
## Comparison matrix (2026-10-21)
43+
44+
Same 1,206 cases, scores averaged over the four case sets. Each embedding model
45+
re-embedded the same stored chunks (queries and documents marked as such where
46+
the model supports it). Hybrid = dense + SQLite FTS5 BM25 fused with RRF (k=60).
47+
48+
Recall@4 by embedding model and search method:
49+
50+
| Embedding | Vector | Vector, 1 chunk/page | Hybrid | Hybrid, 1 chunk/page |
51+
|---|---|---|---|---|
52+
| Nemotron-3.5-Embed-8B (DeepInfra) | 70.4 | **77.3** | 68.0 | 75.2 |
53+
| Gemini embedding-2 | 67.0 | 74.0 | 68.8 | 75.8 |
54+
| Qwen3-Embedding-8B (DeepInfra) | 66.5 | 70.2 | 66.9 | 74.3 |
55+
| Gemini embedding-001 | 68.2 | 74.1 | 67.3 | 73.8 |
56+
| Cohere embed-v4 (Bedrock) | 64.3 | 71.9 | 65.6 | 72.0 |
57+
| OpenAI text-embedding-3-large (today) | 63.8 | 68.2 | 64.8 | 70.8 |
58+
| OpenAI text-embedding-3-large @1024 | 64.0 | 69.5 | 64.2 | 70.4 |
59+
| Mistral embed | 60.8 | 65.3 | 64.2 | 69.8 |
60+
| OpenAI text-embedding-3-small | 55.8 | 63.8 | 61.9 | 67.9 |
61+
62+
Keyword search alone (BM25): 53.0, or 57.6 with one chunk per page.
63+
64+
Rerankers on the top 40 hybrid chunks, then one chunk per page:
65+
66+
| Reranker | On text-embedding-3-large: @1 / @4 / MRR@10 | On Nemotron-3.5-Embed-8B: @1 / @4 / MRR@10 | Latency p50 / p95 (one at a time) |
67+
|---|---|---|---|
68+
| No reranker (hybrid, 1 chunk/page) | 42.5 / 70.8 / 0.560 | 42.5 / 75.2 / 0.575 | — |
69+
| **Nemotron-3.5-Rerank-8B** | **52.7 / 79.5 / 0.655** | **52.1 / 82.8 / 0.662** | 1.5 s / 2.8 s |
70+
| Nemotron rerank VL 1B (current chatbot) | 44.5 / 74.6 / 0.588 | 43.5 / 76.6 / 0.589 | 0.86 s / 2.1 s |
71+
| Qwen3-Reranker-4B | 44.5 / 76.0 / 0.588 | 44.0 / 76.4 / 0.593 | 2.3 s / 12.9 s |
72+
| Qwen3-Reranker-8B | 40.1 / 72.7 / 0.557 | 38.7 / 74.2 / 0.553 | 2.1 s / 4.9 s |
73+
| Cohere Rerank 3.5 (Bedrock) | 40.9 / 72.4 / 0.560 | 40.4 / 74.7 / 0.564 | 1.6 s / 2.2 s |
74+
| Claude Haiku, listwise (user questions only) | — | 41.6 / 75.7 / 0.570 | 2.0 s / 2.6 s |
75+
76+
Today (text-embedding-3-large, vector search, no reranker): 43.0 / 63.8 / 0.532.
77+
78+
On today's vector-only candidates (top 40 of text-embedding-3-large vector search), recall@1 / @4 / MRR@10:
79+
80+
| Reranker | Chunks as reranked | Then 1 chunk/page |
81+
|---|---|---|
82+
| No reranker | 43.0 / 63.8 / 0.532 | 43.0 / 68.2 / 0.556 |
83+
| Nemotron rerank VL 1B (current chatbot) | 43.4 / 65.9 / 0.551 | 43.4 / 73.5 / 0.578 |
84+
| Nemotron-3.5-Rerank-8B | 52.5 / 73.8 / 0.624 | 52.5 / 78.5 / 0.647 |
85+
86+
The VL 1B reranker on its own adds about +0.4 recall@1 and +2 recall@4; most of
87+
its apparent gain comes from one chunk per page.
88+
89+
### MCP server implementation (measured)
90+
91+
`mcp/src/server.ts` now runs vector search → keyword search fused by RRF
92+
(SQLite, `HYBRID_SEARCH`) → URL filters → reranking (`RERANKER=deepinfra`) →
93+
one chunk per page (`ONE_CHUNK_PER_PAGE`). Its built code, called through
94+
`queryDocumentation` with `limit: 4` on the same cases, scored:
95+
96+
| Configuration | recall@1 | recall@4 |
97+
|---|---|---|
98+
| Vector only (previous behaviour) | 43.0 | 63.8 |
99+
| One chunk per page | 43.0 | 68.2 |
100+
| Hybrid + one chunk per page | 42.5 | 70.8 |
101+
| Hybrid + Nemotron-3.5-Rerank-8B + one chunk per page | 52.7 | 79.5 |
102+
103+
These match `doc2vec eval --mode hybrid --per-page --rerank deepinfra`
104+
(identical on three sets, one case apart on the fourth, within reranker
105+
run-to-run variation).
106+
107+
### Cost: text-embedding-3-large vs Nemotron-3.5-Embed-8B
108+
109+
| | text-embedding-3-large (OpenAI) | Nemotron-3.5-Embed-8B (DeepInfra) |
110+
|---|---|---|
111+
| Price per 1M tokens | $0.13 ($0.065 via Batch API) | $0.035 |
112+
| Re-embed the two eval sites (5,762 chunks, ~2.4M tokens) | ~$0.31 | ~$0.08 |
113+
| Per 1M chunks (~415 tokens each) | ~$54 | ~$15 |
114+
| Per 1M queries (~20 tokens each) | ~$2.60 | ~$0.70 |
115+
| Vector size (float32) | 3072 dims, 12 KB per chunk | 4096 dims, 16 KB per chunk (+33%) |
116+
| SQLite file, kgateway / agentgateway | 46 MB / 60 MB | 58 MB / 76 MB |
117+
118+
Nemotron supports Matryoshka truncation (1024, 768, 512 dims, re-normalized),
119+
which would make vectors smaller than today's; quality at lower dims is not
120+
yet measured. Token counts are estimated at 4 characters per token. Reranking
121+
40 chunks is about 17k tokens per query: ~$0.85 per 1,000 queries with
122+
Nemotron-3.5-Rerank-8B ($0.05/M), ~$0.17 with VL 1B ($0.01/M). Using Nemotron
123+
embeddings in production also needs `input_type` (`query` / `passage`) sent by
124+
ingestion and by the MCP server; the measured scores used it, and doc2vec does
125+
not send it today.
126+
127+
Findings:
128+
- One chunk per page is the largest free gain: +4 to +8 points of recall@4 for every model.
129+
- Nemotron-3.5-Embed-8B and Gemini embedding-2 beat text-embedding-3-large by 5–9 points with the same search method; text-embedding-3-small and Mistral embed are the weakest.
130+
- Cutting text-embedding-3-large to 1024 dims costs nothing measurable (3x smaller vectors).
131+
- Hybrid helps weaker embeddings most; with the strongest one, vector + one chunk per page is as good or better.
132+
- Only Nemotron-3.5-Rerank-8B clearly beats the best first stage without a reranker. Most rerankers lower recall@1 below today's.
133+
- Swapping the chatbot's reranker for Nemotron-3.5-Rerank-8B gains about +5 points of recall@4 and +8 of recall@1 on today's embeddings, for about 0.6 s more median latency.
134+
- The Qwen3 retrieval instruction changes nothing on DeepInfra (it likely applies a default).
135+
- Latency under 6 parallel requests reached 5–7 s median for the 8B models on DeepInfra: plan capacity or a dedicated deployment.
136+
137+
## Ideas
138+
139+
1. ~~One chunk per page in the MCP query tools.~~ Done (`ONE_CHUNK_PER_PAGE=true`, opt-in).
140+
2. ~~Nemotron-3.5-Rerank-8B in the MCP server.~~ Done (`RERANKER=deepinfra`, opt-in).
141+
Still to do: the same reranker in the support chatbot.
142+
3. ~~Hybrid search for Qdrant.~~ Done: sources with `keyword_index: true`
143+
store a `bm25` sparse vector computed by Qdrant (`qdrant/bm25`), existing
144+
collections are extended and backfilled on the next sync (Qdrant 1.18+),
145+
and the MCP server queries it with `HYBRID_SEARCH=true`. SQLite builds its
146+
keyword index in memory at query time. Qdrant's BM25 stems English and drops
147+
stopwords where SQLite FTS5 does neither, so their scores can differ.
148+
4. **Record the embedding model and dimension in each database** and check it
149+
at query time; prerequisite for switching models safely.
150+
5. **Switch embeddings to Nemotron-3.5-Embed-8B or Gemini embedding-2**,
151+
with query/document input types. Needs (4), provider support in ingestion
152+
and the MCP server, and a full re-embed.
153+
6. **Lower dimensions** if staying on text-embedding-3-large: 1024 dims
154+
scored the same as 3072.
155+
7. **Contextual chunk enrichment** (LLM-written context per chunk, or a
156+
context-aware embedding model such as voyage-context-3).
157+
8. **Batch embedding requests** during ingestion (currently one chunk per
158+
call) — speed, not quality, but it makes a re-embed for (5) cheaper.
159+
9. **Multi-vector / late interaction (ColBERT, ColPali)** — last; storage cost
160+
is high and sqlite-vec has no MaxSim. ColPali only if PDFs with diagrams
161+
matter.
162+
10. **Not tested (no keys):** Voyage embeddings and rerankers, Zerank-2, Cohere
163+
Rerank 4 Pro, Jina.
164+
165+
## Found while building the eval
166+
167+
- **Fixed.** Chunks with identical text on different pages collapsed into one Qdrant
168+
point: the point id was derived from `chunk_id`, a hash of the chunk text
169+
(breadcrumb included), so the last page written won. Loading the eval sites
170+
into Qdrant kept 2,303 of 2,649 kgateway chunks and 2,949 of 3,113
171+
agentgateway chunks; SQLite keeps them all. Both pages then looked changed
172+
on every sync: with the old ids, 108 of 339 kgateway pages were re-embedded
173+
on every pass. Point ids now come from the page URL and chunk position; a
174+
one-time pass on website and S3 sources restores the lost chunks (measured
175+
on a local Qdrant: the first sync rewrote 55 pages and restored all 2,649
176+
chunks, later syncs rewrote nothing).
177+
- **Fixed.** `@qdrant/js-client-rest` 1.19 removed `search`, and the MCP
178+
server's `^1.16.2` range allowed 1.19 on a fresh install. Its vector search
179+
now uses the Query API and the client is on 1.19 (1.16 also fails on
180+
Node 26).

0 commit comments

Comments
 (0)