fastrag.benchmark.metrics.retrieval#
Retrieval metrics: deterministic, free, fast — reach for these first.
Classes
|
|
|
|
|
Binary-gain NDCG, with the ideal ranking over |
|
|
|
|
|
Shared machinery for the five ranking metrics below. |
- class fastrag.benchmark.metrics.retrieval.RetrievalMetric(*, k=5, key='id')[source]#
Bases:
MetricShared machinery for the five ranking metrics below.
- Parameters:
k (int) – How many of the retrieved documents to consider, best first.
key (Literal['id', 'source_uri']) – Which field identifies a document for comparison against
sample.relevant_ids."id"is the chunk id (sha256(source_uri, chunk_index, content)), which changes with every chunker configuration."source_uri"compares at the page level, which survives a chunk-size sweep — pass it whenever the ground truth was authored per page rather than per chunk.
- aggregate(scores)#
Aggregate this metric’s per-sample scores. Override to change how.
- Parameters:
- Return type:
- higher_is_better: bool = True#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- needs_answer: bool = False#
Truewhenscore()readsoutcome.answer— never satisfied by a bareRetriever.
- abstractmethod async score(sample, outcome)#
This sample’s score, or
Nonewhen it is not measurable.- Parameters:
sample (EvalSample)
outcome (QueryOutcome)
- Return type:
float | None
- class fastrag.benchmark.metrics.retrieval.Recall(*, k=5, key='id')[source]#
Bases:
RetrievalMetric|relevant ∩ retrieved| / |relevant|.- async score(sample, outcome)[source]#
Recall at
k, orNonewhen the sample has no relevant ids.- Parameters:
sample (EvalSample)
outcome (QueryOutcome)
- Return type:
float | None
- aggregate(scores)#
Aggregate this metric’s per-sample scores. Override to change how.
- Parameters:
- Return type:
- higher_is_better: bool = True#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- class fastrag.benchmark.metrics.retrieval.Precision(*, k=5, key='id')[source]#
Bases:
RetrievalMetric|relevant ∩ retrieved| / len(retrieved).- async score(sample, outcome)[source]#
Precision at
k, orNonewhen the sample has no relevant ids.- Parameters:
sample (EvalSample)
outcome (QueryOutcome)
- Return type:
float | None
- aggregate(scores)#
Aggregate this metric’s per-sample scores. Override to change how.
- Parameters:
- Return type:
- higher_is_better: bool = True#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- class fastrag.benchmark.metrics.retrieval.MRR(*, k=5, key='id')[source]#
Bases:
RetrievalMetric1 / (position of the first relevant), else0.0.- async score(sample, outcome)[source]#
Reciprocal rank of the first relevant hit, or
Nonewith no relevant ids.- Parameters:
sample (EvalSample)
outcome (QueryOutcome)
- Return type:
float | None
- aggregate(scores)#
Aggregate this metric’s per-sample scores. Override to change how.
- Parameters:
- Return type:
- higher_is_better: bool = True#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- class fastrag.benchmark.metrics.retrieval.NDCG(*, k=5, key='id')[source]#
Bases:
RetrievalMetricBinary-gain NDCG, with the ideal ranking over
min(len(relevant), k).- async score(sample, outcome)[source]#
Binary-gain NDCG at
k, orNonewhen the sample has no relevant ids.- Parameters:
sample (EvalSample)
outcome (QueryOutcome)
- Return type:
float | None
- aggregate(scores)#
Aggregate this metric’s per-sample scores. Override to change how.
- Parameters:
- Return type:
- higher_is_better: bool = True#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- class fastrag.benchmark.metrics.retrieval.HitRate(*, k=5, key='id')[source]#
Bases:
RetrievalMetric1.0if any relevant document is in the window, else0.0.- async score(sample, outcome)[source]#
1.0 if any relevant document is in the window, else 0.0.
- Parameters:
sample (EvalSample)
outcome (QueryOutcome)
- Return type:
float | None
- aggregate(scores)#
Aggregate this metric’s per-sample scores. Override to change how.
- Parameters:
- Return type:
- higher_is_better: bool = True#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.