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).
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
What just happened?
- The skimmer turned every page into a meaning code once. For your question it just compared codes: super fast, but it only checks "same topic".
- The judge read your question and each page together, word by word, so it can tell "about elephants" from "says how long elephants live".
- The judge is much slower per page, so it only gets the skimmer's top 20, never the whole library.
- The exam measures how high the right page lands. A higher score after the judge means a better page goes into the chatbot's prompt.
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-v2encodes query and passage separately (mean pooling, L2-normalised, 384-d). Passage vectors are precomputed, so a query costs one encoder pass plusNdot 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 ofcross-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 / rankof 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.