The platform for continuously improving AI agents.
import json
from types import SimpleNamespace
from django.db import IntegrityError, transaction
from django.utils.dateparse import parse_datetime
from overbae.api.eval_serializers import EvalSetSerializer
from overbae.core.errors import InputValidationError
from overbae.models import EvalSet
def find(project, identifier):
eval_set = (
EvalSet.objects.select_related("capability").filter(project=project, pk=identifier).first()
)
if eval_set is None:
raise InputValidationError("The eval set was not found in this project")
return eval_set
def describe(eval_set):
return {
"id": str(eval_set.pk),
"name": eval_set.name,
"description": eval_set.description,
"capability": eval_set.capability.slug if eval_set.capability_id else None,
"criteria": eval_set.matrix_snapshot(),
# A set built from shared evaluators is revised through those evaluators.
"amendable": eval_set.criteria is not None,
"updated_at": eval_set.updated_at.isoformat(),
}
def serializer(project, user, existing, definition):
checked = EvalSetSerializer(
instance=existing,
data=definition if existing else {**definition, "project": str(project.pk)},
partial=existing is not None,
context={"request": SimpleNamespace(user=user)},
)
if not checked.is_valid():
raise InputValidationError(f"The eval set failed validation: {json.dumps(checked.errors)}")
taken = EvalSet.objects.filter(
project=project,
capability=existing.capability_id if existing else None,
name=definition["name"],
)
if taken.exclude(pk=existing.pk if existing else None).exists():
raise InputValidationError(f"An eval set named '{definition['name']}' already exists")
return checked
def definition_of(checked):
return json.loads(
json.dumps(
{key: checked.validated_data[key] for key in ("name", "description", "criteria")}
)
)
def draft(project, user, *, eval_set, name, description, criteria):
"""A validated proposal. Nothing is saved until its review approves it."""
existing = find(project, eval_set) if eval_set else None
if existing is not None and existing.criteria is None:
raise InputValidationError(
"This eval set is built from shared evaluators. Draft a new eval set with its criteria."
)
definition = definition_of(
serializer(
project,
user,
existing,
{"name": name, "description": description, "criteria": criteria},
)
)
current = describe(existing) if existing else None
if current and all(current[key] == value for key, value in definition.items()):
raise InputValidationError("The amendment changes nothing")
return {**definition, "eval_set": str(existing.pk) if existing else None, "current": current}
@transaction.atomic
def apply(project, user, proposal, criteria):
existing = None
if proposal["eval_set"]:
existing = (
EvalSet.objects.select_for_update()
.filter(project=project, pk=proposal["eval_set"])
.first()
)
if existing is None:
raise InputValidationError("The eval set this amendment changes no longer exists")
if existing.updated_at != parse_datetime(proposal["current"]["updated_at"]):
raise InputValidationError(
"The eval set changed after this amendment was drafted. Reject it and ask for a new draft."
)
checked = serializer(
project,
user,
existing,
{"name": proposal["name"], "description": proposal["description"], "criteria": criteria},
)
try:
return checked.save() if existing else checked.save(created_by=user)
except IntegrityError:
raise InputValidationError(
f"An eval set named '{proposal['name']}' already exists"
) from None