fastrag.benchmark.metrics#
Metrics: an object per measurement, scored per sample, aggregated per run.
- class fastrag.benchmark.metrics.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.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.AnswerRelevancy(*, judge)[source]#
Bases:
JudgeMetricHow relevant the answer is to the question asked, per the judge.
- Parameters:
judge (ChatModel)
- name = 'answer_relevancy'#
- async score(sample, outcome)[source]#
Ask the judge to score how relevant the answer is to the question.
- 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.Correctness(*, judge)[source]#
Bases:
JudgeMetricHow correct the answer is against the sample’s expected answers, per the judge.
- Parameters:
judge (ChatModel)
- name = 'correctness'#
- async score(sample, outcome)[source]#
Ask the judge to score correctness, or
Nonewith no expected answers.- 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.Cost(*, input_price, output_price)[source]#
Bases:
MetricDollar cost of one sample’s generation call.
- Parameters:
- name = 'cost_usd'#
- higher_is_better: bool = False#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- async score(sample, outcome)[source]#
Dollar cost from token counts, or
Nonewhen usage is unreported.- 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:
- class fastrag.benchmark.metrics.DistributionSummary(count, missing, mean, median, std, min, max, percentiles=<factory>)[source]#
Bases:
objectA run’s scores for one metric, never collapsed to a single mean.
missingcounts theNonescores, dropped beforemean/median/std/min/max/percentilesare computed over what is left. An empty or all-Noneinput yieldscount=0and zeros everywhere, never aZeroDivisionError.- Parameters:
- classmethod of(scores)[source]#
Build a
DistributionSummaryfrom raw per-sample scores.- Parameters:
- Return type:
- class fastrag.benchmark.metrics.Faithfulness(*, judge)[source]#
Bases:
JudgeMetricHow well the answer is supported by its retrieved sources, per the judge.
- Parameters:
judge (ChatModel)
- name = 'faithfulness'#
- async score(sample, outcome)[source]#
Ask the judge to score how well the answer is grounded in its sources.
- 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.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.
- class fastrag.benchmark.metrics.JudgeMetric(*, judge)[source]#
Bases:
MetricShared machinery for a metric scored by asking a chat model to grade.
- Parameters:
judge (ChatModel) – The chat model asked to grade each sample.
- needs_answer: bool = True#
Truewhenscore()readsoutcome.answer— never satisfied by a bareRetriever.
- 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.
- 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.Latency[source]#
Bases:
MetricWall-clock time for one sample, in milliseconds.
Never
None:Benchmarktimes every sample itself, so this metric works against a bareRetrievertoo, with no chat model involved.- name = 'latency_ms'#
- higher_is_better: bool = False#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- async score(sample, outcome)[source]#
Return the sample’s wall-clock latency in milliseconds.
- 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:
- class fastrag.benchmark.metrics.Metric[source]#
Bases:
ABCOne measurement, computed per sample and aggregated over a run.
A metric carries its own dependencies as class attributes, so
Benchmarkcan refuse an unsatisfiable request at construction rather than silently returning a number that means nothing.- needs_answer: bool = False#
Truewhenscore()readsoutcome.answer— never satisfied by a bareRetriever.
- higher_is_better: bool = True#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- abstractmethod async score(sample, outcome)[source]#
This sample’s score, or
Nonewhen it is not measurable.- Parameters:
sample (EvalSample)
outcome (QueryOutcome)
- Return type:
float | None
- class fastrag.benchmark.metrics.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.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.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.TokenOverlap[source]#
Bases:
MetricBag-of-words F1 between the answer and the best-matching expected answer.
A cheap smoke-test baseline, never accuracy: it rewards word overlap, not correctness, and a fluent answer that restates the question can score well while being wrong. Reach for the retrieval metrics or a judge-backed metric when the number needs to mean something.
- name = 'token_overlap'#
- needs_answer: bool = True#
Truewhenscore()readsoutcome.answer— never satisfied by a bareRetriever.
- async score(sample, outcome)[source]#
Best F1 across expected answers, or
Nonewhen there are none.- 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:
- class fastrag.benchmark.metrics.TokenUsage[source]#
Bases:
MetricInput plus output tokens for one sample.
Nonewhen either count is missing — a bareRetrievernever generates, andask()does not report usage (in the new query layerChatModel.invokereturns plain text, not aCompletioncarrying token counts).- name = 'total_tokens'#
- higher_is_better: bool = False#
Which direction improves.
Falseforlatency_ms,total_tokens, andcost_usd.
- async score(sample, outcome)[source]#
Total tokens, or
Nonewhen the provider reported no usage.- 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:
Modules
The |
|
Generation metrics: a free bag-of-words baseline, and three judge-backed scores. |
|
Operational metrics: cost of running the pipeline, not quality of its output. |
|
Retrieval metrics: deterministic, free, fast — reach for these first. |