ML6: Language models and retrieval (RAG)

Lesson 2 of 5 · reading 15–25 minutes; practice and project time is on the module page

Search: matching words and matching meaning

🎯You'll be able to

Explain TF-IDF and embedding search, compute a similarity score, and improve word-based search with stemming and a glossary.

🏠The everyday picture

A librarian who only matches the exact words on your slip finds "refund policy" but not "getting my money back". A librarian who knows what you mean finds both. Search systems are one or the other, or a mix.

📘What it really is

📝A worked example

Input: the kit's 30 answerable test questions (eval/rag_questions.jsonl), scored with "hit at 3": is the right page among the top 3 results? Reference measurements, 2 November 2026:

Retrieval Hit at 3 Answer contains the expected fact
Plain TF-IDF on sections 0.9333 0.6667
With stemming and a 15-entry glossary 1.0 0.9333

The glossary was built by reading these same questions' failures, so these figures are optimistic; Lesson 4 measures it on questions kept aside. Look at the tokeniser: tokens("Parcels weren't delivered; deliveries failed twice") gives parcel, weren, deliver, delivery, fail, twice.

🌍Where it's used

📝Case scenario

A bank's help search found nothing for "card stuck in machine" because the page was titled "ATM card retention". Adding synonyms helped a little; adding embedding search found it and many similar cases. They kept word matching too, because customers also typed exact error codes, which embeddings sometimes ranked poorly.

🖱️Try it

Remove the glossary entry for "heaviest" from dakdost/rag.py, rebuild the index and ask "What's the heaviest parcel I can send?". Success: the answer now quotes the business-account sentence from the pricing page instead of the 30 kg limit, which shows exactly what the glossary was doing.

⚠️Common mistakes

🛠️For your project

Project 3's test 2 checks hit at 3 of at least 0.9 on the 30 answerable questions.

✅Quick check

What does stemming do for word matching?

Show the answer

It cuts word endings, so different forms of the same word match.

What can embeddings find that word matching misses?

Show the answer

Different words with the same meaning, such as "money back" and "refund".

← PreviousLarge language models, for engineers
ProLearners Assist