К содержанию
Статья ещё не опубликована на языке Russian. Показана версия на English.

Evaluating Retrieval Quality: Recall@K, Precision@K, MRR, and NDCG Explained

2 мин на чтение

Never evaluate a RAG system exclusively on final generated prose. When an answer is wrong, you need to know immediately whether the retriever failed to surface the right evidence or the generator misread the evidence it was given. Evaluating retrieval separately makes iteration faster, cheaper, and completely deterministic.

Core Retrieval Metrics

MetricWhat It MeasuresIdeal ForBlind Spot
Recall@KFraction of all relevant docs found in top KMaking sure required facts reach the LLMIgnores rank order inside top K
Precision@KFraction of top K docs that are relevantKeeping prompts clean and low-noiseCan look high even when key facts are missing
MRR (Mean Reciprocal Rank)1 / rank of the first relevant resultSingle-answer FAQ and lookup tasksIgnores 2nd and 3rd relevant passages
NDCG@KGraded relevance discounted logarithmically by rankMulti-document and ranked synthesis queriesRequires graded labels (0, 1, 2)

Computing Recall@K and NDCG@K in TypeScript

ts
export function recallAtK(retrievedIds: string[], expectedIds: Set<string>, k: number): number {
  if (expectedIds.size === 0) return 1;
  const topK = retrievedIds.slice(0, k);
  const hits = topK.filter(id => expectedIds.has(id)).length;
  return hits / expectedIds.size;
}

export function ndcgAtK(retrievedIds: string[], relevanceById: Map<string, number>, k: number): number {
  const dcg = (ids: string[]) =>
    ids.slice(0, k).reduce((sum, id, idx) => {
      const rel = relevanceById.get(id) ?? 0;
      return sum + (Math.pow(2, rel) - 1) / Math.log2(idx + 2);
    }, 0);

  const idealIds = Array.from(relevanceById.entries())
    .sort((a, b) => b[1] - a[1])
    .map(([id]) => id);

  const idcg = dcg(idealIds);
  return idcg === 0 ? 0 : dcg(retrievedIds) / idcg;
}

Building a High-Signal Golden Dataset

  1. Sample real production queries, not just synthetic prompts written by engineers who already know the exact article titles.
  2. Stratify by difficulty: include exact-code lookups, multi-turn follow-ups, multilingual queries, negative/unanswerable queries (where no doc should match), and multi-doc comparisons.
  3. Start with 50 to 100 well-curated test cases: a small, clean golden set run automatically in CI catches regressions far more reliably than a noisy 5,000-row synthetic dump.

Next: measure the generation half of the pipeline in Evaluating RAG Answers: Faithfulness, Groundedness, and RAGAS.

Было ли это полезно?