Exercise: a relevance grader and a web fallback
- Track
- Agentic RAG
- By
- Sourav Banerjee
- Last updated
- 30 Nov 2026
01 Context
Problem statement
Larkspur Cloud's handbook assistant answers from whatever retrieval returns, even when none of it fits the question. You add the two corrective steps of CRAG to the baseline, one at a time: a relevance grader that refuses when no retrieved passage answers the question, then a web fallback that searches outside the handbook instead of refusing. You measure what each step changes on 20 questions: 12 the handbook answers, 5 only a Larkspur web page answers, and 3 nobody can answer for Larkspur.
There is no web search offline. The fallback searches a local folder of eight pages that stands in for the web: three from Larkspur's public site and five from other sites. It is a stand-in, so treat its numbers as a test of the design, not of a search engine.
The rule you apply comes from lesson 3 of T9.2: correction earns its calls only when the fix it reaches for can supply the missing evidence. A fallback that answers a question with another provider's page has not supplied evidence. It has invented a Larkspur fact.
02 Outcomes
What you will learn
- Grade each retrieved passage for whether it answers the question, with a typed structured-output field.
- Add the grader and the fallback as two separate rows, so each change has its own effect.
- Count LLM calls per row, the cost side of lesson 3 of T9.2.
- Read the per-question changes to see which refusals the grader adds and which answers the fallback adds.
- Tell a fallback answer from Larkspur's own page apart from one taken from a stranger's page.
03 Setup
Before you start
Lessons
- T9.1 Agentic RAG Foundations: lesson 1 of T9.1 Retriever as a tool; grade then rewrite for the document grader, answerability against relatedness and typed grader labels.
- T9.2 Self-Correcting RAG: lesson 1 of T9.2 Corrective RAG with web-search fallback for the retrieval evaluator, the CRAG graph and where missing evidence may come from, and lesson 3 of T9.2 When correction is worth the extra calls for calls per question.
- T9.5 Reliability: lesson 1 of T9.5 Reliable RAG for what relevance grading can and cannot fix.
Dataset
The Larkspur Cloud handbook (13 files), eight local pages that stand in for the web, and 20 questions with the facts a correct answer must hold, all in data/. Twelve questions come from the golden set with handbook evidence. Five are answered only by a Larkspur page in the web folder. Three have a page on the same topic from another provider, and no Larkspur answer anywhere.
Code
load_handbook, load_jsonl and tfidf_retriever from src/lab_kit.py, RecursiveCharacterTextSplitter from langchain_text_splitters, ChatOllama from langchain_ollama, and with_structured_output with a Pydantic model for the grader.
Environment
The course's default environment (uv run jupyter lab) and a local Ollama server with qwen2.5:3b pulled (ollama pull qwen2.5:3b). Everything runs offline: no API keys and no network calls. In Colab you need to install Ollama in the runtime first.
04 Architecture
Experiment workflow
The handbook, the retriever, k = 4, the model, the prompts and the questions stay fixed. Each row adds one step to the one before it.
Extension: a fourth row that grades the web pages too before answering from them.
05 Build
Implementation tasks
Task 1: Load the data
Load the handbook from
data/larkspur/handbook/, the pages fromdata/web/and the questions fromdata/questions.jsonl.Task 2: Build the two searches
Split the handbook with the recursive splitter and build a TF-IDF retriever over the chunks. Keep each web page whole and build a second TF-IDF retriever over the pages. Keep a map from each text to its file or site, so the prompts can show where a passage came from.
RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) handbook_search = tfidf_retriever(chunks, k=4) web_search = tfidf_retriever(pages, k=3)New here: the web search is a TF-IDF search over a local folder. It stands in for the web search tool of lesson 1 of T9.2, which needs a network and a key.
Task 3: Write the grader
One call per passage that returns a typed field. Ask whether the passage answers the question, not whether it is on the same topic.
class DocGrade(BaseModel): relevant: bool = Field(description="True only if this passage answers the question, in whole or in part") grader = grade_prompt | llm.with_structured_output(DocGrade)Task 4: Run the three rows
Row 1 answers from all four passages. Row 2 grades each passage, answers from the ones that passed, and refuses when none did. Row 3 is row 2, except that a question with no passing passage goes to the web search and a web answer prompt. Grade each question once and let rows 2 and 3 share the grades, so they differ only on the fallback. Count the LLM calls each row makes per question.
Task 5: Score the replies
Label every reply
correct,refusedorwrong. A reply with a refusal phrase is a refusal. For a question nobody can answer, only a refusal is correct. Otherwise a reply is correct when it holds every key fact of its question. Print every reply beside its label and check the scorer by eye.New here: the labels come from string checks against
key_facts, not from a judge model, so the run scores the same way every time.Task 6: Read what changed
List the questions whose label changes from row 1 to row 2 and from row 2 to row 3. For each one, say whether the grader or the fallback was right to change it.
Task 7: Extension: grade the web pages
Run the same grader over the web results before the web answer, and refuse when no page passes. Add it as a fourth row.
06 Measure
Record results
What you enter stays in this browser only.
| Row | Handbook right (of 12) | Web-only right (of 5) | Refused rightly (of 3) | Wrong answers (of 20) | Web fallbacks | LLM calls |
|---|---|---|---|---|---|---|
| Baseline | none | 20 | ||||
| Grader | none | |||||
| Grader + web fallback | ||||||
| Grader + graded web (extension) |
A wrong answer is a reply that states something false or missing a key fact, or any answer to a question nobody can answer. A refusal on an answerable question is not wrong, but it is not right either.
07 Verify
Done when
0 of 4 done
What you enter stays in this browser only.
08 Debug
Common mistakes
Adding both steps in one row
Then a change in the table cannot be traced to the grader or to the fallback.
Grading relatedness
A grader that passes any passage on the same topic keeps the near misses lesson 1 of T9.1 describes, and the fallback never fires.
Trusting every fallback answer
The web branch answers from pages nobody graded (lesson 1 of T9.2). A page from another provider reads like a Larkspur fact once it is in the answer.
Scoring only the means
A row can gain web answers and lose handbook answers at the same time. The per-question list shows both.
09 Transfer
Interview angle
“Should a RAG assistant over a private handbook fall back to web search when retrieval fails?”
Show a model answer
Only for facts that can live outside the corpus, and only with a check on where the page came from. Measure it: count what the fallback answers, what it answers wrongly from other sites, and what the grader refused that the handbook could have answered.
10 Review
Worked solution
The worked solution is solution/p08_exercise_solution.ipynb (open in Colab). Locally, open it from exercise/solution/ so ../data/ resolves, start Ollama with qwen2.5:3b pulled, and run it top to bottom with the default environment. In Colab, clone the repository and install Ollama first, because the notebook reads the data and src/lab_kit.py from the repository and calls a local model.
Show our run and the reasoning
Our run: qwen2.5:3b through Ollama 0.40.6, langchain-ollama 1.1.0, langchain-core 1.7.0 and scikit-learn 1.9.1, offline.
| Row | Handbook right (of 12) | Web-only right (of 5) | Refused rightly (of 3) | Wrong answers (of 20) | Web fallbacks | LLM calls |
|---|---|---|---|---|---|---|
| Baseline | 9 | 0 | 3 | 4 | none | 20 |
| Grader | 3 | 0 | 3 | 3 | none | 80 |
| Grader + web fallback | 3 | 5 | 1 | 5 | 14 | 94 |
| Grader + graded web (extension) | 3 | 5 | 2 | 4 | 7 | 120 |
The grader is the expensive change, and with this model it does harm. It passed no passage at all for g01, g03, g17, g19 and g24, although the baseline's passages held the answer each time, so six questions the baseline answered became refusals (g02, which the baseline got wrong, became a refusal too). For g01 the refunds chunk with "5 to 10 business days" was retrieved first and graded not relevant. A 3B grader asked "does this answer the question?" says no too often, and the row costs four times the calls of the baseline. The grader's one gain is w02, where the baseline invented a Starter price from the server-size table and the grader refused instead.
The web fallback fired on 14 questions. On the 5 web questions it did what it is for: every answer came from the right Larkspur page and named it. On the 6 handbook questions the grader had wrongly rejected, it found nothing and said so, which is harmless but does not recover them. On the 3 questions nobody can answer it gave two wrong answers. g29 answered "a 30% discount" from Cobalt Hosting's page and named Cobalt, so the reply is true of someone else. w03 claimed Larkspur is ISO 27001 certified and cited Larkspur's about page, which says nothing of the kind. That is lesson 1 of T9.2's warning in two lines: the web branch answers from pages nobody graded.
Grading the web pages fixed w03, because no page passed and the row refused. It did not fix g29, because the Cobalt page does answer "what discount do non-profits get", just not for Larkspur. Relevance is not provenance. A source check (only larkspur.example pages may answer a Larkspur question) would stop it with no extra call.
Three replies in the table are the model failing rather than the design. For g13 and g15 in every row, and g05 in the graded rows, qwen2.5:3b replied only "[1]", a citation with no text. The scorer also counts the baseline's g03 reply as correct because it holds "30 days", although it first says you cannot export after cancelling. Read the printed replies before you trust a count.
For Larkspur we would keep the baseline, not ship this grader, and treat the fallback as a site search of Larkspur's own public pages. The fallback earns its calls only for public facts the handbook leaves out, which is the "can the answer live outside your corpus?" branch of lesson 3 of T9.2.