Skip to content

< all problems126 · Level 03, RAG

Judge a RAG Answer Without an Answer Key

medium · implement · RAG

Score a RAG system's answers with no answer key, the RAGAS way: a model breaks each answer into claims and checks each one against the retrieved context, and judges which retrieved chunks were useful. Implement faithfulness(question, answer, contexts, split, judge), context_precision(question, answer, contexts, judge) and evaluate(rows, split, judge). split(prompt) and judge(prompt) are models; each returns a string.

  1. Claims. Call split once with CLAIMS_PROMPT (fields question, answer). The claims are the reply's non-blank lines, stripped.
  2. Support. For each claim, in order, call judge with SUPPORT_PROMPT (fields context: the contexts joined with "\n\n", and claim).
  3. A verdict is yes when the reply's first word, lowercased and with punctuation stripped from its ends, is yes. Anything else is a no.
  4. faithfulness is the yes count over the claim count.
  5. context_precision calls judge once per chunk, in order, with USEFUL_PROMPT (fields question, answer, chunk). It is the mean of precision@k (useful chunks among the first k, over k) taken over the ranks k whose chunk is useful. No useful chunk gives 0.0.
  6. evaluate takes rows of {"question", "answer", "contexts"} and does faithfulness, then context precision, for each. It returns {"rows": [{"faithfulness": f, "context_precision": p}, ...], "faithfulness": mean, "context_precision": mean}.

The catch. An answer with no claims, such as a refusal, is neither faithful nor unfaithful: 0 would punish a system for declining, 1 would reward it for saying nothing. Its faithfulness is None, with no judge call, and a mean skips None instead of counting it as 0. A mean with nothing to average is None.