RAG & Retrieval

Second Opinion

Searching is done in two steps. A fast searcher skims all 60 pages and grabs 20 that look about right. Then a careful judge, a second real AI, reads each of those 20 together with your question and re-sorts them. Fast-then-careful is how big search engines find the right page.

I need two brains for this: a fast skimmer and a careful judge. Press Wake up both models (about 46 MB, downloaded once).

1. Fast skimmer: 60 pages → top 20 · -
2. Careful judge: re-sorts the 20 · -

Wake up the models, then ask a question.

Green rows really answer the exam question. Arrows show how far the judge moved each page.

Challenges

Jump to the top: find a question where the judge moves the right page up to #1.
Run the exam and see whether the judge improves the score.
Why two steps? Check how much slower the judge is per page than the skimmer.
Your own words: type a question that isn't a button, and see the order change.

What just happened?

How it works

Prepare

Ahead of time, the skimmer turns all 60 pages into codes of 384 numbers.

Skim

Your question gets a code; the 20 closest page codes win. One quick sum per page.

Judge

Glue question + page together and run the judge on each of the 20 pairs. It gives each a score.

Re-sort

Order the 20 by the judge's score. The best few go to the chatbot.

For grown-ups: what's really going on

This is retrieve-then-rerank, the standard two-stage pipeline in search and RAG. Both models run in the browser with Transformers.js 3.8.1 (ONNX Runtime Web, WASM), 8-bit quantised.

  • Stage 1, bi-encoder: Xenova/all-MiniLM-L6-v2 encodes query and passage separately (mean pooling, L2-normalised, 384-d). Passage vectors are precomputed, so a query costs one encoder pass plus N dot products, which scales to millions with an ANN index. Weakness: everything about a passage must be squeezed into one vector before the query is known.
  • Stage 2, cross-encoder: Xenova/ms-marco-MiniLM-L-6-v2 (ONNX port of cross-encoder/ms-marco-MiniLM-L-6-v2, a 6-layer BERT trained on MS MARCO passage ranking) takes [CLS] query [SEP] passage [SEP] and outputs one relevance logit. Full attention between query and passage tokens makes it far more accurate, but nothing can be precomputed: cost is one transformer pass per (query, passage) pair. Pairs are scored here in batches of 10 with padding.
  • Metrics: MRR = mean over queries of 1 / rank of the first relevant passage (within the top 20); precision@3 = relevant passages in the top 3 ÷ 3. With exactly one relevant passage per query, P@3 is at most 0.33, so it is shown alongside "right page in top 1".
  • Latency: timings are wall-clock on your device. The cross-encoder's per-pair cost times the corpus size shows why it is never run over the whole collection; reranking only the top 20 to 100 keeps it affordable. Stage 2 can only reorder what stage 1 found: if the right passage isn't in the top 20, reranking can't save it (recall@20 bounds the final quality).

Further reading: Nogueira & Cho, Passage Re-ranking with BERT (2019) · Sentence-BERT (2019) · sentence-transformers: Retrieve & Re-Rank.