AI Engineer DojoAll lessons  Courses
Free lesson · The AI Evaluation Engineer · Lesson 7

Did Search Find the Right Passage?

In a RAG system, the model can only use what the search step hands it. If the right passage never arrives, no prompt and no model can fix the answer. Your team is about to report a search score from a well-known benchmark. Before they do, you'll measure search yourself, find out what the benchmark is really measuring, and test it with questions in your own words.

How this lesson works

You'll play the search engine on one question, compute search scores by hand, run a full search over 2,067 Wikipedia paragraphs, and then try to break it with your own wording. Plan on about an hour. The script needs only Python, nothing to install.

The data

SQuAD is a reading-comprehension dataset from Stanford. People were shown paragraphs from Wikipedia articles and wrote questions that each paragraph answers. This lesson uses its development set: 2,067 paragraphs from 48 articles and 10,570 questions. For every question we know which paragraph holds the answer, so we can check whether a search finds it.

The search is BM25, the standard keyword ranking behind most search engines and many RAG systems. It scores each paragraph by the question's words it contains, giving more weight to rare words.

Source: Rajpurkar et al., 2016, "SQuAD: 100,000+ Questions for Machine Comprehension of Text." Dataset: SQuAD 1.1, CC-BY-SA 4.0. Paragraphs are Wikipedia text, CC-BY-SA.

Step 1

Be the search engine

A user asks:

Here are four paragraphs from the collection. Which one answers the question?

Predict
Where do you think keyword search ranked the paragraph with the answer, out of 2,067?

The answer is in the paragraph that begins "In 1501, at the age of 19, he entered the University of Erfurt." You probably found it in seconds, because you understood that "entered the University" answers "go to school" and that "he" is Luther.

Keyword search didn't return it at all. After dropping small words like "where" and "did," the question's words are "martin," "luther," "go" and "school." The paragraph contains none of them. It calls him "he" and says "University," not "school." BM25 only scores paragraphs that share a word with the question, so this one never entered the list. Look at what it ranked instead: first, a paragraph about Luther's birth and family, which names him; second, a paragraph about Nikola Tesla, which contains "Martin" (his math teacher, Martin Sekulić) and "school"; third, one that contains "Luther" and the word "go" ("one half was to go to the building of St Peter's"). That's keyword matching: it finds the right words and misses what the question means.

Data: SQuAD 1.1, Rajpurkar, P. et al. (2016). SQuAD: 100,000+ Questions for Machine Comprehension of Text. EMNLP. CC-BY-SA 4.0. BM25: Robertson, S. and Zaragoza, H. (2009). The Probabilistic Relevance Framework: BM25 and Beyond.