RAG & Retrieval

Open-Book Robo

Robo has a little library of notes about Moonberry Island, a place he never learned about. With the book closed, he guesses from memory and confidently makes things up. With the book open, he looks up the right notes first, copies the facts and says where each one came from. A fact checker colours every sentence.

I'm in closed-book mode: no notes, just my memory. Pick a question and I'll answer with total confidence. Will I be right?

1 Look it up match score

2 Stuff the prompt


            

This is exactly what a real chatbot would be sent.

3 Answer + fact check

Challenges

Caught you: with the book closed, get an answer the fact checker marks red.
Open the book: get a fully green answer with a citation like [1].
Honest Robo: ask something the library doesn't know (dashed buttons) and get "I don't know".
Garbage in: poison the library and get a green, cited answer that is still wrong.
Top of the class: run the exam with the book open and score 12 out of 12.

What just happened?

How it works

Retrieve

Score every note by how many of the question's important words it contains. Keep the top k.

Filter

Throw away notes below the "I don't know" line. Nothing left? Say so.

Stuff the prompt

Paste the notes, numbered, above the question, with the rule "only use these notes".

Answer

Copy the best-matching sentences and tag each with the number of its note.

Check

For each sentence, look in the note it cites. If the important words are there, it's green.

For grown-ups: what's really going on

Retrieval-Augmented Generation (Lewis et al., 2020) conditions a generator on retrieved passages instead of relying on what is stored in its weights (parametric memory). Everything here runs in the page with no model download, so each stage is a transparent stand-in for the real thing:

  • Closed-book model: a word trigram language model trained live on 31 generic sentences (none about the island), with back-off to bigrams and unigrams, sampled with a seeded RNG from a template stem. It produces fluent, confident, wrong answers, the same failure mode as a large model asked about something outside its training data. "Ask again" re-samples, so the hallucination changes while the confidence doesn't.
  • Retriever: score = IDF-weighted share of the query's content words found in the note, Σw∈q∩d idf(w) / Σw∈q idf(w), ties broken by BM25. Real systems use dense embeddings (see the Ask My Notes lesson) or hybrid search.
  • Abstention: passages under the score threshold are dropped; with none left the system answers "I don't know". Too low a threshold lets weak matches through; too high refuses answerable questions (try 0.7: the ferry question fails).
  • Generator: extractive. It picks the one or two sentences from the kept passages that share the most IDF-weighted words with the question and appends [i] citations. A real LLM paraphrases, which is why it can drift from its sources; the "add his own words" toggle simulates that by appending a trigram sentence.
  • Fact checker: an attribution check, a lexical stand-in for an NLI/entailment model. Sentence s is supported by its cited source d if Σw∈s∩d idf(w) / Σw∈s idf(w) ≥ 0.8. Uncited sentences are checked against the whole library. Grounded-ness = supported / all factual sentences; abstentions count as neither.
  • Poisoning: a wrong note that matches strongly gets retrieved and cited, so the answer is wrong but passes the attribution check. Grounding measures faithfulness to sources, not truth, which is why corpus quality, provenance and access control matter (and why injected documents are an attack surface).

Further reading: Lewis et al., Retrieval-Augmented Generation (2020) · Hallucination (Wikipedia).