Building a Golden Test Set for RAG
Create a curated question-answer dataset that lets you measure and compare RAG quality objectively over time.
Building a Golden Test Set for RAG is a free LangChain / RAG / Vector DBs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the LangChain / RAG / Vector DBs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why You Need a Test Set
Eyeballing a few answers does not tell you if a change helped or hurt. A golden test set of question-answer pairs gives you repeatable, comparable measurements.
Anatomy of a Test Case
Each case captures what to ask, what is correct, and where the answer lives.
questionground_truthanswerrelevant_sources(expected docs)
A Sample Dataset
Store cases as simple records you can load and iterate over.
testset = [
{"question": "What is the refund window?",
"ground_truth": "30 days from purchase.",
"sources": ["policy.pdf#p2"]},
{"question": "Who approves expenses?",
"ground_truth": "The department manager.",
"sources": ["handbook.pdf#p7"]},
]Manual vs. Synthetic
You can write cases by hand for accuracy, or generate them by prompting an LLM over your documents for scale. A hybrid approach is common: generate, then review.
Generating Questions with an LLM
Feed a chunk to the model and ask it to produce a question whose answer is contained in that chunk, plus the answer itself.
prompt = (
"Read the passage and write one question a user might ask, "
"plus the exact answer.\n\nPassage: " + chunk.page_content
)
qa = llm.invoke(prompt)Reviewing Synthetic Cases
LLM-generated pairs can be ambiguous or unanswerable. Human review filters out weak cases before they pollute your metrics.
Retrieval vs. Generation Metrics
Separate two questions: did we fetch the right docs (retrieval), and did we write the right answer (generation)? Each is measured differently.
Context Recall
Context recall checks whether the expected source appears among the retrieved chunks. It isolates retrieval quality from the LLM.
def context_recall(retrieved_ids, expected_ids):
hits = sum(1 for e in expected_ids if e in retrieved_ids)
return hits / len(expected_ids)Answer Correctness
Compare the generated answer to the ground truth. Exact match is brittle, so use an LLM judge or semantic similarity for fuzzy correctness.
judge_prompt = (
"Is the ANSWER correct given the REFERENCE? Reply yes or no.\n"
"REFERENCE: " + truth + "\nANSWER: " + answer
)
verdict = llm.invoke(judge_prompt)Running the Suite
Loop over every case, run your pipeline, and aggregate scores so one number summarizes the whole system.
scores = []
for case in testset:
docs = retriever.invoke(case["question"])
ans = rag_chain.invoke(case["question"])
scores.append(evaluate(case, docs, ans))
print(sum(scores) / len(scores))Track Results Over Time
Save each run with a timestamp and the config used. When a metric drops, you can pinpoint the change that caused the regression.
Quick Check
Test your understanding of RAG evaluation.
Recap
You built an evaluation foundation:
- A golden test set of question, ground truth, and sources
- Generate then review synthetic cases
- Measure retrieval (context recall) and generation (answer correctness) separately
- Track scores across runs
Frequently asked questions
Is the “Building a Golden Test Set for RAG” lesson free?
Yes — the full text of “Building a Golden Test Set for RAG” is free to read here on the web, and the LangChain / RAG / Vector DBs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the LangChain / RAG / Vector DBs course, upgrade to CoddyKit PRO.
What will I learn in “Building a Golden Test Set for RAG”?
Create a curated question-answer dataset that lets you measure and compare RAG quality objectively over time. You practise LangChain / RAG / Vector DBs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start LangChain / RAG / Vector DBs?
No prior experience is required. LangChain / RAG / Vector DBs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a Golden Test Set for RAG” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this LangChain / RAG / Vector DBs lesson?
Yes. Every LangChain / RAG / Vector DBs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integrating All RAG Components
- Querying and Generating Answers
- Evaluating RAG System Performance
- Building a Golden Test Set for RAG