Skip to content

Evaluators

Modules:

Name Description
bertscore
bleu
g_eval
qags
rouge

Classes:

Name Description
BertScoreCalculator

Compute BERTScore without silently discarding tokens from either text.

BertScoreLengthError

Input length cannot be scored within the model's declared context.

BleuPrecisionScoreCalculator

Legacy modified unigram precision calculator, without a brevity penalty.

BleuScoreCalculator

Unsmoothed sentence BLEU-4 for one complete prediction/reference pair.

GEvalScoreCalculator

G-Eval with complete, in-range integer responses.

GEvalScorerFactory

Scorer factory for G-Eval.

QagsConfig

Configuration for EvalSense's QAGS QA agreement metric.

QagsScoreCalculator

QAGS score calculator with strict answer validation.

QagsScorerFactory

Scorer factory for the QAGS QA agreement metric.

QagsValidationError

Generated questions or answers cannot support a valid score.

RougeScoreCalculator

Calculator for computing ROUGE scores.

Functions:

Name Description
bleu_metric

Base metric for BLEU scores.

get_bertscore_evaluator

Returns a BERTScore evaluator.

get_bleu_evaluator

Return sentence BLEU-4 scores with a separately labelled corpus BLEU metric.

get_g_eval_evaluator

Constructs a G-Eval evaluator that can be used in EvalSense evaluation pipeline.

get_qags_evaluator

Constructs a QAGS evaluator for EvalSense.

get_rouge_evaluator

Returns an evaluator for ROUGE scores.

BertScoreCalculator

Bases: ScoreCalculator

Compute BERTScore without silently discarding tokens from either text.

Prediction and reference lengths are checked separately, including special tokens, before embedding computation. Counting follows BERTScore's tokenizer, surrounding-whitespace and prefix-space handling with truncation disabled. The smallest finite tokenizer/configuration limit applies. Overlength inputs raise BertScoreLengthError with counts, never the input text or a partial score.

XLNet explicitly declares no fixed positional limit. When its tokenizer also has no finite bound, the complete inputs are passed to BERTScore without an arbitrary cap. A finite tokenizer limit still applies. Missing declarations in other configurations remain errors; a large tokenizer sentinel alone is not evidence that the model supports arbitrary lengths.

Use the default slow tokenizer for unbounded XLNet with BERTScore 0.3.13: that backend passes the large length sentinel to fast tokenizers, which can overflow. Tokenizer/backend errors propagate without producing a score. No fixed positional limit does not guarantee enough memory or reliable long-document scores; validate the chosen model on the intended task.

Within-limit scores and hash metadata are unchanged. The validation context is loaded lazily, keeps only the current model/tokenizer setting, and is reset on cleanup. This calculator does not truncate, segment documents or average chunk scores. Increasing batch_size does not extend the context limit.

This per-sample implementation rejects IDF weighting because corpus weights cannot be estimated from a single reference. Only idf=False is supported.

Methods:

Name Description
__init__

Initializes the BERTScore calculator.

calculate

Calculates BERTScore for the supplied model prediction and reference input.

calculate_async

Calculates BERTScore for the supplied model prediction and reference input.

Source code in evalsense/evaluation/evaluators/bertscore.py
class BertScoreCalculator(ScoreCalculator):
    """Compute BERTScore without silently discarding tokens from either text.

    Prediction and reference lengths are checked separately, including special
    tokens, before embedding computation. Counting follows BERTScore's tokenizer,
    surrounding-whitespace and prefix-space handling with truncation disabled.
    The smallest finite tokenizer/configuration limit applies. Overlength inputs
    raise BertScoreLengthError with counts, never the input text or a partial score.

    XLNet explicitly declares no fixed positional limit. When its tokenizer also
    has no finite bound, the complete inputs are passed to BERTScore without an
    arbitrary cap. A finite tokenizer limit still applies. Missing declarations
    in other configurations remain errors; a large tokenizer sentinel alone is
    not evidence that the model supports arbitrary lengths.

    Use the default slow tokenizer for unbounded XLNet with BERTScore 0.3.13:
    that backend passes the large length sentinel to fast tokenizers, which can
    overflow. Tokenizer/backend errors propagate without producing a score.
    No fixed positional limit does not guarantee enough memory or reliable
    long-document scores; validate the chosen model on the intended task.

    Within-limit scores and hash metadata are unchanged. The validation context
    is loaded lazily, keeps only the current model/tokenizer setting, and is reset
    on cleanup. This calculator does not truncate, segment documents or average
    chunk scores. Increasing batch_size does not extend the context limit.

    This per-sample implementation rejects IDF weighting because corpus
    weights cannot be estimated from a single reference. Only idf=False
    is supported.
    """

    def __init__(
        self,
        model_type: str = "microsoft/deberta-xlarge-mnli",
        lang: str = "en",
        num_layers: int | None = None,
        idf: bool = False,
    ):
        """
        Initializes the BERTScore calculator.

        Args:
            model_type (str, optional): The model type to use for computing BERTScore.
                Defaults to "microsoft/deberta-xlarge-mnli", the currently best-performing
                model according to BERTScore authors.
            lang (str, optional): The language of the text. Defaults to "en".
            num_layers (int, optional): The layer of representations to use.
            idf (bool, optional): Must be False. Estimating IDF from a single
                reference is invalid, and the Evaluate backend does not use supplied
                IDF dictionaries as fixed weights. Defaults to False.

        Raises:
            ValueError: If idf is not False. No metric or model is loaded in that case.
        """
        _validate_pairwise_idf(idf)
        self.implementation = MetricImplementation.resolve("bertscore")
        self.bertscore_module = self.implementation.load()
        self.model_type = model_type
        self.lang = lang
        self.num_layers = num_layers
        self.idf = idf
        self._length_context: tuple[str, bool, Any, int | None] | None = None

    def _validate_input_lengths(
        self, prediction: str, reference: str, use_fast_tokenizer: bool
    ) -> None:
        context = self._length_context
        if context is None or context[:2] != (self.model_type, use_fast_tokenizer):
            tokenizer, limit = _load_length_context(self.model_type, use_fast_tokenizer)
            context = (self.model_type, use_fast_tokenizer, tokenizer, limit)
            self._length_context = context
        _, _, tokenizer, limit = context
        for source, text in (("prediction", prediction), ("reference", reference)):
            count = _encoded_length(tokenizer, text)
            if limit is not None and count > limit:
                # Do not echo clinical text into logs or error messages.
                raise BertScoreLengthError(
                    f"BERTScore {source} has {count} tokens including special tokens; "
                    f"the declared input limit is {limit}. No score was computed. "
                    "Use a suitable long-context model or an explicitly validated "
                    "segmentation strategy."
                )

    @override
    def calculate(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        verbose=False,
        device=None,
        batch_size=64,
        nthreads=1,
        rescale_with_baseline=False,
        baseline_path=None,
        use_fast_tokenizer=False,
        **kwargs,
    ) -> Score:
        """
        Calculates BERTScore for the supplied model prediction and reference input.

        Args:
            prediction (str): The text of the prediction from the model.
            input (str, optional): The text of the model input. Ignored for BERTScore.
            reference (str, optional): The text of the reference input to compare against.
            metadata (dict[str, Any], optional): Metadata for the evaluation. Ignored for BERTScore.
            verbose (bool): Whether to turn on verbose mode.
            device (str, optional): The device to use for computing the contextual embeddings.
            batch_size (int): The batch size to use for computing the contextual embeddings.
            nthreads (int): The number of threads to use for computing the contextual embeddings.
            rescale_with_baseline (bool): Whether to rescale the BERTScore with pre-computed baseline.
            baseline_path (str, optional): Customized baseline file.
            use_fast_tokenizer (bool): The `use_fast` parameter passed to HF tokenizer.

        Raises:
            BertScoreLengthError: If either input exceeds the declared context or
                its length capability is unknown. XLNet's explicit no-fixed-limit
                declaration is supported. No partial score is returned.

        Returns:
            Score: Inspect AI Score with the calculated evaluation results.
        """
        if reference is None:
            raise ValueError(
                "Reference is required for computing BERTScore, but was None."
            )

        # Validate public IDF configuration before loading tokenizer resources.
        _validate_pairwise_idf(self.idf)
        _validate_pairwise_idf(kwargs.get("idf", False))
        self._validate_input_lengths(prediction, reference, use_fast_tokenizer)
        predictions = [prediction]
        references = [reference]

        result = self.bertscore_module.compute(
            predictions=predictions,
            references=references,
            lang=self.lang,
            model_type=self.model_type,
            num_layers=self.num_layers,
            verbose=verbose,
            idf=False,
            device=device,
            batch_size=batch_size,
            nthreads=nthreads,
            rescale_with_baseline=rescale_with_baseline,
            baseline_path=baseline_path,
            use_fast_tokenizer=use_fast_tokenizer,
        )
        return Score(
            value={
                "BERTScore Precision": result["precision"][0],  # type: ignore
                "BERTScore Recall": result["recall"][0],  # type: ignore
                "BERTScore F1": result["f1"][0],  # type: ignore
            },
            answer=prediction,
            metadata={
                "hashcode": result["hashcode"],  # type: ignore
                "metric_provenance": self.implementation.provenance,
            },
        )

    @override
    async def calculate_async(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        verbose=False,
        device=None,
        batch_size=64,
        nthreads=1,
        rescale_with_baseline=False,
        baseline_path=None,
        use_fast_tokenizer=False,
        **kwargs,
    ) -> Score:
        """
        Calculates BERTScore for the supplied model prediction and reference input.

        Args:
            prediction (str): The text of the prediction from the model.
            input (str | None): The text of the model input. Ignored for BERTScore.
            reference (str, optional): The text of the reference input to compare against.
            metadata (dict[str, Any] | None): Metadata for the evaluation. Ignored for BERTScore.
            verbose (bool): Whether to turn on verbose mode.
            device (str | None): The device to use for computing the contextual embeddings.
            batch_size (int): The batch size to use for computing the contextual embeddings.
            nthreads (int): The number of threads to use for computing the contextual embeddings.
            rescale_with_baseline (bool): Whether to rescale the BERTScore with pre-computed baseline.
            baseline_path (str | None): Customized baseline file.
            use_fast_tokenizer (bool): The `use_fast` parameter passed to HF tokenizer.

        Raises:
            BertScoreLengthError: If either input exceeds the declared context or
                its length capability is unknown. XLNet's explicit no-fixed-limit
                declaration is supported. No partial score is returned.

        Returns:
            Score: Inspect AI Score with the calculated evaluation results.
        """
        # Generic calculator callers may supply options through kwargs.
        _validate_pairwise_idf(kwargs.get("idf", False))
        return self.calculate(
            prediction=prediction,
            reference=reference,
            verbose=verbose,
            device=device,
            batch_size=batch_size,
            nthreads=nthreads,
            rescale_with_baseline=rescale_with_baseline,
            baseline_path=baseline_path,
            use_fast_tokenizer=use_fast_tokenizer,
        )

__init__

__init__(
    model_type: str = "microsoft/deberta-xlarge-mnli",
    lang: str = "en",
    num_layers: int | None = None,
    idf: bool = False,
)

Initializes the BERTScore calculator.

Parameters:

Name Type Description Default
model_type str

The model type to use for computing BERTScore. Defaults to "microsoft/deberta-xlarge-mnli", the currently best-performing model according to BERTScore authors.

'microsoft/deberta-xlarge-mnli'
lang str

The language of the text. Defaults to "en".

'en'
num_layers int

The layer of representations to use.

None
idf bool

Must be False. Estimating IDF from a single reference is invalid, and the Evaluate backend does not use supplied IDF dictionaries as fixed weights. Defaults to False.

False

Raises:

Type Description
ValueError

If idf is not False. No metric or model is loaded in that case.

Source code in evalsense/evaluation/evaluators/bertscore.py
def __init__(
    self,
    model_type: str = "microsoft/deberta-xlarge-mnli",
    lang: str = "en",
    num_layers: int | None = None,
    idf: bool = False,
):
    """
    Initializes the BERTScore calculator.

    Args:
        model_type (str, optional): The model type to use for computing BERTScore.
            Defaults to "microsoft/deberta-xlarge-mnli", the currently best-performing
            model according to BERTScore authors.
        lang (str, optional): The language of the text. Defaults to "en".
        num_layers (int, optional): The layer of representations to use.
        idf (bool, optional): Must be False. Estimating IDF from a single
            reference is invalid, and the Evaluate backend does not use supplied
            IDF dictionaries as fixed weights. Defaults to False.

    Raises:
        ValueError: If idf is not False. No metric or model is loaded in that case.
    """
    _validate_pairwise_idf(idf)
    self.implementation = MetricImplementation.resolve("bertscore")
    self.bertscore_module = self.implementation.load()
    self.model_type = model_type
    self.lang = lang
    self.num_layers = num_layers
    self.idf = idf
    self._length_context: tuple[str, bool, Any, int | None] | None = None

calculate

calculate(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    verbose=False,
    device=None,
    batch_size=64,
    nthreads=1,
    rescale_with_baseline=False,
    baseline_path=None,
    use_fast_tokenizer=False,
    **kwargs,
) -> Score

Calculates BERTScore for the supplied model prediction and reference input.

Parameters:

Name Type Description Default
prediction str

The text of the prediction from the model.

required
input str

The text of the model input. Ignored for BERTScore.

None
reference str

The text of the reference input to compare against.

None
metadata dict[str, Any]

Metadata for the evaluation. Ignored for BERTScore.

None
verbose bool

Whether to turn on verbose mode.

False
device str

The device to use for computing the contextual embeddings.

None
batch_size int

The batch size to use for computing the contextual embeddings.

64
nthreads int

The number of threads to use for computing the contextual embeddings.

1
rescale_with_baseline bool

Whether to rescale the BERTScore with pre-computed baseline.

False
baseline_path str

Customized baseline file.

None
use_fast_tokenizer bool

The use_fast parameter passed to HF tokenizer.

False

Raises:

Type Description
BertScoreLengthError

If either input exceeds the declared context or its length capability is unknown. XLNet's explicit no-fixed-limit declaration is supported. No partial score is returned.

Returns:

Name Type Description
Score Score

Inspect AI Score with the calculated evaluation results.

Source code in evalsense/evaluation/evaluators/bertscore.py
@override
def calculate(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    verbose=False,
    device=None,
    batch_size=64,
    nthreads=1,
    rescale_with_baseline=False,
    baseline_path=None,
    use_fast_tokenizer=False,
    **kwargs,
) -> Score:
    """
    Calculates BERTScore for the supplied model prediction and reference input.

    Args:
        prediction (str): The text of the prediction from the model.
        input (str, optional): The text of the model input. Ignored for BERTScore.
        reference (str, optional): The text of the reference input to compare against.
        metadata (dict[str, Any], optional): Metadata for the evaluation. Ignored for BERTScore.
        verbose (bool): Whether to turn on verbose mode.
        device (str, optional): The device to use for computing the contextual embeddings.
        batch_size (int): The batch size to use for computing the contextual embeddings.
        nthreads (int): The number of threads to use for computing the contextual embeddings.
        rescale_with_baseline (bool): Whether to rescale the BERTScore with pre-computed baseline.
        baseline_path (str, optional): Customized baseline file.
        use_fast_tokenizer (bool): The `use_fast` parameter passed to HF tokenizer.

    Raises:
        BertScoreLengthError: If either input exceeds the declared context or
            its length capability is unknown. XLNet's explicit no-fixed-limit
            declaration is supported. No partial score is returned.

    Returns:
        Score: Inspect AI Score with the calculated evaluation results.
    """
    if reference is None:
        raise ValueError(
            "Reference is required for computing BERTScore, but was None."
        )

    # Validate public IDF configuration before loading tokenizer resources.
    _validate_pairwise_idf(self.idf)
    _validate_pairwise_idf(kwargs.get("idf", False))
    self._validate_input_lengths(prediction, reference, use_fast_tokenizer)
    predictions = [prediction]
    references = [reference]

    result = self.bertscore_module.compute(
        predictions=predictions,
        references=references,
        lang=self.lang,
        model_type=self.model_type,
        num_layers=self.num_layers,
        verbose=verbose,
        idf=False,
        device=device,
        batch_size=batch_size,
        nthreads=nthreads,
        rescale_with_baseline=rescale_with_baseline,
        baseline_path=baseline_path,
        use_fast_tokenizer=use_fast_tokenizer,
    )
    return Score(
        value={
            "BERTScore Precision": result["precision"][0],  # type: ignore
            "BERTScore Recall": result["recall"][0],  # type: ignore
            "BERTScore F1": result["f1"][0],  # type: ignore
        },
        answer=prediction,
        metadata={
            "hashcode": result["hashcode"],  # type: ignore
            "metric_provenance": self.implementation.provenance,
        },
    )

calculate_async async

calculate_async(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    verbose=False,
    device=None,
    batch_size=64,
    nthreads=1,
    rescale_with_baseline=False,
    baseline_path=None,
    use_fast_tokenizer=False,
    **kwargs,
) -> Score

Calculates BERTScore for the supplied model prediction and reference input.

Parameters:

Name Type Description Default
prediction str

The text of the prediction from the model.

required
input str | None

The text of the model input. Ignored for BERTScore.

None
reference str

The text of the reference input to compare against.

None
metadata dict[str, Any] | None

Metadata for the evaluation. Ignored for BERTScore.

None
verbose bool

Whether to turn on verbose mode.

False
device str | None

The device to use for computing the contextual embeddings.

None
batch_size int

The batch size to use for computing the contextual embeddings.

64
nthreads int

The number of threads to use for computing the contextual embeddings.

1
rescale_with_baseline bool

Whether to rescale the BERTScore with pre-computed baseline.

False
baseline_path str | None

Customized baseline file.

None
use_fast_tokenizer bool

The use_fast parameter passed to HF tokenizer.

False

Raises:

Type Description
BertScoreLengthError

If either input exceeds the declared context or its length capability is unknown. XLNet's explicit no-fixed-limit declaration is supported. No partial score is returned.

Returns:

Name Type Description
Score Score

Inspect AI Score with the calculated evaluation results.

Source code in evalsense/evaluation/evaluators/bertscore.py
@override
async def calculate_async(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    verbose=False,
    device=None,
    batch_size=64,
    nthreads=1,
    rescale_with_baseline=False,
    baseline_path=None,
    use_fast_tokenizer=False,
    **kwargs,
) -> Score:
    """
    Calculates BERTScore for the supplied model prediction and reference input.

    Args:
        prediction (str): The text of the prediction from the model.
        input (str | None): The text of the model input. Ignored for BERTScore.
        reference (str, optional): The text of the reference input to compare against.
        metadata (dict[str, Any] | None): Metadata for the evaluation. Ignored for BERTScore.
        verbose (bool): Whether to turn on verbose mode.
        device (str | None): The device to use for computing the contextual embeddings.
        batch_size (int): The batch size to use for computing the contextual embeddings.
        nthreads (int): The number of threads to use for computing the contextual embeddings.
        rescale_with_baseline (bool): Whether to rescale the BERTScore with pre-computed baseline.
        baseline_path (str | None): Customized baseline file.
        use_fast_tokenizer (bool): The `use_fast` parameter passed to HF tokenizer.

    Raises:
        BertScoreLengthError: If either input exceeds the declared context or
            its length capability is unknown. XLNet's explicit no-fixed-limit
            declaration is supported. No partial score is returned.

    Returns:
        Score: Inspect AI Score with the calculated evaluation results.
    """
    # Generic calculator callers may supply options through kwargs.
    _validate_pairwise_idf(kwargs.get("idf", False))
    return self.calculate(
        prediction=prediction,
        reference=reference,
        verbose=verbose,
        device=device,
        batch_size=batch_size,
        nthreads=nthreads,
        rescale_with_baseline=rescale_with_baseline,
        baseline_path=baseline_path,
        use_fast_tokenizer=use_fast_tokenizer,
    )

BertScoreLengthError

Bases: ValueError

Input length cannot be scored within the model's declared context.

Source code in evalsense/evaluation/evaluators/bertscore.py
class BertScoreLengthError(ValueError):
    """Input length cannot be scored within the model's declared context."""

BleuPrecisionScoreCalculator

Bases: ScoreCalculator

Legacy modified unigram precision calculator, without a brevity penalty.

Retained for direct callers. The BLEU evaluator uses BleuScoreCalculator; unigram precision must not be interpreted as sentence or corpus BLEU.

Methods:

Name Description
calculate

Calculates modified unigram precision, not BLEU, for one text pair.

calculate_async

Asynchronously call this calculator's calculate method with the same inputs.

Source code in evalsense/evaluation/evaluators/bleu.py
class BleuPrecisionScoreCalculator(ScoreCalculator):
    """Legacy modified unigram precision calculator, without a brevity penalty.

    Retained for direct callers. The BLEU evaluator uses BleuScoreCalculator;
    unigram precision must not be interpreted as sentence or corpus BLEU.
    """

    def __init__(self):
        self.implementation = MetricImplementation.resolve("bleu")
        self.bleu_module = self.implementation.load()

    @override
    def calculate(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs,
    ) -> Score:
        """
        Calculates modified unigram precision, not BLEU, for one text pair.

        Args:
            prediction (str): The text of the prediction from the model.
            input (str, optional): The text of the input to the model. Ignored for BLEU.
            reference (str, optional): The text of the reference input to compare against.
            metadata (dict[str, Any], optional): Additional metadata for the score.
                Ignored for BLEU.

        Returns:
            Score: Inspect AI Score with the calculated evaluation results.
        """
        if reference is None:
            raise ValueError(
                "Reference is required for computing BLEU precision, but was None."
            )

        predictions = [prediction]
        references = [reference]

        result = self.bleu_module.compute(
            predictions=predictions, references=references
        )
        return Score(
            value=result["precisions"][0],  # type: ignore
            answer=prediction,
            metadata={
                "prediction": prediction,
                "reference": reference,
                "metric_provenance": self.implementation.provenance,
            },
        )

    @override
    async def calculate_async(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs,
    ) -> Score:
        """
        Asynchronously call this calculator's calculate method with the same inputs.

        Args:
            prediction (str): The text of the prediction from the model.
            input (str, optional): The text of the input to the model. Ignored for BLEU.
            reference (str, optional): The text of the reference input to compare against.
            metadata (dict[str, Any], optional): Additional metadata for the score.
                Ignored for BLEU.

        Returns:
            Score: Inspect AI Score with the calculated evaluation results.
        """
        return self.calculate(
            prediction=prediction,
            reference=reference,
            input=input,
            metadata=metadata,
            **kwargs,
        )

calculate

calculate(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score

Calculates modified unigram precision, not BLEU, for one text pair.

Parameters:

Name Type Description Default
prediction str

The text of the prediction from the model.

required
input str

The text of the input to the model. Ignored for BLEU.

None
reference str

The text of the reference input to compare against.

None
metadata dict[str, Any]

Additional metadata for the score. Ignored for BLEU.

None

Returns:

Name Type Description
Score Score

Inspect AI Score with the calculated evaluation results.

Source code in evalsense/evaluation/evaluators/bleu.py
@override
def calculate(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score:
    """
    Calculates modified unigram precision, not BLEU, for one text pair.

    Args:
        prediction (str): The text of the prediction from the model.
        input (str, optional): The text of the input to the model. Ignored for BLEU.
        reference (str, optional): The text of the reference input to compare against.
        metadata (dict[str, Any], optional): Additional metadata for the score.
            Ignored for BLEU.

    Returns:
        Score: Inspect AI Score with the calculated evaluation results.
    """
    if reference is None:
        raise ValueError(
            "Reference is required for computing BLEU precision, but was None."
        )

    predictions = [prediction]
    references = [reference]

    result = self.bleu_module.compute(
        predictions=predictions, references=references
    )
    return Score(
        value=result["precisions"][0],  # type: ignore
        answer=prediction,
        metadata={
            "prediction": prediction,
            "reference": reference,
            "metric_provenance": self.implementation.provenance,
        },
    )

calculate_async async

calculate_async(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score

Asynchronously call this calculator's calculate method with the same inputs.

Parameters:

Name Type Description Default
prediction str

The text of the prediction from the model.

required
input str

The text of the input to the model. Ignored for BLEU.

None
reference str

The text of the reference input to compare against.

None
metadata dict[str, Any]

Additional metadata for the score. Ignored for BLEU.

None

Returns:

Name Type Description
Score Score

Inspect AI Score with the calculated evaluation results.

Source code in evalsense/evaluation/evaluators/bleu.py
@override
async def calculate_async(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score:
    """
    Asynchronously call this calculator's calculate method with the same inputs.

    Args:
        prediction (str): The text of the prediction from the model.
        input (str, optional): The text of the input to the model. Ignored for BLEU.
        reference (str, optional): The text of the reference input to compare against.
        metadata (dict[str, Any], optional): Additional metadata for the score.
            Ignored for BLEU.

    Returns:
        Score: Inspect AI Score with the calculated evaluation results.
    """
    return self.calculate(
        prediction=prediction,
        reference=reference,
        input=input,
        metadata=metadata,
        **kwargs,
    )

BleuScoreCalculator

Bases: BleuPrecisionScoreCalculator

Unsmoothed sentence BLEU-4 for one complete prediction/reference pair.

Uses Evaluate's BLEU backend and its default case-sensitive 13a tokenizer, uniform weights over n-gram orders 1 through 4, and the brevity penalty. Score.value is result["bleu"], not unigram precision. Scores are in [0, 1]. No smoothing or effective-order adjustment is applied: missing any n-gram order, including predictions shorter than four tokens, gives zero. Such short samples provide little information for correlation analyses.

Corpus BLEU recomputes pooled n-gram counts and the corpus brevity penalty; it is not the mean of these sentence scores. Original texts are retained in score metadata for that calculation. Sync and async calls use the same policy.

Methods:

Name Description
calculate

Compute the backend's BLEU-4 value for a single text pair.

calculate_async

Asynchronously call this calculator's calculate method with the same inputs.

Source code in evalsense/evaluation/evaluators/bleu.py
class BleuScoreCalculator(BleuPrecisionScoreCalculator):
    """Unsmoothed sentence BLEU-4 for one complete prediction/reference pair.

    Uses Evaluate's BLEU backend and its default case-sensitive 13a tokenizer,
    uniform weights over n-gram orders 1 through 4, and the brevity penalty.
    ``Score.value`` is ``result["bleu"]``, not unigram precision. Scores are in
    [0, 1]. No smoothing or effective-order adjustment is applied: missing any
    n-gram order, including predictions shorter than four tokens, gives zero.
    Such short samples provide little information for correlation analyses.

    Corpus BLEU recomputes pooled n-gram counts and the corpus brevity penalty;
    it is not the mean of these sentence scores. Original texts are retained in
    score metadata for that calculation. Sync and async calls use the same policy.
    """

    @override
    def calculate(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs,
    ) -> Score:
        """Compute the backend's BLEU-4 value for a single text pair.

        Args:
            prediction (str): Complete generated text.
            input (str | None): Unused source text.
            reference (str | None): Required reference text.
            metadata (dict[str, Any] | None): Unused sample metadata.
            **kwargs (Any): Unused arguments accepted by ScoreCalculator.

        Returns:
            Score: Sentence BLEU with input texts and the score definition recorded.

        Raises:
            ValueError: If a reference is missing or either text is blank.
                Backend errors propagate; no fallback score is returned.
        """
        if reference is None or not reference.strip() or not prediction.strip():
            raise ValueError(
                "Sentence BLEU requires non-empty prediction and reference texts."
            )
        result = self.bleu_module.compute(
            predictions=[prediction], references=[reference], max_order=4, smooth=False
        )
        result = cast(dict[str, Any], result)
        return Score(
            value=result["bleu"],
            answer=prediction,
            metadata={
                "prediction": prediction,
                "reference": reference,
                "bleu_sample_variant": "sentence_bleu_4",
                "max_order": 4,
                "smooth": False,
                "metric_provenance": self.implementation.provenance,
            },
        )

calculate

calculate(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score

Compute the backend's BLEU-4 value for a single text pair.

Parameters:

Name Type Description Default
prediction str

Complete generated text.

required
input str | None

Unused source text.

None
reference str | None

Required reference text.

None
metadata dict[str, Any] | None

Unused sample metadata.

None
**kwargs Any

Unused arguments accepted by ScoreCalculator.

{}

Returns:

Name Type Description
Score Score

Sentence BLEU with input texts and the score definition recorded.

Raises:

Type Description
ValueError

If a reference is missing or either text is blank. Backend errors propagate; no fallback score is returned.

Source code in evalsense/evaluation/evaluators/bleu.py
@override
def calculate(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score:
    """Compute the backend's BLEU-4 value for a single text pair.

    Args:
        prediction (str): Complete generated text.
        input (str | None): Unused source text.
        reference (str | None): Required reference text.
        metadata (dict[str, Any] | None): Unused sample metadata.
        **kwargs (Any): Unused arguments accepted by ScoreCalculator.

    Returns:
        Score: Sentence BLEU with input texts and the score definition recorded.

    Raises:
        ValueError: If a reference is missing or either text is blank.
            Backend errors propagate; no fallback score is returned.
    """
    if reference is None or not reference.strip() or not prediction.strip():
        raise ValueError(
            "Sentence BLEU requires non-empty prediction and reference texts."
        )
    result = self.bleu_module.compute(
        predictions=[prediction], references=[reference], max_order=4, smooth=False
    )
    result = cast(dict[str, Any], result)
    return Score(
        value=result["bleu"],
        answer=prediction,
        metadata={
            "prediction": prediction,
            "reference": reference,
            "bleu_sample_variant": "sentence_bleu_4",
            "max_order": 4,
            "smooth": False,
            "metric_provenance": self.implementation.provenance,
        },
    )

calculate_async async

calculate_async(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score

Asynchronously call this calculator's calculate method with the same inputs.

Parameters:

Name Type Description Default
prediction str

The text of the prediction from the model.

required
input str

The text of the input to the model. Ignored for BLEU.

None
reference str

The text of the reference input to compare against.

None
metadata dict[str, Any]

Additional metadata for the score. Ignored for BLEU.

None

Returns:

Name Type Description
Score Score

Inspect AI Score with the calculated evaluation results.

Source code in evalsense/evaluation/evaluators/bleu.py
@override
async def calculate_async(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score:
    """
    Asynchronously call this calculator's calculate method with the same inputs.

    Args:
        prediction (str): The text of the prediction from the model.
        input (str, optional): The text of the input to the model. Ignored for BLEU.
        reference (str, optional): The text of the reference input to compare against.
        metadata (dict[str, Any], optional): Additional metadata for the score.
            Ignored for BLEU.

    Returns:
        Score: Inspect AI Score with the calculated evaluation results.
    """
    return self.calculate(
        prediction=prediction,
        reference=reference,
        input=input,
        metadata=metadata,
        **kwargs,
    )

GEvalScoreCalculator

Bases: ScoreCalculator

G-Eval with complete, in-range integer responses.

Weighted mode requires one output token and complete rating evidence in the returned alternatives. Failures never fall back to a sampled rating. Syntax validation does not establish judge accuracy or injection resistance.

Methods:

Name Description
__init__

Initializes the G-Eval score calculator.

calculate

This method is not supported for G-Eval and will raise an error when called.

calculate_async

Calculates the G-Eval score asynchronously.

Source code in evalsense/evaluation/evaluators/g_eval.py
class GEvalScoreCalculator(ScoreCalculator):
    """G-Eval with complete, in-range integer responses.

    Weighted mode requires one output token and complete rating evidence in the
    returned alternatives. Failures never fall back to a sampled rating.
    Syntax validation does not establish judge accuracy or injection resistance.
    """

    def __init__(
        self,
        model: Model,
        prompt_template: str,
        logprobs: bool = True,
        top_logprobs: int = 20,
        min_score: int = 1,
        max_score: int = 10,
        normalise: bool = True,
        debug: bool = False,
    ):
        """
        Initializes the G-Eval score calculator.

        Args:
            model (Model): The model to use for evaluation.
            prompt_template (str): The prompt template with the scoring instructions.
            logprobs (bool): Use a one-token conditional mean over reported valid
                rating alternatives. Every allowed rating must be present; missing
                or inconsistent evidence raises ValueError. False uses the sampled
                integer without a one-token limit.
            top_logprobs (int): Positive integer alternative count, at least the
                number of allowed ratings in weighted mode. Providers may impose
                their own limit or omit low-probability ratings.
            min_score (int): The minimum valid score.
            max_score (int): The maximum valid score.
            normalise (bool): Whether to normalise the scores between 0 and 1.
            debug (bool): Retained for compatibility. Weighting failures always raise.
        """
        validate_score_range(min_score, max_score, normalise=normalise)
        validate_weighting_settings(logprobs, top_logprobs, min_score, max_score)
        self.model = model
        self.prompt_template = prompt_template
        self.logprobs = logprobs
        self.top_logprobs = top_logprobs
        self.min_score = min_score
        self.max_score = max_score
        self.normalise = normalise
        self.debug = debug

    @override
    def calculate(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs,
    ) -> Score:
        """This method is not supported for G-Eval and will raise an error when called.

        Use `calculate_async` instead.

        Raises:
            NotImplementedError: When called, as synchronous evaluation is not
                supported for G-Eval.
        """
        raise NotImplementedError(
            "Synchronous evaluation is not supported for G-Eval. "
            "Use calculate_async instead."
        )

    @override
    async def calculate_async(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs,
    ) -> Score:
        """Calculates the G-Eval score asynchronously.

        Args:
            prediction (str): The predicted output to evaluate.
            input (str | None): The input text for the model. Defaults to `None`.
            reference (str | None): The reference text for the model. Defaults to `None`.
            metadata (dict[str, Any] | None): Additional metadata for the evaluation.
                Defaults to `None`.
            **kwargs: Additional keyword arguments.

        Returns:
            Score: The calculated score.

        Raises:
            ValueError: If settings, the complete integer response or weighted
                token evidence are invalid. Failed weighting never returns a
                sampled score, and malformed responses are never repaired.
        """
        validate_score_range(self.min_score, self.max_score, normalise=self.normalise)
        validate_weighting_settings(
            self.logprobs, self.top_logprobs, self.min_score, self.max_score
        )
        logprobs_config = GenerateConfig(
            logprobs=self.logprobs,
            top_logprobs=self.top_logprobs if self.logprobs else None,
            max_tokens=1 if self.logprobs else None,
            num_choices=1 if self.logprobs else None,
        )
        if metadata is None:
            metadata = {}
        llm_input = format_template(
            self.prompt_template,
            prediction=prediction,
            reference=reference,
            input=input,
            **metadata,
        )
        output = await self.model.generate(llm_input, config=logprobs_config)

        raw_score = extract_score(output.completion, self.min_score, self.max_score)
        weighted_rating = None
        if self.logprobs:
            weighted_rating = extract_weighted_rating(
                output, min_score=self.min_score, max_score=self.max_score
            )
            raw_score = weighted_rating.score

        if self.normalise:
            score = (raw_score - self.min_score) / (self.max_score - self.min_score)
        else:
            score = raw_score

        score_metadata: dict[str, Any] = {
            "prompt": llm_input,
            "output_text": output.completion,
            "raw_score": raw_score,
            "score_format": SCORE_FORMAT,
            "estimator_format": ESTIMATOR_FORMAT,
            "estimator": "weighted_rating_tokens"
            if self.logprobs
            else "sampled_rating",
        }
        if weighted_rating is not None:
            score_metadata.update(
                weighting_policy=WEIGHTING_POLICY,
                rating_probabilities={
                    str(rating): probability
                    for rating, probability in sorted(
                        weighted_rating.probabilities.items()
                    )
                },
                rating_probability_mass=weighted_rating.probability_mass,
            )
        return Score(value=score, answer=prediction, metadata=score_metadata)

__init__

__init__(
    model: Model,
    prompt_template: str,
    logprobs: bool = True,
    top_logprobs: int = 20,
    min_score: int = 1,
    max_score: int = 10,
    normalise: bool = True,
    debug: bool = False,
)

Initializes the G-Eval score calculator.

Parameters:

Name Type Description Default
model Model

The model to use for evaluation.

required
prompt_template str

The prompt template with the scoring instructions.

required
logprobs bool

Use a one-token conditional mean over reported valid rating alternatives. Every allowed rating must be present; missing or inconsistent evidence raises ValueError. False uses the sampled integer without a one-token limit.

True
top_logprobs int

Positive integer alternative count, at least the number of allowed ratings in weighted mode. Providers may impose their own limit or omit low-probability ratings.

20
min_score int

The minimum valid score.

1
max_score int

The maximum valid score.

10
normalise bool

Whether to normalise the scores between 0 and 1.

True
debug bool

Retained for compatibility. Weighting failures always raise.

False
Source code in evalsense/evaluation/evaluators/g_eval.py
def __init__(
    self,
    model: Model,
    prompt_template: str,
    logprobs: bool = True,
    top_logprobs: int = 20,
    min_score: int = 1,
    max_score: int = 10,
    normalise: bool = True,
    debug: bool = False,
):
    """
    Initializes the G-Eval score calculator.

    Args:
        model (Model): The model to use for evaluation.
        prompt_template (str): The prompt template with the scoring instructions.
        logprobs (bool): Use a one-token conditional mean over reported valid
            rating alternatives. Every allowed rating must be present; missing
            or inconsistent evidence raises ValueError. False uses the sampled
            integer without a one-token limit.
        top_logprobs (int): Positive integer alternative count, at least the
            number of allowed ratings in weighted mode. Providers may impose
            their own limit or omit low-probability ratings.
        min_score (int): The minimum valid score.
        max_score (int): The maximum valid score.
        normalise (bool): Whether to normalise the scores between 0 and 1.
        debug (bool): Retained for compatibility. Weighting failures always raise.
    """
    validate_score_range(min_score, max_score, normalise=normalise)
    validate_weighting_settings(logprobs, top_logprobs, min_score, max_score)
    self.model = model
    self.prompt_template = prompt_template
    self.logprobs = logprobs
    self.top_logprobs = top_logprobs
    self.min_score = min_score
    self.max_score = max_score
    self.normalise = normalise
    self.debug = debug

calculate

calculate(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score

This method is not supported for G-Eval and will raise an error when called.

Use calculate_async instead.

Raises:

Type Description
NotImplementedError

When called, as synchronous evaluation is not supported for G-Eval.

Source code in evalsense/evaluation/evaluators/g_eval.py
@override
def calculate(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score:
    """This method is not supported for G-Eval and will raise an error when called.

    Use `calculate_async` instead.

    Raises:
        NotImplementedError: When called, as synchronous evaluation is not
            supported for G-Eval.
    """
    raise NotImplementedError(
        "Synchronous evaluation is not supported for G-Eval. "
        "Use calculate_async instead."
    )

calculate_async async

calculate_async(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score

Calculates the G-Eval score asynchronously.

Parameters:

Name Type Description Default
prediction str

The predicted output to evaluate.

required
input str | None

The input text for the model. Defaults to None.

None
reference str | None

The reference text for the model. Defaults to None.

None
metadata dict[str, Any] | None

Additional metadata for the evaluation. Defaults to None.

None
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
Score Score

The calculated score.

Raises:

Type Description
ValueError

If settings, the complete integer response or weighted token evidence are invalid. Failed weighting never returns a sampled score, and malformed responses are never repaired.

Source code in evalsense/evaluation/evaluators/g_eval.py
@override
async def calculate_async(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs,
) -> Score:
    """Calculates the G-Eval score asynchronously.

    Args:
        prediction (str): The predicted output to evaluate.
        input (str | None): The input text for the model. Defaults to `None`.
        reference (str | None): The reference text for the model. Defaults to `None`.
        metadata (dict[str, Any] | None): Additional metadata for the evaluation.
            Defaults to `None`.
        **kwargs: Additional keyword arguments.

    Returns:
        Score: The calculated score.

    Raises:
        ValueError: If settings, the complete integer response or weighted
            token evidence are invalid. Failed weighting never returns a
            sampled score, and malformed responses are never repaired.
    """
    validate_score_range(self.min_score, self.max_score, normalise=self.normalise)
    validate_weighting_settings(
        self.logprobs, self.top_logprobs, self.min_score, self.max_score
    )
    logprobs_config = GenerateConfig(
        logprobs=self.logprobs,
        top_logprobs=self.top_logprobs if self.logprobs else None,
        max_tokens=1 if self.logprobs else None,
        num_choices=1 if self.logprobs else None,
    )
    if metadata is None:
        metadata = {}
    llm_input = format_template(
        self.prompt_template,
        prediction=prediction,
        reference=reference,
        input=input,
        **metadata,
    )
    output = await self.model.generate(llm_input, config=logprobs_config)

    raw_score = extract_score(output.completion, self.min_score, self.max_score)
    weighted_rating = None
    if self.logprobs:
        weighted_rating = extract_weighted_rating(
            output, min_score=self.min_score, max_score=self.max_score
        )
        raw_score = weighted_rating.score

    if self.normalise:
        score = (raw_score - self.min_score) / (self.max_score - self.min_score)
    else:
        score = raw_score

    score_metadata: dict[str, Any] = {
        "prompt": llm_input,
        "output_text": output.completion,
        "raw_score": raw_score,
        "score_format": SCORE_FORMAT,
        "estimator_format": ESTIMATOR_FORMAT,
        "estimator": "weighted_rating_tokens"
        if self.logprobs
        else "sampled_rating",
    }
    if weighted_rating is not None:
        score_metadata.update(
            weighting_policy=WEIGHTING_POLICY,
            rating_probabilities={
                str(rating): probability
                for rating, probability in sorted(
                    weighted_rating.probabilities.items()
                )
            },
            rating_probability_mass=weighted_rating.probability_mass,
        )
    return Score(value=score, answer=prediction, metadata=score_metadata)

GEvalScorerFactory

Bases: ScorerFactory

Scorer factory for G-Eval.

Methods:

Name Description
__init__

Initialize the G-Eval scorer factory.

create_scorer

Creates a G-Eval scorer.

Source code in evalsense/evaluation/evaluators/g_eval.py
class GEvalScorerFactory(ScorerFactory):
    """Scorer factory for G-Eval."""

    def __init__(
        self,
        name: str,
        prompt_template: str,
        metrics: list[Metric | dict[str, list[Metric]]]
        | dict[str, list[Metric]]
        | None = None,
        logprobs: bool = True,
        top_logprobs: int = 20,
        min_score: int = 1,
        max_score: int = 10,
        normalise: bool = True,
        debug: bool = False,
    ):
        """
        Initialize the G-Eval scorer factory.

        Args:
            name (str): The name of the scorer.
            prompt_template (str): The prompt template with the scoring instructions.
            metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
                The metrics to use for the evaluation. If `None`, the default metric
                will be used (G-Eval with mean aggregation).
            logprobs (bool): Use a one-token conditional mean over reported valid
                rating alternatives. Every allowed rating must be present; missing
                or inconsistent evidence raises ValueError. False uses the sampled
                integer without a one-token limit.
            top_logprobs (int): Positive integer alternative count, at least the
                number of allowed ratings in weighted mode. Providers may impose
                their own limit or omit low-probability ratings.
            min_score (int): The minimum valid score.
            max_score (int): The maximum valid score.
            normalise (bool): Whether to normalise the scores between 0 and 1.
            debug (bool): Retained for compatibility. Weighting failures always raise.
        """
        validate_score_range(min_score, max_score, normalise=normalise)
        validate_weighting_settings(logprobs, top_logprobs, min_score, max_score)
        self.name = name
        self.prompt_template = prompt_template
        if metrics is None:
            metrics = [mean()]
        self.metrics = metrics
        self.logprobs = logprobs
        self.top_logprobs = top_logprobs
        self.min_score = min_score
        self.max_score = max_score
        self.normalise = normalise
        self.debug = debug

    @override
    def create_scorer(self, model: Model) -> Scorer:
        """
        Creates a G-Eval scorer.

        Args:
            model (Model): The model to create a scorer for.

        Returns:
            Scorer: The created G-Eval scorer.
        """

        @scorer(name=self.name, metrics=self.metrics)
        def g_eval_scorer() -> Scorer:
            g_eval_calculator = GEvalScoreCalculator(
                model=model,
                prompt_template=self.prompt_template,
                logprobs=self.logprobs,
                top_logprobs=self.top_logprobs,
                min_score=self.min_score,
                max_score=self.max_score,
                normalise=self.normalise,
                debug=self.debug,
            )

            async def score(state: TaskState, target: Target):
                return await g_eval_calculator.calculate_async(
                    input=state.input_text,
                    prediction=state.output.completion,
                    reference=target.text,
                    metadata=state.metadata,
                )

            return score

        return g_eval_scorer()

__init__

__init__(
    name: str,
    prompt_template: str,
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    logprobs: bool = True,
    top_logprobs: int = 20,
    min_score: int = 1,
    max_score: int = 10,
    normalise: bool = True,
    debug: bool = False,
)

Initialize the G-Eval scorer factory.

Parameters:

Name Type Description Default
name str

The name of the scorer.

required
prompt_template str

The prompt template with the scoring instructions.

required
metrics list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None

The metrics to use for the evaluation. If None, the default metric will be used (G-Eval with mean aggregation).

None
logprobs bool

Use a one-token conditional mean over reported valid rating alternatives. Every allowed rating must be present; missing or inconsistent evidence raises ValueError. False uses the sampled integer without a one-token limit.

True
top_logprobs int

Positive integer alternative count, at least the number of allowed ratings in weighted mode. Providers may impose their own limit or omit low-probability ratings.

20
min_score int

The minimum valid score.

1
max_score int

The maximum valid score.

10
normalise bool

Whether to normalise the scores between 0 and 1.

True
debug bool

Retained for compatibility. Weighting failures always raise.

False
Source code in evalsense/evaluation/evaluators/g_eval.py
def __init__(
    self,
    name: str,
    prompt_template: str,
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    logprobs: bool = True,
    top_logprobs: int = 20,
    min_score: int = 1,
    max_score: int = 10,
    normalise: bool = True,
    debug: bool = False,
):
    """
    Initialize the G-Eval scorer factory.

    Args:
        name (str): The name of the scorer.
        prompt_template (str): The prompt template with the scoring instructions.
        metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
            The metrics to use for the evaluation. If `None`, the default metric
            will be used (G-Eval with mean aggregation).
        logprobs (bool): Use a one-token conditional mean over reported valid
            rating alternatives. Every allowed rating must be present; missing
            or inconsistent evidence raises ValueError. False uses the sampled
            integer without a one-token limit.
        top_logprobs (int): Positive integer alternative count, at least the
            number of allowed ratings in weighted mode. Providers may impose
            their own limit or omit low-probability ratings.
        min_score (int): The minimum valid score.
        max_score (int): The maximum valid score.
        normalise (bool): Whether to normalise the scores between 0 and 1.
        debug (bool): Retained for compatibility. Weighting failures always raise.
    """
    validate_score_range(min_score, max_score, normalise=normalise)
    validate_weighting_settings(logprobs, top_logprobs, min_score, max_score)
    self.name = name
    self.prompt_template = prompt_template
    if metrics is None:
        metrics = [mean()]
    self.metrics = metrics
    self.logprobs = logprobs
    self.top_logprobs = top_logprobs
    self.min_score = min_score
    self.max_score = max_score
    self.normalise = normalise
    self.debug = debug

create_scorer

create_scorer(model: Model) -> Scorer

Creates a G-Eval scorer.

Parameters:

Name Type Description Default
model Model

The model to create a scorer for.

required

Returns:

Name Type Description
Scorer Scorer

The created G-Eval scorer.

Source code in evalsense/evaluation/evaluators/g_eval.py
@override
def create_scorer(self, model: Model) -> Scorer:
    """
    Creates a G-Eval scorer.

    Args:
        model (Model): The model to create a scorer for.

    Returns:
        Scorer: The created G-Eval scorer.
    """

    @scorer(name=self.name, metrics=self.metrics)
    def g_eval_scorer() -> Scorer:
        g_eval_calculator = GEvalScoreCalculator(
            model=model,
            prompt_template=self.prompt_template,
            logprobs=self.logprobs,
            top_logprobs=self.top_logprobs,
            min_score=self.min_score,
            max_score=self.max_score,
            normalise=self.normalise,
            debug=self.debug,
        )

        async def score(state: TaskState, target: Target):
            return await g_eval_calculator.calculate_async(
                input=state.input_text,
                prediction=state.output.completion,
                reference=target.text,
                metadata=state.metadata,
            )

        return score

    return g_eval_scorer()

QagsConfig

Bases: Protocol

Configuration for EvalSense's QAGS QA agreement metric.

EvalSense adapts Wang et al. (2020) by generating questions from both texts and supporting ternary, exact-match and LLM-judge comparisons. It does not use the original answer-conditioned question filtering. The EvalSense paper reports a clinical-note case study and meta-evaluation of the ternary and judge variants. Suitability still depends on the task, prompts and judge.

Prompts choose whether the comparison uses a reference note or source text; agreement with a reference note does not independently check its source.

Methods:

Name Description
__init__

Initializes the QAGS configuration.

enforce_not_none

Helper method to enforce that a parameter is not None.

get_answer_comparison_prompt

Constructs the prompt for comparing answers to the generated questions.

get_answer_generation_prompt

Constructs the prompt for generating the answer to a single question.

get_question_generation_prompt

Constructs the prompt for generating the questions for the model output.

Source code in evalsense/evaluation/evaluators/qags.py
class QagsConfig(Protocol):
    """Configuration for EvalSense's QAGS QA agreement metric.

    EvalSense adapts [Wang et al. (2020)](https://aclanthology.org/2020.acl-main.450/)
    by generating questions from both texts and supporting ternary, exact-match
    and LLM-judge comparisons. It does not use the original answer-conditioned
    question filtering. The [EvalSense paper](https://aclanthology.org/2026.eacl-demo.33/)
    reports a clinical-note case study and meta-evaluation of the ternary and
    judge variants. Suitability still depends on the task, prompts and judge.

    Prompts choose whether the comparison uses a reference note or source text;
    agreement with a reference note does not independently check its source.
    """

    answer_comparison_mode: Literal["ternary", "exact", "judge"]
    logprobs: bool
    top_logprobs: int
    ci: float
    debug: bool

    def __init__(
        self,
        answer_comparison_mode: Literal["ternary", "exact", "judge"],
        logprobs: bool = True,
        top_logprobs: int = 20,
        ci: float = 0.1,
        debug: bool = False,
    ):
        """
        Initializes the QAGS configuration.

        Args:
            answer_comparison_mode (Literal["ternary", "exact", "judge"]): The mode
                for comparing answers. Either "ternary", "exact", or "judge".
                In "ternary" mode, the model is expected to answer the generated
                questions with "yes", "no", or "unknown". In other modes, the model
                may give arbitrary answers, which are either compared in terms
                of exact match or compared by the model itself.
            logprobs (bool): Whether to use logprobs to compute weighted answers. Can only
                be used when `answer_comparison_mode` is set to "judge".
            top_logprobs (int): The number of top log probabilities to consider
                when computing weighted answers.
            ci (float): The range near the extreme values (0.0 or 1.0) in which
                to consider the model answer as confident when comparing answers.
                This only affects the score explanation when `answer_comparison_mode`
                is set to "judge". The default value is 0.1, which means that
                answers with a score of 0.9 or are confident "yes", while answers
                with a score of 0.1 or lower are confident "no".
            debug (bool): Whether to report repeated errors in the log.
        """
        self.answer_comparison_mode = answer_comparison_mode
        self.logprobs = logprobs
        self.top_logprobs = top_logprobs
        self.ci = ci
        self.debug = debug

    def enforce_not_none[T](self, param_name: str, param_value: T | None) -> T:
        """
        Helper method to enforce that a parameter is not None.

        Args:
            param_name (str): The name of the parameter.
            param_value (T | None): The value of the parameter.

        Raises:
            ValueError: If the parameter value is None.

        Returns:
            T: The parameter value if it is not None.
        """
        if param_value is None:
            raise ValueError(f"{param_name} cannot be None.")
        return param_value

    @abstractmethod
    def get_question_generation_prompt(
        self,
        *,
        source: Literal["prediction", "reference"],
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> str:
        """
        Constructs the prompt for generating the questions for the model output.

        The prompt should instruct the model to generate one question per line,
        ending in exactly one question mark and containing a letter or digit.
        Recognised list prefixes are removed using ``strip_list_prefix``;
        Unicode is normalised to NFC and surrounding whitespace is trimmed.
        Text-identical questions are scored once across both sources in their
        first-occurrence order. Case, internal whitespace, decimals and signed
        numerical values are preserved. Filtering checks syntax, not relevance.
        An empty retained set raises QagsValidationError before any QA calls.

        Args:
            source (Literal["prediction", "reference"]): The source to use for
                generating the questions. Either "prediction" or "reference".
                According to the source, the generated prompt should either use
                the model output or the reference output/input. When
                `answer_comparison_mode` is set to "ternary", the generated
                questions should be answerable with "yes", "no", or "unknown".
            prediction (str, optional): The model output to evaluate.
            input (str, optional): The input to the model. Optional.
            reference (str, optional): The reference output to compare against.
                Optional.
            metadata (dict[str, Any], optional): Additional Inspect AI sample/task
                state metadata. Optional.

        Returns:
            str: The generated prompt.
        """
        ...

    @abstractmethod
    def get_answer_generation_prompt(
        self,
        *,
        source: Literal["prediction", "reference"],
        question: str,
        prediction: str | None = None,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> str:
        """
        Constructs the prompt for generating the answer to a single question.

        Args:
            source (Literal["prediction", "reference"]): The source to use for
                generating the answer. Either "prediction" or "reference".
                According to the source, the generated prompt should either use
                the model output or the reference output/input when asking
                the model to answer the question. When `answer_comparison_mode`
                is set to "ternary", the prompt should instruct the model to
                answer with only "yes", "no", or "unknown". The complete-answer
                parser also accepts true/false, "I don't know", and a single
                balanced quote, emphasis or inline-code wrapper. Explanations,
                multiple labels and nested wrappers fail validation. Otherwise,
                the model should be instructed to give an answer only without
                any further comments.
            prediction (str, optional): The model output to evaluate.
            input (str, optional): The input to the model. Optional.
            reference (str, optional): The reference output to compare against.
                Optional.
            metadata (dict[str, Any], optional): Additional Inspect AI sample/task
                state metadata. Optional.

        Returns:
            str: The generated prompt.
        """
        ...

    def get_answer_comparison_prompt(
        self,
        *,
        question: str,
        prediction_answer: str,
        reference_answer: str,
        input: str | None = None,
        prediction: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> str:
        """
        Constructs the prompt for comparing answers to the generated questions.

        This method is only used when `answer_comparison_mode` is set to "judge".

        Args:
            question (str): The question to compare answers for.
            prediction_answer (str): The answer generated from the model output.
            reference_answer (str): The answer generated from the reference output.
            input (str | None, optional): The input to the model. Optional.
            prediction (str | None, optional): The model output to evaluate. Optional.
            reference (str | None, optional): The reference output to compare against.
                Optional.
            metadata (dict[str, Any] | None, optional): Additional Inspect AI sample/task
                state metadata. Optional.

        Returns:
            str: The generated prompt.
        """
        if self.answer_comparison_mode == "judge":
            raise NotImplementedError(
                "Answer comparison prompt generation is not implemented. "
                "If you want to use QAGS in judge mode, please implement this method."
            )
        assert False, (
            "Should not attempt to generate comparison prompt in non-judge mode."
        )

__init__

__init__(
    answer_comparison_mode: Literal[
        "ternary", "exact", "judge"
    ],
    logprobs: bool = True,
    top_logprobs: int = 20,
    ci: float = 0.1,
    debug: bool = False,
)

Initializes the QAGS configuration.

Parameters:

Name Type Description Default
answer_comparison_mode Literal['ternary', 'exact', 'judge']

The mode for comparing answers. Either "ternary", "exact", or "judge". In "ternary" mode, the model is expected to answer the generated questions with "yes", "no", or "unknown". In other modes, the model may give arbitrary answers, which are either compared in terms of exact match or compared by the model itself.

required
logprobs bool

Whether to use logprobs to compute weighted answers. Can only be used when answer_comparison_mode is set to "judge".

True
top_logprobs int

The number of top log probabilities to consider when computing weighted answers.

20
ci float

The range near the extreme values (0.0 or 1.0) in which to consider the model answer as confident when comparing answers. This only affects the score explanation when answer_comparison_mode is set to "judge". The default value is 0.1, which means that answers with a score of 0.9 or are confident "yes", while answers with a score of 0.1 or lower are confident "no".

0.1
debug bool

Whether to report repeated errors in the log.

False
Source code in evalsense/evaluation/evaluators/qags.py
def __init__(
    self,
    answer_comparison_mode: Literal["ternary", "exact", "judge"],
    logprobs: bool = True,
    top_logprobs: int = 20,
    ci: float = 0.1,
    debug: bool = False,
):
    """
    Initializes the QAGS configuration.

    Args:
        answer_comparison_mode (Literal["ternary", "exact", "judge"]): The mode
            for comparing answers. Either "ternary", "exact", or "judge".
            In "ternary" mode, the model is expected to answer the generated
            questions with "yes", "no", or "unknown". In other modes, the model
            may give arbitrary answers, which are either compared in terms
            of exact match or compared by the model itself.
        logprobs (bool): Whether to use logprobs to compute weighted answers. Can only
            be used when `answer_comparison_mode` is set to "judge".
        top_logprobs (int): The number of top log probabilities to consider
            when computing weighted answers.
        ci (float): The range near the extreme values (0.0 or 1.0) in which
            to consider the model answer as confident when comparing answers.
            This only affects the score explanation when `answer_comparison_mode`
            is set to "judge". The default value is 0.1, which means that
            answers with a score of 0.9 or are confident "yes", while answers
            with a score of 0.1 or lower are confident "no".
        debug (bool): Whether to report repeated errors in the log.
    """
    self.answer_comparison_mode = answer_comparison_mode
    self.logprobs = logprobs
    self.top_logprobs = top_logprobs
    self.ci = ci
    self.debug = debug

enforce_not_none

enforce_not_none(
    param_name: str, param_value: T | None
) -> T

Helper method to enforce that a parameter is not None.

Parameters:

Name Type Description Default
param_name str

The name of the parameter.

required
param_value T | None

The value of the parameter.

required

Raises:

Type Description
ValueError

If the parameter value is None.

Returns:

Name Type Description
T T

The parameter value if it is not None.

Source code in evalsense/evaluation/evaluators/qags.py
def enforce_not_none[T](self, param_name: str, param_value: T | None) -> T:
    """
    Helper method to enforce that a parameter is not None.

    Args:
        param_name (str): The name of the parameter.
        param_value (T | None): The value of the parameter.

    Raises:
        ValueError: If the parameter value is None.

    Returns:
        T: The parameter value if it is not None.
    """
    if param_value is None:
        raise ValueError(f"{param_name} cannot be None.")
    return param_value

get_answer_comparison_prompt

get_answer_comparison_prompt(
    *,
    question: str,
    prediction_answer: str,
    reference_answer: str,
    input: str | None = None,
    prediction: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
) -> str

Constructs the prompt for comparing answers to the generated questions.

This method is only used when answer_comparison_mode is set to "judge".

Parameters:

Name Type Description Default
question str

The question to compare answers for.

required
prediction_answer str

The answer generated from the model output.

required
reference_answer str

The answer generated from the reference output.

required
input str | None

The input to the model. Optional.

None
prediction str | None

The model output to evaluate. Optional.

None
reference str | None

The reference output to compare against. Optional.

None
metadata dict[str, Any] | None

Additional Inspect AI sample/task state metadata. Optional.

None

Returns:

Name Type Description
str str

The generated prompt.

Source code in evalsense/evaluation/evaluators/qags.py
def get_answer_comparison_prompt(
    self,
    *,
    question: str,
    prediction_answer: str,
    reference_answer: str,
    input: str | None = None,
    prediction: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
) -> str:
    """
    Constructs the prompt for comparing answers to the generated questions.

    This method is only used when `answer_comparison_mode` is set to "judge".

    Args:
        question (str): The question to compare answers for.
        prediction_answer (str): The answer generated from the model output.
        reference_answer (str): The answer generated from the reference output.
        input (str | None, optional): The input to the model. Optional.
        prediction (str | None, optional): The model output to evaluate. Optional.
        reference (str | None, optional): The reference output to compare against.
            Optional.
        metadata (dict[str, Any] | None, optional): Additional Inspect AI sample/task
            state metadata. Optional.

    Returns:
        str: The generated prompt.
    """
    if self.answer_comparison_mode == "judge":
        raise NotImplementedError(
            "Answer comparison prompt generation is not implemented. "
            "If you want to use QAGS in judge mode, please implement this method."
        )
    assert False, (
        "Should not attempt to generate comparison prompt in non-judge mode."
    )

get_answer_generation_prompt abstractmethod

get_answer_generation_prompt(
    *,
    source: Literal["prediction", "reference"],
    question: str,
    prediction: str | None = None,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
) -> str

Constructs the prompt for generating the answer to a single question.

Parameters:

Name Type Description Default
source Literal['prediction', 'reference']

The source to use for generating the answer. Either "prediction" or "reference". According to the source, the generated prompt should either use the model output or the reference output/input when asking the model to answer the question. When answer_comparison_mode is set to "ternary", the prompt should instruct the model to answer with only "yes", "no", or "unknown". The complete-answer parser also accepts true/false, "I don't know", and a single balanced quote, emphasis or inline-code wrapper. Explanations, multiple labels and nested wrappers fail validation. Otherwise, the model should be instructed to give an answer only without any further comments.

required
prediction str

The model output to evaluate.

None
input str

The input to the model. Optional.

None
reference str

The reference output to compare against. Optional.

None
metadata dict[str, Any]

Additional Inspect AI sample/task state metadata. Optional.

None

Returns:

Name Type Description
str str

The generated prompt.

Source code in evalsense/evaluation/evaluators/qags.py
@abstractmethod
def get_answer_generation_prompt(
    self,
    *,
    source: Literal["prediction", "reference"],
    question: str,
    prediction: str | None = None,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
) -> str:
    """
    Constructs the prompt for generating the answer to a single question.

    Args:
        source (Literal["prediction", "reference"]): The source to use for
            generating the answer. Either "prediction" or "reference".
            According to the source, the generated prompt should either use
            the model output or the reference output/input when asking
            the model to answer the question. When `answer_comparison_mode`
            is set to "ternary", the prompt should instruct the model to
            answer with only "yes", "no", or "unknown". The complete-answer
            parser also accepts true/false, "I don't know", and a single
            balanced quote, emphasis or inline-code wrapper. Explanations,
            multiple labels and nested wrappers fail validation. Otherwise,
            the model should be instructed to give an answer only without
            any further comments.
        prediction (str, optional): The model output to evaluate.
        input (str, optional): The input to the model. Optional.
        reference (str, optional): The reference output to compare against.
            Optional.
        metadata (dict[str, Any], optional): Additional Inspect AI sample/task
            state metadata. Optional.

    Returns:
        str: The generated prompt.
    """
    ...

get_question_generation_prompt abstractmethod

get_question_generation_prompt(
    *,
    source: Literal["prediction", "reference"],
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
) -> str

Constructs the prompt for generating the questions for the model output.

The prompt should instruct the model to generate one question per line, ending in exactly one question mark and containing a letter or digit. Recognised list prefixes are removed using strip_list_prefix; Unicode is normalised to NFC and surrounding whitespace is trimmed. Text-identical questions are scored once across both sources in their first-occurrence order. Case, internal whitespace, decimals and signed numerical values are preserved. Filtering checks syntax, not relevance. An empty retained set raises QagsValidationError before any QA calls.

Parameters:

Name Type Description Default
source Literal['prediction', 'reference']

The source to use for generating the questions. Either "prediction" or "reference". According to the source, the generated prompt should either use the model output or the reference output/input. When answer_comparison_mode is set to "ternary", the generated questions should be answerable with "yes", "no", or "unknown".

required
prediction str

The model output to evaluate.

required
input str

The input to the model. Optional.

None
reference str

The reference output to compare against. Optional.

None
metadata dict[str, Any]

Additional Inspect AI sample/task state metadata. Optional.

None

Returns:

Name Type Description
str str

The generated prompt.

Source code in evalsense/evaluation/evaluators/qags.py
@abstractmethod
def get_question_generation_prompt(
    self,
    *,
    source: Literal["prediction", "reference"],
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
) -> str:
    """
    Constructs the prompt for generating the questions for the model output.

    The prompt should instruct the model to generate one question per line,
    ending in exactly one question mark and containing a letter or digit.
    Recognised list prefixes are removed using ``strip_list_prefix``;
    Unicode is normalised to NFC and surrounding whitespace is trimmed.
    Text-identical questions are scored once across both sources in their
    first-occurrence order. Case, internal whitespace, decimals and signed
    numerical values are preserved. Filtering checks syntax, not relevance.
    An empty retained set raises QagsValidationError before any QA calls.

    Args:
        source (Literal["prediction", "reference"]): The source to use for
            generating the questions. Either "prediction" or "reference".
            According to the source, the generated prompt should either use
            the model output or the reference output/input. When
            `answer_comparison_mode` is set to "ternary", the generated
            questions should be answerable with "yes", "no", or "unknown".
        prediction (str, optional): The model output to evaluate.
        input (str, optional): The input to the model. Optional.
        reference (str, optional): The reference output to compare against.
            Optional.
        metadata (dict[str, Any], optional): Additional Inspect AI sample/task
            state metadata. Optional.

    Returns:
        str: The generated prompt.
    """
    ...

QagsScoreCalculator

Bases: ScoreCalculator

QAGS score calculator with strict answer validation.

Each unique question has equal weight within a document. Both texts must supply exactly one nonempty answer per question in every comparison mode. Raw question outputs, retained questions and their source(s), answer outputs, and counts of candidate, invalid, duplicate and retained question lines are recorded in score metadata. Deduplication is textual, not semantic.

Ternary and judge-comparison labels use parse_ternary_answer from evalsense.utils.text. It validates the whole response, allowing one balanced formatting wrapper and optional terminal punctuation. Explicit unknown remains distinct from malformed text; judge comparisons must be binary. Errors are translated to QagsValidationError without echoing text.

Ternary Accuracy is the fraction of equal labels, including unknown/unknown. Such agreement is not factual evidence. Coverage and Groundedness are the number of yes/yes matches divided by reference yes answers and prediction yes answers, respectively, or zero when the corresponding denominator is empty. These positive-answer ratios depend on question polarity and do not measure all correctly preserved negative facts (see issue #57).

Exact mode averages case-insensitive, whitespace-trimmed text matches; formatting wrappers are not stripped from free-text answers. Judge mode averages binary comparisons or the existing probability-weighted values. Weighting failures retain the existing logged binary fallback. Judge calibration and log-probability handling are unchanged.

Invalid or incomplete answers raise QagsValidationError. They are neither counted as unknown nor dropped from the question denominator. There are no automatic format retries. In the current pipeline, a scoring exception marks the affected evaluation as failed; other evaluations may continue. Report failures alongside completed scores.

Successful scores include evaluation_protocol="evalsense.qags-derived.v2", comparison mode, question-weighting policy and invalid-answer policy. The factory defaults to mean aggregation across document scores. Changing the validation or question set changes the evaluated protocol; regression tests check implementation behavior, not clinical validity.

Methods:

Name Description
__init__

Initializes the QAGS score calculator.

calculate

This method is not supported for QAGS and will raise an error when called.

calculate_async

Asynchronously computes evaluation scores for QAGS.

Attributes:

Name Type Description
generate_config GenerateConfig

Generation configuration for the model.

Source code in evalsense/evaluation/evaluators/qags.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
class QagsScoreCalculator(ScoreCalculator):
    """QAGS score calculator with strict answer validation.

    Each unique question has equal weight within a document. Both texts must
    supply exactly one nonempty answer per question in every comparison mode.
    Raw question outputs, retained questions and their source(s), answer outputs,
    and counts of candidate, invalid, duplicate and retained question lines are
    recorded in score metadata. Deduplication is textual, not semantic.

    Ternary and judge-comparison labels use ``parse_ternary_answer`` from
    ``evalsense.utils.text``. It validates the whole response, allowing one
    balanced formatting wrapper and optional terminal punctuation. Explicit
    unknown remains distinct from malformed text; judge comparisons must be
    binary. Errors are translated to QagsValidationError without echoing text.

    Ternary Accuracy is the fraction of equal labels, including unknown/unknown.
    Such agreement is not factual evidence. Coverage and Groundedness are the
    number of yes/yes matches divided by reference yes answers and prediction
    yes answers, respectively, or zero when the corresponding denominator is
    empty. These positive-answer ratios depend on question polarity and do not
    measure all correctly preserved negative facts (see issue #57).

    Exact mode averages case-insensitive, whitespace-trimmed text matches;
    formatting wrappers are not stripped from free-text answers. Judge mode
    averages binary comparisons or the existing probability-weighted values.
    Weighting failures retain the existing logged binary fallback. Judge
    calibration and log-probability handling are unchanged.

    Invalid or incomplete answers raise QagsValidationError. They are neither
    counted as unknown nor dropped from the question denominator. There are no
    automatic format retries. In the current pipeline, a scoring exception marks
    the affected evaluation as failed; other evaluations may continue. Report
    failures alongside completed scores.

    Successful scores include ``evaluation_protocol="evalsense.qags-derived.v2"``,
    comparison mode, question-weighting policy and invalid-answer policy. The
    factory defaults to mean aggregation across document scores. Changing the
    validation or question set changes the evaluated protocol; regression tests
    check implementation behavior, not clinical validity.
    """

    _symbol_dict: dict[bool | None, str] = {
        True: "✅",
        False: "❌",
        None: "❓",
    }

    def __init__(
        self,
        model: Model,
        config: QagsConfig,
        name: str = "QAGS",
        debug: bool = False,
    ):
        """
        Initializes the QAGS score calculator.

        Args:
            model (Model): The model to use for evaluation.
            config (QagsConfig): The configuration for the QAGS score calculator.
            name (str): The name of the score calculator. Defaults to "QAGS".
            debug (bool): Whether to report repeated errors in the log.
        """
        if config.answer_comparison_mode not in ("ternary", "exact", "judge"):
            raise ValueError(
                "answer_comparison_mode must be 'ternary', 'exact', or 'judge'."
            )
        self.model = model
        self.config = config
        self.name = name
        self.warned_weighted_answer = False

    @property
    def generate_config(self) -> GenerateConfig:
        """Generation configuration for the model."""
        if self.config.logprobs and self.config.answer_comparison_mode == "judge":
            return GenerateConfig(
                logprobs=self.config.logprobs,
                top_logprobs=self.config.top_logprobs,
            )
        return GenerateConfig()

    @override
    def calculate(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs: dict,
    ) -> Score:
        """This method is not supported for QAGS and will raise an error when called.

        Use `calculate_async` instead.

        Raises:
            NotImplementedError: When called, as synchronous evaluation is not
                supported for QAGS.
        """
        raise NotImplementedError(
            "Synchronous evaluation is not supported for QAGS. "
            "Use calculate_async instead."
        )

    async def _generate_questions(
        self,
        *,
        prediction: str,
        score_metadata: dict[str, Any],
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> list[str]:
        """Generates questions for the model output and reference output.

        Args:
            prediction (str): The model output to evaluate.
            score_metadata (dict[str, Any]): The dictionary for storing metadata
                associated with the evaluation, returned with the score.
            input (str | None, optional): The input to the model. Optional.
            reference (str | None, optional): The reference output to compare against.
                Optional.
            metadata (dict[str, Any] | None, optional): Additional Inspect AI sample/task
                state metadata. Optional.

        Returns:
            list[str]: A list of generated questions.
        """
        # Questions for model outputs
        prediction_questions_prompt = self.config.get_question_generation_prompt(
            source="prediction",
            prediction=prediction,
            input=input,
            reference=reference,
            metadata=metadata,
        )
        # We don't actually need the logprobs until comparing the answers,
        # but the vLLM provider uses the config from the first sample in the batch
        # so we need to use consistent config for all samples.
        prediction_questions_output = await self.model.generate(
            prediction_questions_prompt, config=self.generate_config
        )

        # Questions for reference outputs
        reference_questions_prompt = self.config.get_question_generation_prompt(
            source="reference",
            prediction=prediction,
            input=input,
            reference=reference,
            metadata=metadata,
        )
        reference_questions_output = await self.model.generate(
            reference_questions_prompt, config=self.generate_config
        )
        questions: list[str] = []
        sources: list[list[str]] = []
        seen: dict[str, int] = {}
        invalid_count = duplicate_count = candidate_count = 0
        for source, text in (
            ("prediction", prediction_questions_output.completion),
            ("reference", reference_questions_output.completion),
        ):
            for line in text.splitlines():
                if not line.strip():
                    continue
                candidate_count += 1
                question = unicodedata.normalize("NFC", strip_list_prefix(line))
                if (
                    not question.endswith("?")
                    or question.count("?") != 1
                    or not any(char.isalnum() for char in question[:-1])
                ):
                    invalid_count += 1
                    continue
                # Case and internal whitespace can be significant in units,
                # identifiers or quoted text. Only deduplicate identical questions.
                key = question
                if key in seen:
                    duplicate_count += 1
                    if source not in sources[seen[key]]:
                        sources[seen[key]].append(source)
                    continue
                seen[key] = len(questions)
                questions.append(question)
                sources.append([source])

        score_metadata["question_sources"] = sources
        score_metadata["question_filtering"] = {
            "candidate_lines": candidate_count,
            "invalid_lines": invalid_count,
            "duplicate_questions": duplicate_count,
            "retained_questions": len(questions),
        }

        score_metadata["questions"] = questions
        score_metadata["prediction_questions_prompt"] = prediction_questions_prompt
        score_metadata["reference_questions_prompt"] = reference_questions_prompt
        score_metadata["raw_prediction_questions"] = (
            prediction_questions_output.completion
        )
        score_metadata["raw_reference_questions"] = (
            reference_questions_output.completion
        )

        if not questions:
            raise QagsValidationError(
                "No valid questions were generated; no score is available."
            )
        return questions

    async def _generate_answers(
        self,
        *,
        prediction: str,
        score_metadata: dict[str, Any],
        questions: list[str],
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> tuple[list[str], list[str]]:
        """Generates answers for the model output and reference output.

        Args:
            prediction (str): The model output to evaluate.
            score_metadata (dict[str, Any]): The dictionary for storing metadata
                associated with the evaluation, returned with the score.
            questions (list[str]): The list of questions to generate answers for.
            input (str | None, optional): The input to the model. Optional.
            reference (str | None, optional): The reference output to compare against.
                Optional.
            metadata (dict[str, Any] | None, optional): Additional Inspect AI sample/task
                state metadata. Optional.

        Returns:
            tuple[list[str], list[str]]: A tuple containing two lists of generated
                answers - one for the model output and one for the reference output,
                respectively.
        """
        prediction_answers: list[str] = []
        reference_answers: list[str] = []
        score_metadata["raw_prediction_answers"] = []
        score_metadata["raw_reference_answers"] = []
        score_metadata["prediction_answer_prompts"] = []
        score_metadata["reference_answer_prompts"] = []
        for question in questions:
            prediction_answer_prompt = self.config.get_answer_generation_prompt(
                source="prediction",
                question=question,
                prediction=prediction,
                input=input,
                reference=reference,
                metadata=metadata,
            )
            prediction_answer_output = await self.model.generate(
                prediction_answer_prompt, config=self.generate_config
            )
            prediction_answers.append(prediction_answer_output.completion)

            reference_answer_prompt = self.config.get_answer_generation_prompt(
                source="reference",
                question=question,
                prediction=prediction,
                input=input,
                reference=reference,
                metadata=metadata,
            )
            reference_answer_output = await self.model.generate(
                reference_answer_prompt, config=self.generate_config
            )
            reference_answers.append(reference_answer_output.completion)

            score_metadata["raw_prediction_answers"].append(
                prediction_answer_output.completion
            )
            score_metadata["raw_reference_answers"].append(
                reference_answer_output.completion
            )
            score_metadata["prediction_answer_prompts"].append(prediction_answer_prompt)
            score_metadata["reference_answer_prompts"].append(reference_answer_prompt)

        return prediction_answers, reference_answers

    def _evaluate_ternary_answers(
        self,
        *,
        prediction: str,
        questions: list[str],
        raw_prediction_answers: list[str],
        raw_reference_answers: list[str],
        score_metadata: dict[str, Any],
    ) -> Score:
        """Evaluates the answers using the ternary answer comparison mode.

        Args:
            prediction (str): The model output to evaluate.
            questions (list[str]): The list of questions generated for the model
                output.
            raw_prediction_answers (list[str]): The list of answers generated from
                the model output.
            raw_reference_answers (list[str]): The list of answers generated from
                the reference output.
            score_metadata (dict[str, Any]): The dictionary for storing metadata
                associated with the evaluation, returned with the score.

        Returns:
            Score: The Inspect AI Score object with the calculated result.
        """
        _validate_answers(questions, raw_prediction_answers, raw_reference_answers)
        prediction_answers = _parse_answers(raw_prediction_answers, "prediction")
        reference_answers = _parse_answers(raw_reference_answers, "reference")

        ref_positive = sum([ra is True for ra in reference_answers])
        pred_positive = sum([pa is True for pa in prediction_answers])
        true_positive = sum(
            [
                pa == ra and ra is True
                for pa, ra in zip(prediction_answers, reference_answers)
            ]
        )
        total_correct = sum(
            [pa == ra for pa, ra in zip(prediction_answers, reference_answers)]
        )

        coverage = true_positive / ref_positive if ref_positive > 0 else 0.0
        groundedness = true_positive / pred_positive if pred_positive > 0 else 0.0
        accuracy = (
            total_correct / len(prediction_answers)
            if len(prediction_answers) > 0
            else 0.0
        )

        explanation = "QAGS Evaluation Report\n\n\nMismatched Q&As\n"
        for i, (question, pa, ra) in enumerate(
            zip(questions, prediction_answers, reference_answers)
        ):
            if pa == ra:
                continue
            explanation += (
                f"* [{i}] Q: {question}, PA: {self._symbol_dict.get(pa)}, "
                f"RA: {self._symbol_dict.get(ra)}, "
                f"Match: {self._symbol_dict.get(False)}\n"
            )
        explanation += "\n\nAll Q&As\n"
        for i, (question, pa, ra) in enumerate(
            zip(questions, prediction_answers, reference_answers)
        ):
            explanation += (
                f"* [{i}] Q: {question}, PA: {self._symbol_dict.get(pa)}, "
                f"RA: {self._symbol_dict.get(ra)}, "
                f"Match: {self._symbol_dict.get(pa == ra)}\n"
            )
        explanation += (
            "\n\n"
            + f"Coverage: {coverage:.2f} ({true_positive}/{ref_positive})\n"
            + f"Groundedness: {groundedness:.2f} ({true_positive}/{pred_positive})\n"
            + f"Accuracy: {accuracy:.2f} ({total_correct}/{len(prediction_answers)})"
        )

        score_metadata["prediction_answers"] = prediction_answers
        score_metadata["reference_answers"] = reference_answers
        score_metadata = {"explanation": explanation} | score_metadata

        return Score(
            value={
                f"{self.name} Coverage": coverage,
                f"{self.name} Groundedness": groundedness,
                f"{self.name} Accuracy": accuracy,
            },
            answer=prediction,
            explanation=explanation,
            metadata=score_metadata,
        )

    def _evaluate_exact_answers(
        self,
        *,
        prediction: str,
        questions: list[str],
        raw_prediction_answers: list[str],
        raw_reference_answers: list[str],
        score_metadata: dict[str, Any],
    ) -> Score:
        """Evaluates the answers using the exact answer comparison mode.

        Args:
            prediction (str): The model output to evaluate.
            questions (list[str]): The list of questions generated for the model
                output.
            raw_prediction_answers (list[str]): The list of answers generated from
                the model output.
            raw_reference_answers (list[str]): The list of answers generated from
                the reference output.
            score_metadata (dict[str, Any]): The dictionary for storing metadata
                associated with the evaluation, returned with the score.

        Returns:
            Score: The Inspect AI Score object with the calculated result.
        """
        _validate_answers(questions, raw_prediction_answers, raw_reference_answers)
        prediction_answers = [pa.strip().lower() for pa in raw_prediction_answers]
        reference_answers = [ra.strip().lower() for ra in raw_reference_answers]
        total_correct = sum(
            [pa == ra for pa, ra in zip(prediction_answers, reference_answers)]
        )
        accuracy = (
            total_correct / len(prediction_answers)
            if len(prediction_answers) > 0
            else 0.0
        )

        explanation = "QAGS Evaluation Report\n\n\nMismatched Q&As\n"
        for i, (question, pa, ra) in enumerate(
            zip(questions, prediction_answers, reference_answers)
        ):
            if pa == ra:
                continue
            explanation += (
                f"* [{i}] Q: {question}, PA: {pa}, RA: {ra}, "
                f"Match: {self._symbol_dict.get(False)}\n"
            )
        explanation += "\n\nAll Q&As\n"
        for i, (question, pa, ra) in enumerate(
            zip(questions, prediction_answers, reference_answers)
        ):
            explanation += (
                f"* [{i}] Q: {question}, PA: {pa}, RA: {ra},"
                f"Match: {self._symbol_dict.get(pa == ra)}\n"
            )
        explanation += (
            f"\n\nAccuracy: {accuracy:.2f} ({total_correct}/{len(prediction_answers)})"
        )

        score_metadata["prediction_answers"] = prediction_answers
        score_metadata["reference_answers"] = reference_answers
        score_metadata = {"explanation": explanation} | score_metadata

        return Score(
            value=accuracy,
            answer=prediction,
            explanation=explanation,
            metadata=score_metadata,
        )

    async def _evaluate_judge_answers(
        self,
        prediction: str,
        questions: list[str],
        raw_prediction_answers: list[str],
        raw_reference_answers: list[str],
        score_metadata: dict[str, Any],
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> Score:
        """Evaluates the answers using the judge answer comparison mode.

        Args:
            prediction (str): The model output to evaluate.
            questions (list[str]): The list of questions generated for the model
                output.
            raw_prediction_answers (list[str]): The list of answers generated from
                the model output.
            raw_reference_answers (list[str]): The list of answers generated from
                the reference output.
            score_metadata (dict[str, Any]): The dictionary for storing metadata
                associated with the evaluation, returned with the score.
            input (str | None, optional): The input to the model. Optional.
            reference (str | None, optional): The reference output to compare against.
                Optional.
            metadata (dict[str, Any] | None, optional): Additional Inspect AI sample/task
                state metadata. Optional.

        Returns:
            Score: The Inspect AI Score object with the calculated result.
        """
        _validate_answers(questions, raw_prediction_answers, raw_reference_answers)
        answer_comparisons: list[float] = []
        for index, (question, prediction_answer, reference_answer) in enumerate(
            zip(questions, raw_prediction_answers, raw_reference_answers, strict=True)
        ):
            answer_comparison_prompt = self.config.get_answer_comparison_prompt(
                question=question,
                prediction_answer=prediction_answer,
                reference_answer=reference_answer,
                input=input,
                prediction=prediction,
                reference=reference,
                metadata=metadata,
            )
            answer_comparison_output = await self.model.generate(
                answer_comparison_prompt, config=self.generate_config
            )
            try:
                parsed_comparison = _parse_answer(
                    answer_comparison_output.completion, binary_only=True
                )
            except QagsValidationError as error:
                raise QagsValidationError(
                    f"Invalid comparison at question {index}: {error}"
                ) from None
            # Unknown has already been rejected by binary_only=True.
            answer_comparison = float(parsed_comparison is True)
            if self.config.logprobs:
                try:
                    answer_comparison = extract_weighted_binary_answer(
                        answer_comparison_output
                    )
                except ValueError as e:
                    if not self.warned_weighted_answer or self.config.debug:
                        self.warned_weighted_answer = True

                        error_message = (
                            f"❌  Cannot compute weighted comparison score: {e} "
                            "Falling back to binary comparison."
                        )

                        if not self.config.debug:
                            error_message += (
                                " Further errors will be suppressed "
                                + "(set debug=True to see all errors)."
                            )

                        logger.error(error_message)
            answer_comparisons.append(answer_comparison)

        def to_match_symbol(answer_comparison: float) -> str:
            if answer_comparison > 1 - self.config.ci:
                return self._symbol_dict[True]
            elif answer_comparison < self.config.ci:
                return self._symbol_dict[False]
            else:
                return self._symbol_dict[None]

        accuracy = sum(answer_comparisons) / len(answer_comparisons)

        explanation = "QAGS Evaluation Report\n\n\nMismatched Q&As\n"
        for i, (question, pa, ra, ac) in enumerate(
            zip(
                questions,
                raw_prediction_answers,
                raw_reference_answers,
                answer_comparisons,
            )
        ):
            if ac > 1 - self.config.ci:
                continue
            explanation += (
                f"* [{i}] Q: {question}, PA: {pa}, RA: {ra}, Score: {ac:.2f}, "
                f"Match: {to_match_symbol(ac)}\n"
            )
        explanation += "\n\nAll Q&As\n"
        for i, (question, pa, ra, ac) in enumerate(
            zip(
                questions,
                raw_prediction_answers,
                raw_reference_answers,
                answer_comparisons,
            )
        ):
            explanation += (
                f"* [{i}] Q: {question}, PA: {pa}, RA: {ra}, Score: {ac:.2f}, "
                f"Match: {to_match_symbol(ac)}\n"
            )
        explanation += (
            f"\n\nAccuracy: {accuracy:.2f} "
            + f"({sum(answer_comparisons):.2f}/{len(answer_comparisons)})"
        )

        score_metadata["prediction_answers"] = raw_prediction_answers
        score_metadata["reference_answers"] = raw_reference_answers
        score_metadata["answer_comparisons"] = answer_comparisons
        score_metadata = {"explanation": explanation} | score_metadata

        return Score(
            value=accuracy,
            answer=prediction,
            explanation=explanation,
            metadata=score_metadata,
        )

    @override
    async def calculate_async(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs: dict,
    ) -> Score:
        """Asynchronously computes evaluation scores for QAGS.

        Args:
            prediction (str): The model output to evaluate.
            input (str, optional): The input to the model. Optional.
            reference (str, optional): The reference output to compare against.
                Optional.
            metadata (dict[str, Any], optional): Additional Inspect AI sample/task
                state metadata. Optional.
            **kwargs (dict): Additional keyword arguments specific to the given
                evaluation method.

        Returns:
            Score: The Inspect AI Score object with the calculated result.
        """

        score_metadata: dict[str, Any] = {
            "evaluation_protocol": QAGS_DERIVED_PROTOCOL,
            "answer_comparison_mode": self.config.answer_comparison_mode,
            "question_weighting": "uniform_unique_questions",
            "invalid_answer_policy": "fail_sample",
        }

        all_questions = await self._generate_questions(
            prediction=prediction,
            score_metadata=score_metadata,
            input=input,
            reference=reference,
            metadata=metadata,
        )

        prediction_answers, reference_answers = await self._generate_answers(
            prediction=prediction,
            score_metadata=score_metadata,
            questions=all_questions,
            input=input,
            reference=reference,
            metadata=metadata,
        )

        match self.config.answer_comparison_mode:
            case "ternary":
                return self._evaluate_ternary_answers(
                    prediction=prediction,
                    questions=all_questions,
                    raw_prediction_answers=prediction_answers,
                    raw_reference_answers=reference_answers,
                    score_metadata=score_metadata,
                )
            case "exact":
                return self._evaluate_exact_answers(
                    prediction=prediction,
                    questions=all_questions,
                    raw_prediction_answers=prediction_answers,
                    raw_reference_answers=reference_answers,
                    score_metadata=score_metadata,
                )
            case "judge":
                return await self._evaluate_judge_answers(
                    prediction=prediction,
                    questions=all_questions,
                    raw_prediction_answers=prediction_answers,
                    raw_reference_answers=reference_answers,
                    score_metadata=score_metadata,
                    input=input,
                    reference=reference,
                    metadata=metadata,
                )
            case _:
                raise ValueError(
                    f"Invalid answer comparison mode: {self.config.answer_comparison_mode}. "
                    "Expected one of 'ternary', 'exact', 'judge'."
                )

generate_config property

generate_config: GenerateConfig

Generation configuration for the model.

__init__

__init__(
    model: Model,
    config: QagsConfig,
    name: str = "QAGS",
    debug: bool = False,
)

Initializes the QAGS score calculator.

Parameters:

Name Type Description Default
model Model

The model to use for evaluation.

required
config QagsConfig

The configuration for the QAGS score calculator.

required
name str

The name of the score calculator. Defaults to "QAGS".

'QAGS'
debug bool

Whether to report repeated errors in the log.

False
Source code in evalsense/evaluation/evaluators/qags.py
def __init__(
    self,
    model: Model,
    config: QagsConfig,
    name: str = "QAGS",
    debug: bool = False,
):
    """
    Initializes the QAGS score calculator.

    Args:
        model (Model): The model to use for evaluation.
        config (QagsConfig): The configuration for the QAGS score calculator.
        name (str): The name of the score calculator. Defaults to "QAGS".
        debug (bool): Whether to report repeated errors in the log.
    """
    if config.answer_comparison_mode not in ("ternary", "exact", "judge"):
        raise ValueError(
            "answer_comparison_mode must be 'ternary', 'exact', or 'judge'."
        )
    self.model = model
    self.config = config
    self.name = name
    self.warned_weighted_answer = False

calculate

calculate(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs: dict,
) -> Score

This method is not supported for QAGS and will raise an error when called.

Use calculate_async instead.

Raises:

Type Description
NotImplementedError

When called, as synchronous evaluation is not supported for QAGS.

Source code in evalsense/evaluation/evaluators/qags.py
@override
def calculate(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs: dict,
) -> Score:
    """This method is not supported for QAGS and will raise an error when called.

    Use `calculate_async` instead.

    Raises:
        NotImplementedError: When called, as synchronous evaluation is not
            supported for QAGS.
    """
    raise NotImplementedError(
        "Synchronous evaluation is not supported for QAGS. "
        "Use calculate_async instead."
    )

calculate_async async

calculate_async(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs: dict,
) -> Score

Asynchronously computes evaluation scores for QAGS.

Parameters:

Name Type Description Default
prediction str

The model output to evaluate.

required
input str

The input to the model. Optional.

None
reference str

The reference output to compare against. Optional.

None
metadata dict[str, Any]

Additional Inspect AI sample/task state metadata. Optional.

None
**kwargs dict

Additional keyword arguments specific to the given evaluation method.

{}

Returns:

Name Type Description
Score Score

The Inspect AI Score object with the calculated result.

Source code in evalsense/evaluation/evaluators/qags.py
@override
async def calculate_async(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs: dict,
) -> Score:
    """Asynchronously computes evaluation scores for QAGS.

    Args:
        prediction (str): The model output to evaluate.
        input (str, optional): The input to the model. Optional.
        reference (str, optional): The reference output to compare against.
            Optional.
        metadata (dict[str, Any], optional): Additional Inspect AI sample/task
            state metadata. Optional.
        **kwargs (dict): Additional keyword arguments specific to the given
            evaluation method.

    Returns:
        Score: The Inspect AI Score object with the calculated result.
    """

    score_metadata: dict[str, Any] = {
        "evaluation_protocol": QAGS_DERIVED_PROTOCOL,
        "answer_comparison_mode": self.config.answer_comparison_mode,
        "question_weighting": "uniform_unique_questions",
        "invalid_answer_policy": "fail_sample",
    }

    all_questions = await self._generate_questions(
        prediction=prediction,
        score_metadata=score_metadata,
        input=input,
        reference=reference,
        metadata=metadata,
    )

    prediction_answers, reference_answers = await self._generate_answers(
        prediction=prediction,
        score_metadata=score_metadata,
        questions=all_questions,
        input=input,
        reference=reference,
        metadata=metadata,
    )

    match self.config.answer_comparison_mode:
        case "ternary":
            return self._evaluate_ternary_answers(
                prediction=prediction,
                questions=all_questions,
                raw_prediction_answers=prediction_answers,
                raw_reference_answers=reference_answers,
                score_metadata=score_metadata,
            )
        case "exact":
            return self._evaluate_exact_answers(
                prediction=prediction,
                questions=all_questions,
                raw_prediction_answers=prediction_answers,
                raw_reference_answers=reference_answers,
                score_metadata=score_metadata,
            )
        case "judge":
            return await self._evaluate_judge_answers(
                prediction=prediction,
                questions=all_questions,
                raw_prediction_answers=prediction_answers,
                raw_reference_answers=reference_answers,
                score_metadata=score_metadata,
                input=input,
                reference=reference,
                metadata=metadata,
            )
        case _:
            raise ValueError(
                f"Invalid answer comparison mode: {self.config.answer_comparison_mode}. "
                "Expected one of 'ternary', 'exact', 'judge'."
            )

QagsScorerFactory

Bases: ScorerFactory

Scorer factory for the QAGS QA agreement metric.

Methods:

Name Description
__init__

Initialize the QAGS scorer factory.

create_scorer

Creates a QAGS scorer.

Source code in evalsense/evaluation/evaluators/qags.py
class QagsScorerFactory(ScorerFactory):
    """Scorer factory for the QAGS QA agreement metric."""

    def __init__(
        self,
        name: str,
        config: QagsConfig,
        metrics: list[Metric | dict[str, list[Metric]]]
        | dict[str, list[Metric]]
        | None = None,
    ):
        """
        Initialize the QAGS scorer factory.

        Args:
            config (QagsConfig): The configuration for the QAGS scorer.
            metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
                The metrics to use for the evaluation. If `None`, the default metric
                will be used for the configured answer comparison mode.
        """
        self.name = name
        self.config = config
        if metrics is None:
            if self.config.answer_comparison_mode == "ternary":
                metrics = [
                    {
                        f"{name} Coverage": [mean()],
                        f"{name} Groundedness": [mean()],
                        f"{name} Accuracy": [mean()],
                    }
                ]
            else:
                metrics = [mean()]
        self.metrics = metrics

    @override
    def create_scorer(self, model: Model) -> Scorer:
        """
        Creates a QAGS scorer.

        Args:
            model (Model): The model to create a scorer for.

        Returns:
            Scorer: The created QAGS scorer.
        """

        @scorer(name=self.name, metrics=self.metrics)
        def qags_scorer() -> Scorer:
            qags_score_calculator = QagsScoreCalculator(
                model=model,
                config=self.config,
                name=self.name,
            )

            async def score(state: TaskState, target: Target):
                return await qags_score_calculator.calculate_async(
                    input=state.input_text,
                    prediction=state.output.completion,
                    reference=target.text,
                    metadata=state.metadata,
                )

            return score

        return qags_scorer()

__init__

__init__(
    name: str,
    config: QagsConfig,
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
)

Initialize the QAGS scorer factory.

Parameters:

Name Type Description Default
config QagsConfig

The configuration for the QAGS scorer.

required
metrics list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None

The metrics to use for the evaluation. If None, the default metric will be used for the configured answer comparison mode.

None
Source code in evalsense/evaluation/evaluators/qags.py
def __init__(
    self,
    name: str,
    config: QagsConfig,
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
):
    """
    Initialize the QAGS scorer factory.

    Args:
        config (QagsConfig): The configuration for the QAGS scorer.
        metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
            The metrics to use for the evaluation. If `None`, the default metric
            will be used for the configured answer comparison mode.
    """
    self.name = name
    self.config = config
    if metrics is None:
        if self.config.answer_comparison_mode == "ternary":
            metrics = [
                {
                    f"{name} Coverage": [mean()],
                    f"{name} Groundedness": [mean()],
                    f"{name} Accuracy": [mean()],
                }
            ]
        else:
            metrics = [mean()]
    self.metrics = metrics

create_scorer

create_scorer(model: Model) -> Scorer

Creates a QAGS scorer.

Parameters:

Name Type Description Default
model Model

The model to create a scorer for.

required

Returns:

Name Type Description
Scorer Scorer

The created QAGS scorer.

Source code in evalsense/evaluation/evaluators/qags.py
@override
def create_scorer(self, model: Model) -> Scorer:
    """
    Creates a QAGS scorer.

    Args:
        model (Model): The model to create a scorer for.

    Returns:
        Scorer: The created QAGS scorer.
    """

    @scorer(name=self.name, metrics=self.metrics)
    def qags_scorer() -> Scorer:
        qags_score_calculator = QagsScoreCalculator(
            model=model,
            config=self.config,
            name=self.name,
        )

        async def score(state: TaskState, target: Target):
            return await qags_score_calculator.calculate_async(
                input=state.input_text,
                prediction=state.output.completion,
                reference=target.text,
                metadata=state.metadata,
            )

        return score

    return qags_scorer()

QagsValidationError

Bases: ValueError

Generated questions or answers cannot support a valid score.

Source code in evalsense/evaluation/evaluators/qags.py
class QagsValidationError(ValueError):
    """Generated questions or answers cannot support a valid score."""

RougeScoreCalculator

Bases: ScoreCalculator

Calculator for computing ROUGE scores.

Methods:

Name Description
calculate

Calculates ROUGE scores for the supplied model prediction and reference input.

calculate_async

Calculates ROUGE scores for the supplied model prediction and reference input.

Source code in evalsense/evaluation/evaluators/rouge.py
class RougeScoreCalculator(ScoreCalculator):
    """Calculator for computing ROUGE scores."""

    def __init__(self):
        self.implementation = MetricImplementation.resolve("rouge")
        self.rouge_module = self.implementation.load()

    @override
    def calculate(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs: dict,
    ) -> Score:
        """
        Calculates ROUGE scores for the supplied model prediction and reference input.

        Args:
            prediction (str): The text of the prediction from the model.
            input (str, optional): The text of the input to the model. Ignored for ROUGE.
            reference (str, optional): The text of the reference input to compare against.
            metadata (dict[str, Any], optional): Additional metadata for the score.
                Ignored for ROUGE.

        Returns:
            Score: Inspect AI Score with the calculated evaluation results.
        """
        if reference is None:
            raise ValueError("Reference is required for computing ROUGE, but was None.")

        predictions = [prediction]
        references = [reference]

        result = self.rouge_module.compute(
            predictions=predictions, references=references
        )
        return Score(
            value={
                "ROUGE-1": result["rouge1"],  # type: ignore
                "ROUGE-2": result["rouge2"],  # type: ignore
                "ROUGE-L": result["rougeL"],  # type: ignore
            },
            answer=prediction,
            metadata={"metric_provenance": self.implementation.provenance},
        )

    @override
    async def calculate_async(
        self,
        *,
        prediction: str,
        input: str | None = None,
        reference: str | None = None,
        metadata: dict[str, Any] | None = None,
        **kwargs: dict,
    ) -> Score:
        """
        Calculates ROUGE scores for the supplied model prediction and reference input.

        Args:
            prediction (str): The text of the prediction from the model.
            input (str, optional): The text of the input to the model. Ignored for ROUGE.
            reference (str, optional): The text of the reference input to compare against.
            metadata (dict[str, Any], optional): Additional metadata for the score.
                Ignored for ROUGE.

        Returns:
            Score: Inspect AI Score with the calculated evaluation results.
        """
        return self.calculate(
            prediction=prediction,
            input=input,
            reference=reference,
            metadata=metadata,
            **kwargs,
        )

calculate

calculate(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs: dict,
) -> Score

Calculates ROUGE scores for the supplied model prediction and reference input.

Parameters:

Name Type Description Default
prediction str

The text of the prediction from the model.

required
input str

The text of the input to the model. Ignored for ROUGE.

None
reference str

The text of the reference input to compare against.

None
metadata dict[str, Any]

Additional metadata for the score. Ignored for ROUGE.

None

Returns:

Name Type Description
Score Score

Inspect AI Score with the calculated evaluation results.

Source code in evalsense/evaluation/evaluators/rouge.py
@override
def calculate(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs: dict,
) -> Score:
    """
    Calculates ROUGE scores for the supplied model prediction and reference input.

    Args:
        prediction (str): The text of the prediction from the model.
        input (str, optional): The text of the input to the model. Ignored for ROUGE.
        reference (str, optional): The text of the reference input to compare against.
        metadata (dict[str, Any], optional): Additional metadata for the score.
            Ignored for ROUGE.

    Returns:
        Score: Inspect AI Score with the calculated evaluation results.
    """
    if reference is None:
        raise ValueError("Reference is required for computing ROUGE, but was None.")

    predictions = [prediction]
    references = [reference]

    result = self.rouge_module.compute(
        predictions=predictions, references=references
    )
    return Score(
        value={
            "ROUGE-1": result["rouge1"],  # type: ignore
            "ROUGE-2": result["rouge2"],  # type: ignore
            "ROUGE-L": result["rougeL"],  # type: ignore
        },
        answer=prediction,
        metadata={"metric_provenance": self.implementation.provenance},
    )

calculate_async async

calculate_async(
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs: dict,
) -> Score

Calculates ROUGE scores for the supplied model prediction and reference input.

Parameters:

Name Type Description Default
prediction str

The text of the prediction from the model.

required
input str

The text of the input to the model. Ignored for ROUGE.

None
reference str

The text of the reference input to compare against.

None
metadata dict[str, Any]

Additional metadata for the score. Ignored for ROUGE.

None

Returns:

Name Type Description
Score Score

Inspect AI Score with the calculated evaluation results.

Source code in evalsense/evaluation/evaluators/rouge.py
@override
async def calculate_async(
    self,
    *,
    prediction: str,
    input: str | None = None,
    reference: str | None = None,
    metadata: dict[str, Any] | None = None,
    **kwargs: dict,
) -> Score:
    """
    Calculates ROUGE scores for the supplied model prediction and reference input.

    Args:
        prediction (str): The text of the prediction from the model.
        input (str, optional): The text of the input to the model. Ignored for ROUGE.
        reference (str, optional): The text of the reference input to compare against.
        metadata (dict[str, Any], optional): Additional metadata for the score.
            Ignored for ROUGE.

    Returns:
        Score: Inspect AI Score with the calculated evaluation results.
    """
    return self.calculate(
        prediction=prediction,
        input=input,
        reference=reference,
        metadata=metadata,
        **kwargs,
    )

bleu_metric

bleu_metric(
    *, implementation: MetricImplementation | None = None
) -> MetricProtocol

Base metric for BLEU scores.

Parameters:

Name Type Description Default
implementation MetricImplementation | None

Pinned implementation shared with the sample scorer. Defaults to the supported BLEU implementation.

None

Rejects input scores with absent or different implementation provenance. Historical scores must be rescored before recomputing this corpus metric.

Returns:

Name Type Description
MetricProtocol MetricProtocol

A function that computes BLEU scores.

Source code in evalsense/evaluation/evaluators/bleu.py
def bleu_metric(
    *, implementation: MetricImplementation | None = None
) -> MetricProtocol:
    """
    Base metric for BLEU scores.

    Args:
        implementation (MetricImplementation | None): Pinned implementation shared
            with the sample scorer. Defaults to the supported BLEU implementation.

    Rejects input scores with absent or different implementation provenance.
    Historical scores must be rescored before recomputing this corpus metric.

    Returns:
        MetricProtocol: A function that computes BLEU scores.
    """

    implementation = implementation or MetricImplementation.resolve("bleu")

    def metric(scores: list[SampleScore]) -> Value:
        if any(
            (score.score.metadata or {}).get("metric_provenance")
            != implementation.provenance
            for score in scores
        ):
            raise ValueError(
                "Corpus BLEU requires scores from the same recorded metric implementation."
            )
        bleu_module = implementation.load()
        predictions = [score.score.metadata["prediction"] for score in scores]  # type: ignore
        references = [score.score.metadata["reference"] for score in scores]  # type: ignore
        result = bleu_module.compute(
            predictions=predictions, references=references, max_order=4, smooth=False
        )
        result = cast(dict[str, Any], result)
        return result["bleu"]

    return metric

get_bertscore_evaluator

get_bertscore_evaluator(
    *,
    name: str = "BERTScore",
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    model_type: str = "microsoft/deberta-xlarge-mnli",
    lang: str = "en",
    num_layers: int | None = None,
    verbose: bool = False,
    idf: bool = False,
    device: str | None = None,
    batch_size: int = 64,
    nthreads: int = 1,
    rescale_with_baseline: bool = False,
    baseline_path: str | None = None,
    use_fast_tokenizer: bool = False,
    version: str | None = None,
) -> Evaluator

Returns a BERTScore evaluator.

Both texts are checked against any finite tokenizer/model limit, including special tokens. XLNet's explicit no-fixed-limit declaration is supported; unknown configurations still fail. See BertScoreCalculator for the length policy and backend constraints. No automatic truncation is performed.

Parameters:

Name Type Description Default
name str

The name of the evaluator and scorer. Defaults to "BERTScore".

'BERTScore'
metrics list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None

The metrics to use for the evaluation. If None, the default metrics will be used (BERTScore Precision, Recall, and F1 with mean aggregation).

None
model_type str

The model type to use for computing BERTScore. Defaults to "microsoft/deberta-xlarge-mnli", the currently best-performing model according to BERTScore authors.

'microsoft/deberta-xlarge-mnli'
lang str

The language of the text. Defaults to "en".

'en'
num_layers int | None

The layer of representations to use. The default is the number of layers tuned on WMT16 correlation data, which depends on the model_type used.

None
verbose bool

Whether to turn on verbose mode. Defaults to False

False
idf bool

Must be False because this evaluator scores one sample at a time. Automatic IDF and precomputed IDF dictionaries are unsupported. Defaults to False (no IDF weighting).

False
device str | None

The device to use for computing the contextual embeddings. If this argument is not set or None, the model will be loaded on cuda:0 if available.

None
nthreads int

The number of threads to use for computing the contextual embeddings. Defaults to 1.

1
batch_size int

The batch size to use for computing the contextual embeddings. Defaults to 64.

64
rescale_with_baseline bool

Whether to rescale the BERTScore with pre-computed baseline. The default value is False.

False
baseline_path str | None

Customized baseline file.

None
use_fast_tokenizer bool

The use_fast parameter passed to HF tokenizer. Defaults to False.

False
version str | None

Cache version for custom code or external resources.

None

Returns:

Name Type Description
Evaluator Evaluator

The BERTScore evaluator.

Raises:

Type Description
ValueError

If idf is not False. Use a corpus-aware BERTScore scorer when IDF weighting is required.

Source code in evalsense/evaluation/evaluators/bertscore.py
def get_bertscore_evaluator(
    *,
    name: str = "BERTScore",
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    model_type: str = "microsoft/deberta-xlarge-mnli",
    lang: str = "en",
    num_layers: int | None = None,
    verbose: bool = False,
    idf: bool = False,
    device: str | None = None,
    batch_size: int = 64,
    nthreads: int = 1,
    rescale_with_baseline: bool = False,
    baseline_path: str | None = None,
    use_fast_tokenizer: bool = False,
    version: str | None = None,
) -> Evaluator:
    """
    Returns a BERTScore evaluator.

    Both texts are checked against any finite tokenizer/model limit, including
    special tokens. XLNet's explicit no-fixed-limit declaration is supported;
    unknown configurations still fail. See BertScoreCalculator for the length
    policy and backend constraints. No automatic truncation is performed.

    Args:
        name (str): The name of the evaluator and scorer. Defaults to "BERTScore".
        metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
            The metrics to use for the evaluation. If `None`, the default metrics
            will be used (BERTScore Precision, Recall, and F1 with mean aggregation).
        model_type (str, optional): The model type to use for computing BERTScore.
            Defaults to "microsoft/deberta-xlarge-mnli", the currently best-performing
            model according to BERTScore authors.
        lang (str, optional): The language of the text. Defaults to "en".
        num_layers (int | None, optional): The layer of representations to use. The
            default is the number of layers tuned on WMT16 correlation data, which
            depends on the `model_type` used.
        verbose (bool, optional): Whether to turn on verbose mode. Defaults to `False`
        idf (bool, optional): Must be False because this evaluator scores one
            sample at a time. Automatic IDF and precomputed IDF dictionaries are
            unsupported. Defaults to False (no IDF weighting).
        device (str | None, optional): The device to use for computing the contextual
            embeddings. If this argument is not set or `None`, the model will be
            loaded on `cuda:0` if available.
        nthreads (int, optional): The number of threads to use for computing the
            contextual embeddings. Defaults to `1`.
        batch_size (int, optional): The batch size to use for computing the
            contextual embeddings. Defaults to `64`.
        rescale_with_baseline (bool, optional): Whether to rescale the BERTScore with
            pre-computed baseline. The default value is `False`.
        baseline_path (str | None, optional): Customized baseline file.
        use_fast_tokenizer (bool, optional): The `use_fast` parameter passed to HF
            tokenizer. Defaults to `False`.

        version (str | None): Cache version for custom code or external resources.

    Returns:
        Evaluator: The BERTScore evaluator.

    Raises:
        ValueError: If idf is not False. Use a corpus-aware BERTScore scorer
            when IDF weighting is required.
    """
    cache_parameters = {
        "method": "bertscore",
        "metrics": metrics,
        "model_type": model_type,
        "lang": lang,
        "num_layers": num_layers,
        "idf": idf,
        "device": device,
        "batch_size": batch_size,
        "rescale_with_baseline": rescale_with_baseline,
        "baseline_path": baseline_path,
        "use_fast_tokenizer": use_fast_tokenizer,
    }
    # Progress logging never changes scores. IDF worker count is irrelevant when
    # IDF is disabled; device and batch size still identify numerical execution.
    if idf is not False:
        cache_parameters["nthreads"] = nthreads
    if metrics is None:
        metrics = [
            {"BERTScore Precision": [mean()]},
            {"BERTScore Recall": [mean()]},
            {"BERTScore F1": [mean()]},
        ]

    calculator = BertScoreCalculator(
        model_type=model_type,
        lang=lang,
        num_layers=num_layers,
        idf=idf,
    )

    async def init_bertscore() -> None:
        async with concurrency("init_bertscore", 1):
            if not hasattr(calculator, "bertscore_module"):
                setattr(
                    calculator,
                    "bertscore_module",
                    calculator.implementation.load(),
                )

    def cleanup_bertscore() -> None:
        import torch

        del calculator.bertscore_module
        calculator._length_context = None
        gc.collect()
        torch.cuda.empty_cache()

    @scorer(
        name=name,
        metrics=metrics,
    )
    def bertscore_scorer() -> Scorer:
        async def score(state: TaskState, target: Target) -> Score:
            await init_bertscore()

            return await calculator.calculate_async(
                prediction=state.output.completion,
                reference=target.text,
                verbose=verbose,
                device=device,
                batch_size=batch_size,
                nthreads=nthreads,
                rescale_with_baseline=rescale_with_baseline,
                baseline_path=baseline_path,
                use_fast_tokenizer=use_fast_tokenizer,
            )

        return score

    return Evaluator(
        version=version,
        cache_parameters=cache_parameters,
        name=name,
        scorer=bertscore_scorer(),
        cleanup_fun=cleanup_bertscore,
        implementation=calculator.implementation,
    )

get_bleu_evaluator

get_bleu_evaluator(
    name: str = "BLEU",
    scorer_name: str = "Sentence BLEU",
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    *,
    version: str | None = None,
) -> Evaluator

Return sentence BLEU-4 scores with a separately labelled corpus BLEU metric.

Correlation and meta-evaluation consume unsmoothed sentence BLEU-4. The default aggregate is <name> (corpus), recomputed from the input texts; it is not a mean sentence score. Supply metrics=[mean()] to request the latter instead. See BleuScoreCalculator for tokenization and short-input behavior.

Re-score under a fresh evaluator name or project when migrating from legacy unigram scores. Existing cached logs are not converted or invalidated here.

Parameters:

Name Type Description Default
name str

The evaluator name and prefix for the corpus metric. Defaults to "BLEU".

'BLEU'
scorer_name str

The name of the internal scorer. Defaults to "Sentence BLEU".

'Sentence BLEU'
metrics list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None

The metrics to use for the evaluation. If None, the default metric will be used (unsmoothed corpus BLEU-4).

None
version str | None

Cache version for custom code or external resources.

None

Returns:

Name Type Description
Evaluator Evaluator

An evaluator for BLEU scores.

Source code in evalsense/evaluation/evaluators/bleu.py
def get_bleu_evaluator(
    name: str = "BLEU",
    scorer_name: str = "Sentence BLEU",
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    *,
    version: str | None = None,
) -> Evaluator:
    """
    Return sentence BLEU-4 scores with a separately labelled corpus BLEU metric.

    Correlation and meta-evaluation consume unsmoothed sentence BLEU-4. The default
    aggregate is ``<name> (corpus)``, recomputed from the input texts; it is not a
    mean sentence score. Supply ``metrics=[mean()]`` to request the latter instead.
    See BleuScoreCalculator for tokenization and short-input behavior.

    Re-score under a fresh evaluator name or project when migrating from legacy
    unigram scores. Existing cached logs are not converted or invalidated here.

    Args:
        name (str): The evaluator name and prefix for the corpus metric. Defaults to "BLEU".
        scorer_name (str): The name of the internal scorer. Defaults to "Sentence BLEU".
        metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
            The metrics to use for the evaluation. If `None`, the default metric
            will be used (unsmoothed corpus BLEU-4).

        version (str | None): Cache version for custom code or external resources.

    Returns:
        Evaluator: An evaluator for BLEU scores.
    """
    cache_parameters = {
        "method": "bleu",
        "scorer_name": scorer_name,
        "metrics": metrics,
    }

    bleu_calculator = BleuScoreCalculator()

    @metric(name=f"{name} (corpus)")
    def bleu() -> MetricProtocol:
        return bleu_metric(implementation=bleu_calculator.implementation)

    if metrics is None:
        metrics = [bleu()]

    @scorer(name=scorer_name, metrics=metrics)
    def sentence_bleu_scorer() -> Scorer:
        async def score(state: TaskState, target: Target):
            return await bleu_calculator.calculate_async(
                prediction=state.output.completion, reference=target.text
            )

        return score

    return Evaluator(
        name,
        version=version,
        cache_parameters=cache_parameters,
        scorer=sentence_bleu_scorer(),
        implementation=bleu_calculator.implementation,
    )

get_g_eval_evaluator

get_g_eval_evaluator(
    *,
    name: str = "G-Eval",
    quality_name: str = "Unknown",
    model_name: str | None = None,
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    prompt_template: str,
    model_config: ModelConfig,
    logprobs: bool = True,
    top_logprobs: int = 20,
    min_score: int = 1,
    max_score: int = 10,
    normalise: bool = True,
    debug: bool = False,
    version: str | None = None,
) -> Evaluator

Constructs a G-Eval evaluator that can be used in EvalSense evaluation pipeline.

Parameters:

Name Type Description Default
name str

The name of the evaluator. Defaults to "G-Eval".

'G-Eval'
quality_name str

The name of the quality to be evaluated by G-Eval.

'Unknown'
model_name str | None

The name of the model to be used for evaluation. If None, the model name will be taken from the model config.

None
metrics list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None

The metrics to use for the evaluation. If None, the default metric will be used (G-Eval).

None
prompt_template str

The prompt template to use. The supplied template should be a format string with {prediction} and (optionally) {reference} as placeholders, as well as any additional placeholders for entries in Inspect AI sample/task state metadata. The template should instruct the judge model to return only one non-negative integer between min_score and max_score. Fractions, decimals, signs and explanatory text fail validation. Outer whitespace is accepted. Leading zeroes are accepted only when logprobs=False; weighted mode requires canonical labels.

required
model_config ModelConfig

The model configuration.

required
logprobs bool

Use a one-token conditional mean over reported valid rating alternatives, requiring evidence for every allowed rating. Missing evidence raises ValueError without a sampled-score fallback. False explicitly uses the sampled integer.

True
top_logprobs int

Positive integer alternative count, at least the number of allowed ratings in weighted mode. This budget does not guarantee the provider returns every rating.

20
min_score int

The minimum valid score.

1
max_score int

The maximum valid score.

10
normalise bool

Whether to normalise the scores between 0 and 1.

True
debug bool

Retained for compatibility. Weighting failures always raise.

False
version str | None

Cache version for custom code or external resources.

None

Returns:

Name Type Description
Evaluator Evaluator

The constructed G-Eval evaluator. Cache identity includes the estimator format and, for weighted mode, the weighting policy.

Source code in evalsense/evaluation/evaluators/g_eval.py
def get_g_eval_evaluator(
    *,
    name: str = "G-Eval",
    quality_name: str = "Unknown",
    model_name: str | None = None,
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    prompt_template: str,
    model_config: ModelConfig,
    logprobs: bool = True,
    top_logprobs: int = 20,
    min_score: int = 1,
    max_score: int = 10,
    normalise: bool = True,
    debug: bool = False,
    version: str | None = None,
) -> Evaluator:
    """
    Constructs a G-Eval evaluator that can be used in EvalSense evaluation pipeline.

    Args:
        name (str): The name of the evaluator. Defaults to "G-Eval".
        quality_name (str): The name of the quality to be evaluated by G-Eval.
        model_name (str | None): The name of the model to be used for evaluation.
            If `None`, the model name will be taken from the model config.
        metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
            The metrics to use for the evaluation. If `None`, the default metric
            will be used (G-Eval).
        prompt_template (str): The prompt template to use. The supplied template should
            be a format string with {prediction} and (optionally) {reference} as
            placeholders, as well as any additional placeholders for entries in
            Inspect AI sample/task state metadata. The template should instruct the
            judge model to return only one non-negative integer between min_score
            and max_score. Fractions, decimals, signs and explanatory text fail
            validation. Outer whitespace is accepted. Leading zeroes are accepted
            only when logprobs=False; weighted mode requires canonical labels.
        model_config (ModelConfig): The model configuration.
        logprobs (bool): Use a one-token conditional mean over reported valid
            rating alternatives, requiring evidence for every allowed rating.
            Missing evidence raises ValueError without a sampled-score fallback.
            False explicitly uses the sampled integer.
        top_logprobs (int): Positive integer alternative count, at least the
            number of allowed ratings in weighted mode. This budget does not
            guarantee the provider returns every rating.
        min_score (int): The minimum valid score.
        max_score (int): The maximum valid score.
        normalise (bool): Whether to normalise the scores between 0 and 1.
        debug (bool): Retained for compatibility. Weighting failures always raise.

        version (str | None): Cache version for custom code or external resources.

    Returns:
        Evaluator: The constructed G-Eval evaluator. Cache identity includes the
            estimator format and, for weighted mode, the weighting policy.
    """
    cache_parameters = {
        "method": "g_eval",
        "score_format": SCORE_FORMAT,
        "estimator_format": ESTIMATOR_FORMAT,
        "quality_name": quality_name,
        "metrics": metrics,
        "prompt_template": prompt_template,
        "logprobs": logprobs,
        "top_logprobs": top_logprobs,
        "min_score": min_score,
        "max_score": max_score,
        "normalise": normalise,
        "debug": debug,
    }
    if logprobs:
        cache_parameters["weighting_policy"] = WEIGHTING_POLICY
    metric_name = f"{name} ({quality_name}, {model_name or model_config.name})"
    return Evaluator(
        version=version,
        cache_parameters=cache_parameters,
        name=metric_name,
        scorer=GEvalScorerFactory(
            name=metric_name,
            metrics=metrics,
            prompt_template=prompt_template,
            logprobs=logprobs,
            top_logprobs=top_logprobs,
            min_score=min_score,
            max_score=max_score,
            normalise=normalise,
            debug=debug,
        ),
        model_config=model_config,
    )

get_qags_evaluator

get_qags_evaluator(
    *,
    config: QagsConfig,
    name: str = "QAGS",
    model_name: str | None = None,
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    model_config: ModelConfig,
    version: str | None = None,
) -> Evaluator

Constructs a QAGS evaluator for EvalSense.

The configured prompts select the two comparison texts and question types. See QagsConfig for the method and QagsScoreCalculator for score definitions, accepted formats and failure behavior. The protocol is recorded in score metadata. Migration requirements are in the changelog.

Parameters:

Name Type Description Default
config QagsConfig

The configuration for the QAGS evaluator.

required
name str

The name of the QAGS evaluator.

'QAGS'
model_name str | None

The name of the model to use for evaluation. If None, the name from the model configuration will be used.

None
metrics list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None

The metrics to use for the evaluation. If None, the default metrics will be used (coverage, groundedness and accuracy in ternary mode; answer agreement in exact and judge modes).

None
model_config ModelConfig

The configuration of the model to be used for evaluation.

required
version str | None

Cache version for custom code or external resources.

None

Returns:

Name Type Description
Evaluator Evaluator

The constructed QAGS evaluator.

Source code in evalsense/evaluation/evaluators/qags.py
def get_qags_evaluator(
    *,
    config: QagsConfig,
    name: str = "QAGS",
    model_name: str | None = None,
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    model_config: ModelConfig,
    version: str | None = None,
) -> Evaluator:
    """
    Constructs a QAGS evaluator for EvalSense.

    The configured prompts select the two comparison texts and question types.
    See QagsConfig for the method and QagsScoreCalculator for score definitions,
    accepted formats and failure behavior. The protocol is recorded in score
    metadata. Migration requirements are in the changelog.

    Args:
        config (QagsConfig): The configuration for the QAGS evaluator.
        name (str): The name of the QAGS evaluator.
        model_name (str | None): The name of the model to use for evaluation.
            If `None`, the name from the model configuration will be used.
        metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
            The metrics to use for the evaluation. If `None`, the default metrics
            will be used (coverage, groundedness and accuracy in ternary mode;
            answer agreement in exact and judge modes).
        model_config (ModelConfig): The configuration of the model to be used
            for evaluation.

        version (str | None): Cache version for custom code or external resources.

    Returns:
        Evaluator: The constructed QAGS evaluator.
    """
    cache_parameters = {
        "method": "qags",
        "config": {"implementation": type(config), "parameters": vars(config)},
        "metrics": metrics,
    }
    metric_name = f"{name} ({model_name or model_config.name})"
    return Evaluator(
        version=version,
        cache_parameters=cache_parameters,
        name=metric_name,
        scorer=QagsScorerFactory(
            name=metric_name,
            config=config,
            metrics=metrics,
        ),
        model_config=model_config,
    )

get_rouge_evaluator

get_rouge_evaluator(
    name: str = "ROUGE",
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    *,
    version: str | None = None,
) -> Evaluator

Returns an evaluator for ROUGE scores.

Parameters:

Name Type Description Default
name str

The name of the evaluator. Defaults to "ROUGE".

'ROUGE'
metrics list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None

The metrics to use for evaluation. If None, defaults to ROUGE-1, ROUGE-2, and ROUGE-L with mean aggregation.

None
version str | None

Cache version for custom code or external resources.

None

Returns:

Name Type Description
Evaluator Evaluator

An evaluator for ROUGE scores.

Source code in evalsense/evaluation/evaluators/rouge.py
def get_rouge_evaluator(
    name: str = "ROUGE",
    metrics: list[Metric | dict[str, list[Metric]]]
    | dict[str, list[Metric]]
    | None = None,
    *,
    version: str | None = None,
) -> Evaluator:
    """
    Returns an evaluator for ROUGE scores.

    Args:
        name (str): The name of the evaluator. Defaults to "ROUGE".
        metrics (list[Metric | dict[str, list[Metric]]] | dict[str, list[Metric]] | None):
            The metrics to use for evaluation. If None, defaults to ROUGE-1, ROUGE-2,
            and ROUGE-L with mean aggregation.

        version (str | None): Cache version for custom code or external resources.

    Returns:
        Evaluator: An evaluator for ROUGE scores.
    """
    cache_parameters = {
        "method": "rouge",
        "metrics": metrics,
    }
    if metrics is None:
        metrics = [
            {
                "ROUGE-1": [mean()],
                "ROUGE-2": [mean()],
                "ROUGE-L": [mean()],
            }
        ]

    rouge_calculator = RougeScoreCalculator()

    @scorer(name=name, metrics=metrics)
    def rouge_scorer() -> Scorer:
        async def score(state: TaskState, target: Target) -> Score:
            return await rouge_calculator.calculate_async(
                prediction=state.output.completion, reference=target.text
            )

        return score

    return Evaluator(
        name,
        version=version,
        cache_parameters=cache_parameters,
        scorer=rouge_scorer(),
        implementation=rouge_calculator.implementation,
    )

G-Eval rating responses

EvalSense requires one complete non-negative decimal integer in the configured rating range. Outer whitespace is allowed. Sampled ratings also accept leading zeroes; weighted ratings require canonical labels such as 8, without zero padding. Fractions, decimals, signs, JSON or Markdown wrappers, and explanatory text are rejected. For a 1–10 scale, 8 is valid; 0/10, -3, 8.5 and Rating scale 1 to 10. Final rating 9 all raise ValueError.

The endpoints must be non-negative integers with min_score <= max_score. Normalisation also requires min_score < max_score. For sampled ratings, normalisation uses (rating - min_score) / (max_score - min_score); on a 1–10 scale, 1 becomes 0, 10 becomes 1, and 8 becomes 7/9. With normalise=False, the validated integer is returned unchanged.

For example, configure a sampled-rating evaluator as follows. The mock model name is an offline placeholder; select the intended judge provider for a real run.

from evalsense.evaluation.evaluators import get_g_eval_evaluator
from evalsense.generation import ModelConfig

evaluator = get_g_eval_evaluator(
    prompt_template=(
        "Rate the candidate from 1 to 10. Return only the integer, "
        "without a denominator, sign or explanation. Candidate: {prediction}"
    ),
    model_config=ModelConfig("mockllm/judge"),
    min_score=1,
    max_score=10,
    logprobs=False,
)

The complete response is checked before optional log-probability weighting. Direct scorer calls raise ValueError for invalid responses; the pipeline records an evaluation error. No automatic format-repair retry is added.

Probability-weighted ratings

With logprobs=True (the default), EvalSense requests one output token and one completion, overriding any larger limits in the model configuration. Each allowed rating must appear as a complete canonical integer token in that token's returned top_logprobs. The sampled token must match the complete response and its reported probability. Token whitespace and one leading ▁ marker are supported; leading zeroes, repeated identical tokens and invalid probability mass are rejected.

top_logprobs must be a positive integer at least as large as the number of ratings on the scale. For 1–10, request at least 10 alternatives. This is a necessary budget, not a guarantee: non-rating tokens may occupy the returned slots, and the provider may return fewer alternatives or impose its own limit.

Missing ratings, missing log probabilities and multi-token responses fail clearly. Where supported, increase top_logprobs to include omitted low-probability ratings. Choose a smaller scale with suitable token labels, or explicitly set logprobs=False to use sampled integer ratings. EvalSense never silently switches from weighted scoring to a sampled rating. Providers that cannot produce this one-token evidence cannot use the weighted mode.

For each allowed rating, EvalSense adds the probabilities of its valid reported token spellings and normalises by their combined mass. The score is the conditional mean over those reported valid rating tokens. Top-k truncation can omit other token spellings, so this remains an approximation. It does not estimate probabilities of arbitrary complete responses or multiply probabilities along multi-token labels.

For example, suppose a 1–10 scale has the following complete token evidence:

Rating Reported probability
1 through 8, each explicitly present 0
9 0.30
10 0.70

The raw weighted score is 9 × 0.30 + 10 × 0.70 = 9.7. Normalisation gives (9.7 - 1) / 9, approximately 0.9667. If the judge instead represents 10 as "1", "0", its first-token probability is not evidence for the complete rating 10. The missing complete token causes an error; EvalSense does not report the incorrect prefix calculation 9 × 0.30 + 1 × 0.70 = 3.4.

Successful scores retain raw_score, output_text, prompt and score_format, plus the following estimator metadata in the saved Inspect log:

Field Meaning
estimator weighted_rating_tokens or sampled_rating, according to the mode actually used.
estimator_format evalsense.g-eval.estimator.v1 for both scoring modes.
weighting_policy evalsense.g-eval.single-token.v1 for weighted scores.
rating_probabilities Normalised probabilities keyed by rating strings, for weighted scores.
rating_probability_mass The total reported valid-token probability mass before normalisation, for weighted scores.

This evidence survives reopening JSON and .eval logs. Failed weighted attempts remain evaluation errors and do not become successful point scores. Valid response syntax and valid probability evidence do not establish the accuracy of the judge.

Existing scores

get_g_eval_evaluator includes the evalsense.g-eval.integer.v1 format marker in its evaluation-cache identity and successful score metadata. Both scoring modes also include the evalsense.g-eval.estimator.v1 estimator-format marker, so earlier scores without estimator evidence are re-scored. Weighted evaluators additionally include the evalsense.g-eval.single-token.v1 weighting policy in their cache identity. Matching generated outputs remain reusable, and historical logs remain readable.

Stored logs are not edited or revalidated. Re-score affected evaluations and keep the historical logs separately when comparing results. Code that constructs a custom Evaluator around a scorer factory must update its explicit evaluator version when changing its policy.

BERTScore IDF weighting

BertScoreCalculator and get_bertscore_evaluator score one prediction/reference pair at a time. They support only idf=False, which is already the default. Requests for automatic IDF or precomputed IDF dictionaries raise ValueError before loading the metric. Changing a calculator's idf attribute is also validated before scoring; weighting is never silently disabled.

BERTScore estimates inverse document frequency from a reference corpus. With one reference, every token in it appears in every document, so its smoothed IDF is zero. These weights cannot be normalised into a valid weighted score. Increasing batch_size only changes embedding batches, not the one-reference corpus passed by this evaluator.

The Hugging Face Evaluate BERTScore wrapper also treats a non-empty idf dictionary as an instruction to fit weights from the supplied references rather than using it as fixed token weights. Accepting a dictionary through this wrapper would therefore retain the same problem. Empty dictionaries are rejected too; they must not silently select unweighted scoring.

For corpus-weighted scoring, use the native bert_score.BERTScorer with idf=True and an explicitly chosen idf_sents reference corpus, or the native bert_score.score function with a precomputed dictionary. Keep the corpus and weights fixed across comparisons, and use token IDs from the matching tokenizer. A corpus-aware EvalSense integration is not implemented by this change.

The default unweighted scores, component names and hash metadata are unchanged. Previously stored weighted results are not repaired or revalidated; retain their provenance and do not compare them as though they were corpus-weighted BERTScore. To rerun without IDF, construct a new evaluator with idf=False and a new name so that old name-based evaluation caches are not reused.

References: