Skip to content

The platform for continuously improving AI agents.

2.0 KB
Raw Blame
"""The rules a benchmark's own criteria follow, for REST, MCP and the assistant's draft."""

from overbae.core.errors import InputValidationError
from overbae.innermind import deterministic
from overbae.models import EvalMatrix

EXPECTATION_MAX_CHARS = 1_000


def clean_criterion(criterion: dict) -> dict:
    """The criterion as stored; Python criteria keep no judge fields. The caller checks
    each field's type and length."""
    if criterion["type"] == EvalMatrix.Type.DETERMINISTIC:
        if problem := deterministic.audit(criterion["source"]):
            raise InputValidationError(problem)
        return {k: v for k, v in criterion.items() if k not in ("options", "output")}
    if len(criterion["source"]) > EXPECTATION_MAX_CHARS:
        raise InputValidationError(f"Question exceeds {EXPECTATION_MAX_CHARS} characters.")
    if "output" not in criterion:
        raise InputValidationError("Choose yes/no or multiple choice.")
    labels = [option["label"].strip() for option in criterion.get("options") or []]
    if not 2 <= len(set(labels)) == len(labels) <= 20 or not all(labels):
        raise InputValidationError("Provide 2–20 distinct answers.")
    if criterion["output"] == "yes_no" and labels != ["Yes", "No"]:
        raise InputValidationError("Yes/no answers are Yes and No.")
    return criterion


def check_names(criteria: list[dict]) -> None:
    names = [criterion["name"] for criterion in criteria]
    if len(set(names)) != len(names):
        raise InputValidationError("Criterion names must be unique.")


def clean_criteria(criteria: list[dict]) -> list[dict]:
    """Every rule at once, naming the criterion that breaks one."""
    if not criteria:
        raise InputValidationError("Add at least one criterion.")
    check_names(criteria)
    cleaned = []
    for criterion in criteria:
        try:
            cleaned.append(clean_criterion(criterion))
        except InputValidationError as exc:
            raise InputValidationError(f"{criterion['name']}: {exc.detail}") from None
    return cleaned