0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery. And welcome to Season Five, the final season: RAG, agents and production.
0:09Why LLMs answer from memory
Throughout this course, one problem kept returning: language models answering from memory, sometimes inventing details, like that sixty-day return policy. They also can't know Brightcart's private documents, or anything that changed after their training. The fix is simple to describe: turn every question into an open-book exam. Before answering, search Brightcart's own documents, pull out the most relevant passages, and put them in front of the model with instructions to answer from them, and cite them. That's retrieval-augmented generation, or RAG, one of the most widely used patterns in AI engineering.
Today: how it works, and the two main ways to find the right passages: keyword search, and search by meaning. Season Five, Module Eighteen: RAG and agents. First stop: retrieval.
1:06Why RAG?
Why is RAG so popular? It gives models knowledge they could never have learned in training: Brightcart's private policies, product details, today's stock levels. Update a document, and tomorrow's answers change, with no retraining. It reduces hallucinations, because answers are grounded in real text. It allows citations, so anyone can check the source. And it's efficient: instead of pasting the entire policy library into every prompt, which is costly, slow and easy to get lost in, it sends just the few passages that matter.
A common question: why not fine-tune the facts into the model instead? As a rule of thumb, retrieval is better for knowledge that changes or must be cited; fine-tuning is better for changing behaviour and style. We'll come back to that in Module Nineteen.
2:01The RAG pipeline
RAG has two phases. Indexing happens ahead of time, and again whenever documents change. Load the documents: policies, FAQs, product pages. Split them into chunks, small passages, which we'll study next episode. Index each chunk so it can be found quickly, and store it with useful details like its title, date and product. Then, for every question: retrieve the most relevant chunks, place them in the prompt inside clearly labelled context tags, and ask the model to answer only from that context, citing its sources, and to say so if the answer isn't there.
Optionally, a judge checks that every claim is supported. The whole system depends on one thing: retrieving the right passages. If retrieval fails, even a perfect model can't answer correctly.
2:54Keyword search: BM25
The classic way to search text is by keywords, and the most famous scoring formula is called B-M-twenty-five. It rewards passages that contain the query's words, with three refinements. Term frequency: a passage that mentions "refund" more often is more relevant, but with diminishing returns, so the tenth mention adds little. Inverse document frequency: rare words matter more. "Kettle" appears in only one of our four passages, so it's very informative; "days" appears in three, so it barely helps. And length normalisation: a long passage shouldn't win just because it contains more words.
For "my kettle arrived damaged, can I get a refund?", BM25 scores the damaged-items policy and the returns policy equally, about one point three nine each, with the kettle warranty close behind, and the delivery policy at zero. Keyword search is fast, needs no training, is easy to explain, and is excellent for exact terms, like product codes, order numbers and names.
4:01The paraphrase problem
Now the weakness. Another customer writes: "money back for a broken kettle." BM25 finds only one matching word, "kettle", so the kettle warranty comes top, and the damaged-items and returns policies score zero, because the customer said "money back" and "broken", not "refund" and "damaged". Keyword search matches words, not meaning, the same limitation we saw with word-overlap metrics in episode fifty-five.
4:32Search by meaning: embeddings
The second approach searches by meaning. Turn every passage, and the question, into an embedding vector, the meaning-space from episode thirty-five, and find the passages whose vectors point in the most similar direction. In a toy version of that space, "money back for a broken kettle" points closest to the damaged-items policy, with a cosine similarity of zero point eight eight, then the returns policy at zero point eight, then the kettle warranty, while the delivery policy is far away. Exactly the ranking we wanted, even though the words barely overlap.
With millions of chunks, comparing the question against every single vector is too slow, so vector databases use approximate nearest-neighbour indexes, the fast neighbour search we previewed with k-N-N in episode thirteen. They hop through a cleverly built graph of neighbours to find near-best matches in milliseconds. Embedding search has its own weak spots: exact codes, order numbers and rare names can slip through, and its quality depends heavily on the embedding model.
5:43Hybrid retrieval and rank fusion
Since the two methods fail in different ways, many systems use both. Run keyword search and vector search side by side, and merge their rankings. Their raw scores live on completely different scales, so a popular method, reciprocal rank fusion, ignores the scores and uses only positions: each passage gets one over sixty plus its rank, from each list, added together. For "money back for a broken kettle", keyword search finds only the kettle warranty; every other passage scores zero, so it doesn't return them at all.
Vector search ranks the damage policy first. After fusion, the warranty comes first, because both methods agree on it, and the damaged-items policy comes second. That's the real win: on its own, keyword search would have missed the damage policy entirely; now it's among the top results the model reads. Hybrid retrieval catches both exact matches and paraphrases, and it's a common default in production. Next episode, a reranking step will read the question and each passage together, and move the damage policy to the top.
6:56Try it yourself: a delivery question
Let's run keyword search on a new question: "how long does standard delivery take?" Which passage wins? Pause. The delivery passage, P3, scores about two point one nine, because "standard" appears only there, making it a rare, informative word, and "delivery" appears there twice. P1 gets a small score, because it mentions delivery once. But notice something: the query says "take" and the passage says "takes", and plain keyword matching treats them as different words. Many search systems reduce words to a common stem to catch exactly this.
7:41Why not paste the whole handbook?
Why not paste Brightcart's entire twelve-thousand-token handbook into every request, instead of retrieving pieces? Pause and estimate the monthly tokens. Twelve thousand tokens times fifty thousand requests is six hundred million tokens a month, against seventy-five million with retrieval: about six hundred dollars against seventy-five at our illustrative price. And it's not just money: long prompts are slower, the model can overlook what's buried in the middle, and you lose the ability to say exactly which passage an answer came from.
8:24Answers with citations
Retrieval makes citations possible. Give each retrieved passage an ID, and instruct the model to cite the ID after every policy claim. The reply then reads: "You can report damage within thirty days of delivery, P1, and return shipping is free for damaged items, P2." A simple checker confirms that every cited passage was actually retrieved and supports the claim. And when agents review a draft, they can click straight through to the source. Citations turn "trust me" into "check me".
8:57Keeping the index fresh
An index can go stale. Imagine Brightcart extends its returns window. The policy document is updated, but the search index still holds the old passages, so the assistant keeps quoting the old rule. The fixes: re-index automatically whenever a document changes, store a version and an effective date with every chunk, delete the outdated ones, and run a daily check that the index matches the source documents. A retrieval system is only as current as its index.
9:29Retrieval must respect permissions
When an index contains private data, permissions must be enforced before retrieval, not after. If the system searches everything and hopes the model won't mention someone else's order notes, that's a leak waiting to happen. Instead, every chunk carries metadata saying who may see it, and each search is restricted to what the verified customer is allowed to access. If the model never retrieves another customer's data, it can't reveal it.
9:59How vector search stays fast
How does vector search stay fast? Comparing a query with every stored vector is exact, but with a million chunks of seven hundred and sixty-eight numbers each, that's seven hundred and sixty-eight million multiply-adds for every question. So vector databases use approximate nearest-neighbour indexes, organising the vectors into clusters or graphs so each search visits only a small fraction of them. It's dramatically faster, at the cost of occasionally missing the very best match, which is why you tune the settings and measure retrieval quality.
10:36When nothing relevant is found
What happens when the answer isn't in the knowledge base? A customer asks whether Brightcart sells garden furniture. The best match is weak, a passage that only shares a word or two. Without care, the model improvises from it. A better system sets a minimum relevance score: below it, the assistant says it couldn't find the answer and offers a colleague. Those moments are logged as coverage gaps, which tell the team exactly which documentation is missing.
11:07Keyword, vector or hybrid?
Which retriever handles each query best? "Order B-4127". "My parcel never showed up". And "is the steel kettle covered if I drop it?" Pause. The order number needs keyword search: exact codes are where embeddings get fuzzy. "My parcel never showed up" needs meaning, since it shares almost no words with the delivery policy. The kettle question needs both: "steel kettle" is an exact match, while "covered" means warranty and "drop it" means accidental damage. Real traffic mixes all three, which is why hybrid retrieval is such a sensible default.
11:52One embedding model for everything
One rule trips up many teams: queries and documents must be embedded with the same model. Each embedding model builds its own space, like a map with its own grid, so a query vector from one model compared with document vectors from another gives meaningless scores. Some models even expect a short label marking which text is a query and which is a document. And switching to a new embedding model means re-embedding every single chunk, which should be a planned migration, tested before and after.
12:28Myth vs Fact
Myth: with RAG, a model can no longer hallucinate. Fact: RAG reduces hallucinations a lot, but the model can still misread the retrieved text, ignore it, or be misled by the wrong passage, like that kettle warranty. So evaluate both steps: did retrieval fetch the right passages, and is the answer actually supported by them?
12:51Recap
Let's recap. RAG turns every question into an open-book exam: retrieve relevant passages, then answer from them, with citations. Documents are chunked and indexed ahead of time. BM25 scores passages by rare, repeated query words, and excels at exact terms. Embedding search finds passages by meaning, catching paraphrases, using fast approximate nearest-neighbour indexes. And hybrid retrieval with rank fusion combines the strengths of both. One sentence to remember: find the right page first, then let the model read it.
13:30Next: better retrieval
Mission complete. Brightcart's assistant now answers from its real policy library, using hybrid search, and cites its sources. And "money back for a broken kettle" now finds the damaged-items policy, which keyword search alone missed. But Dev has found answers that are still wrong, because the retrieved chunk cut a policy in half, or the best passage came fifth. Next: how to chunk documents well, rerank results, rewrite vague questions, and measure retrieval properly. See you on the map.