Мақала Kazakh тілінде әлі жарияланбаған. 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
| Metric | What It Measures | Ideal For | Blind Spot |
|---|---|---|---|
| Recall@K | Fraction of all relevant docs found in top K | Making sure required facts reach the LLM | Ignores rank order inside top K |
| Precision@K | Fraction of top K docs that are relevant | Keeping prompts clean and low-noise | Can look high even when key facts are missing |
| MRR (Mean Reciprocal Rank) | 1 / rank of the first relevant result | Single-answer FAQ and lookup tasks | Ignores 2nd and 3rd relevant passages |
| NDCG@K | Graded relevance discounted logarithmically by rank | Multi-document and ranked synthesis queries | Requires 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
- Sample real production queries, not just synthetic prompts written by engineers who already know the exact article titles.
- 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.
- 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.
Бұл пайдалы болды ма?