← Back to DAGDA | Support | Source: https://github.com/vibrantlabsai/ragas/pull/2816
Skip to content

feat(metrics): add traditional IR metrics — NDCGScore, MRRScore, PrecisionAtK, RecallAtK - #2816

Open
shadowmodder wants to merge 1 commit into
vibrantlabsai:mainfrom
shadowmodder:feat/traditional-retrieval-metrics
Open

shadowmodder wants to merge 1 commit into
vibrantlabsai:mainfrom
shadowmodder:feat/traditional-retrieval-metrics

Conversation

@shadowmodder

Copy link
Copy Markdown

Addresses #2028, contributes to the metrics refactor in #2315.

Summary

Adds four deterministic retrieval quality metrics that require no LLM:

Class What it measures
NDCGScore Normalized Discounted Cumulative Gain@K — penalises relevant docs ranked lower
MRRScore Mean Reciprocal Rank — 1/rank of the first relevant result
PrecisionAtK Fraction of top-K retrieved docs that are relevant
RecallAtK Fraction of relevant docs captured in top-K results

All four operate on SingleTurnSample.retrieved_context_ids and reference_context_ids, which are already part of the schema.

Usage

from ragas.metrics import NDCGScore, MRRScore, PrecisionAtK, RecallAtK
from ragas.dataset_schema import SingleTurnSample

sample = SingleTurnSample(
    user_input="What causes transformer attention to scale quadratically?",
    retrieved_context_ids=["doc_1", "doc_3", "doc_7", "doc_2", "doc_9"],
    reference_context_ids=["doc_1", "doc_7"],
)

print(NDCGScore(k=5).single_turn_score(sample))   # 0.9307
print(MRRScore().single_turn_score(sample))        # 1.0
print(PrecisionAtK(k=5).single_turn_score(sample)) # 0.4
print(RecallAtK(k=5).single_turn_score(sample))    # 1.0

Design

Follows the RougeScore pattern exactly: @dataclass, init() is a no-op, _single_turn_ascore() is pure Python with no external dependencies. All four are exported from ragas.metrics.

References

  • Järvelin & Kekäläinen, "Cumulated gain-based evaluation of IR techniques", ACM TOIS 2002

Addresses vibrantlabsai#2028, contributes to vibrantlabsai#2315

Adds NDCGScore, MRRScore, PrecisionAtK, RecallAtK as SingleTurnMetric
subclasses. These are deterministic, require no LLM, and operate on
retrieved_context_ids / reference_context_ids from SingleTurnSample.

All four follow the RougeScore pattern: dataclass, init() is a no-op,
_single_turn_ascore() is pure Python.
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

✅ No documentation update needed — The four new IR metrics (NDCGScore, MRRScore, PrecisionAtK, RecallAtK) all include complete docstrings with usage examples, parameters, and required fields, so the auto-generated API reference is covered without manual doc edits.

@shadowmodder

Copy link
Copy Markdown
Author

Checking in on this one. It still merges cleanly. If traditional IR metrics like NDCG and MRR are not something you want in ragas, tell me and I will close it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant