AI Engineer Portal
Your personal operating system for career transition.
Exercise
Evaluate RAG faithfulness with citation tracking
Evaluate RAG Faithfulness with Citation Tracking
A RAG answer is only as trustworthy as its grounding. Faithfulness evaluation measures whether each claim in the answer is supported by the retrieved context. Citation tracking extends this: can you point to the exact chunk that each claim came from?
What you are building
Create a FaithfulnessEvaluator that:
- Extracts claims from a generated answer (one per sentence).
- Matches each claim to the most relevant retrieved chunk using cosine similarity.
- Classifies each claim as
SUPPORTED(similarity above threshold) orUNSUPPORTED. - Computes a faithfulness score as the fraction of supported claims.
- Builds a citation map linking each supported claim to its best-matching chunk ID.
- Generates a human-readable summary of the evaluation result.
Why this matters
This is the core evaluation loop in every production RAG system that cares about auditability. Building it yourself means you understand what faithfulness actually measures, how to tune thresholds, and how to debug low scores.
Constraints
- Use cosine similarity for matching (inject the embedding function — no LLM judge).
- No external evaluation libraries.
- Return an
EvaluationResultdataclass with all detail needed to debug a failing case. - Default similarity threshold: 0.75 (configurable).
Rag Systems / medium / Step 4 of 6
General drill
Keep the solution explicit and reviewable.
Make the solution explicit, debuggable, and easy to explain.
Review where the boundary is, what gets validated, and what would be hard to debug later.
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.