VisualAI
Chapter 8 · Using AI Well

Giving AI a Library

How do you make a model answer about your private documents? You let it look them up first. That's RAG.

8 minLesson 21 of 23

A language model knows a great deal about the world in general — and nothing at all about your world: your documents, your company, your notes. So how do you make it answer questions about those? You don't retrain it. You let it look them up first.

Ask a base model about a tiny company it's never heard of and it won't say “I don't know.” As we saw two lessons ago, it's built to sound right, so it will invent a fluent, confident, wrong answer. The fix is almost obvious once you see it: keep the real facts in a library, and when a question comes in, fetch the relevant pages and paste them into the prompt before the model answers. That's retrieval-augmented generation, or RAG.

Give It a Library

Pick a question · toggle retrieval · tune k

Ask Northwind's assistant

Strategy

Chunks retrieved (k)top 2

The library — 6 handbook snippets

Northwind Coffee was founded in 2019 in Portland by Mara Devi.

0.95#1

Our flagship roast, Midnight Tide, is a dark roast with notes of cocoa and fig.

0.18

Unopened bags may be returned within 30 days for a full refund.

0.05

Every bean is sourced from a single farmers' cooperative in Huila, Colombia.

0.22

The Portland café is open 7am–6pm on weekdays and closed on Sundays.

0.28#2

In 2023 we switched to fully compostable bags — no more plastic packaging.

0.12

Bars show how similar each snippet is to your question. Top 2 get pasted into the prompt.

The model answers

“Who started Northwind, and when?”

Northwind Coffee was founded in 2019, in Portland, by Mara Devi. [from snippet #1]

This is retrieval-augmented generation: don't train the facts into the model — store them, fetch the relevant ones at question time, and let the model answer from what it just read.

With retrieval on, the most similar snippets get pulled into the prompt and the model answers from them. Turn it off and it falls back on imagination. One question is a trap — the top match only looks relevant, so you need k ≥ 2.
Try this

Start on Answer from memory — every answer is fluent and fabricated. Switch to Look it up first and watch the right snippet light up and ground the reply. Then pick “Can I return a bag I never opened?” and drop k to 1: the top-ranked snippet is about packaging, not returns, so it whiffs. Raise k to 2 and the real answer gets pulled in.

Retrieve, then generate

The trick that makes retrieval work is that it searches by meaning, not keywords. Every snippet in the library is turned into an embedding— a point in a space where similar ideas sit close together. Your question becomes a point too, and retrieval simply grabs the nearest snippets. “Where do your beans come from?” lands next to the snippet about the Colombian cooperative even though they share almost no words.

Key idea

RAG splits the job in two: a retriever finds the relevant text, and the generator(the language model) writes the answer using it. The model's knowledge stops being something baked in once during training and becomes something you can swap out and update any time — just edit the library.

That's why RAG is everywhere: customer-support bots over a help centre, “chat with your PDF” tools, coding assistants that read your repo. It sidesteps three big problems at once — it grounds answers in real sources (fewer hallucinations), it works on private data the model was never trained on, and you can update the facts without touching the model. The catch is the one you just felt: retrieval can fetch the wrong thing. Get a poor match and the model answers confidently from irrelevant text — garbage in, fluent garbage out.

The payoff

Retrieval is really the model reaching for one tool — a search box — before it speaks. But why stop at one tool, used once? Give a model a whole toolbox and let it decide when to reach for each, in a loop, and a chatbot becomes something that can actually get things done. That's an agent — next.