The platform for continuously improving AI agents.
"""A native evaluation compares prompt variants of one model, and a development measurement
also reads decision training rows; a final one reads only an evaluation dataset."""
import hashlib
import json
import pytest
from overbae.core.errors import InputValidationError
from overbae.models import Dataset, FinetuningJob, NativeEvaluationPlan, Project
from overbae.services import native_evaluation
from overbae.services.datasets import land
from overbae.services.decision_providers import question
pytestmark = [pytest.mark.django_db, pytest.mark.covers("state-machines")]
REQUEST = {"state": "Evidence", "question": "Choose", "kind": "choice", "options": ["yes", "no"]}
def suite(project, intent, row):
dataset = Dataset.objects.create(project=project, name=intent, intent=intent)
land.land_rows(dataset, [row(i) for i in range(3)])
dataset.refresh_from_db()
return dataset.active_cell
def evaluation_row(i):
return {
"input": {"decision": {**REQUEST, "state": f"Evidence {i}"}},
"expected_output": {"probabilities": [1, 0]},
}
def training_row(i):
return {"decision": {**REQUEST, "state": f"Evidence {i}", "target_probabilities": [1, 0]}}
@pytest.fixture
def job():
project = Project.objects.create(name="Variants", slug="variants")
train = Dataset.objects.create(project=project, name="Train", intent="train")
return FinetuningJob.objects.create(
project=project,
dataset=train,
base_model="fixture",
hyperparameters={"objective": "decision_cross_entropy"},
status="running",
)
def plan(
job, final_cell, *, purpose="final", instructions="Answer as a cautious reviewer.", key=""
):
return native_evaluation.create_plan(
job.project,
name="Prompts",
request_key=key or f"{purpose}-{len(instructions)}",
final_cell=final_cell,
participants=[
{"key": "plain", "name": "Plain", "kind": "trained", "job": str(job.pk)},
{
"key": "careful",
"name": "Careful",
"kind": "trained",
"job": str(job.pk),
"instructions": instructions,
},
],
baseline="plain",
purpose=purpose,
)
def test_a_prompt_variant_is_prepared_on_its_own_and_its_receipt_must_match(job):
variants = plan(job, suite(job.project, "eval", evaluation_row))
assert variants.config["participants"][1]["instructions"] == "Answer as a cautious reviewer."
assert "instructions" not in variants.config["participants"][0]
assert native_evaluation.stage_sequence(variants)[1:5] == [
"final_prepare",
"final_prepare_careful",
"final_plain",
"final_careful",
]
with pytest.raises(InputValidationError, match="instructions differ"):
native_evaluation.collect(variants, "final_prepare_careful", {"instructions": ""})
# System One reads the same instructions before the question.
assert question(REQUEST, "Be brief.")["instructions"] == "Be brief.\n\nChoose"
with pytest.raises(InputValidationError, match="at most 8000"):
plan(job, variants.final_cell, instructions="x" * 8001)
def test_development_reads_training_decisions_and_final_refuses_them(job, tmp_path, settings):
settings.MEDIA_ROOT = tmp_path
training = suite(job.project, "train", training_row)
with pytest.raises(InputValidationError, match="evaluation dataset"):
plan(job, training)
development = plan(job, training, purpose="development")
assert development.config["purpose"] == "development"
inputs = native_evaluation.seal_suite(development, "final")
references = [json.loads(line) for line in (inputs.parent / "references.jsonl").open()]
assert [json.loads(line)["decision"] for line in inputs.open()][0] == {
**REQUEST,
"state": "Evidence 0",
}
assert [row["reference"] for row in references] == [{"probabilities": [1, 0]}] * 3
def test_a_plan_saved_before_purpose_was_recorded_retries_as_itself(job):
cell = suite(job.project, "eval", evaluation_row)
saved = plan(job, cell)
older = {k: v for k, v in saved.config.items() if k != "purpose"}
NativeEvaluationPlan.objects.filter(pk=saved.pk).update(config=older)
assert plan(job, cell).pk == saved.pk
def test_a_shared_preparation_skips_a_variant_that_reuses_its_predictions(
job, tmp_path, settings, fake_modal
):
settings.MEDIA_ROOT = tmp_path
cell = suite(job.project, "eval", evaluation_row)
source = plan(job, cell, key="source")
source.calls = {"final_careful": {"state": "completed", "receipt": {}}}
source.save()
predictions = native_evaluation.directory(source, "final") / "careful.jsonl"
predictions.parent.mkdir(parents=True)
predictions.write_text("")
variants = native_evaluation.reuse_predictions(
plan(job, cell, key="variants"),
source=source,
participant="careful",
source_participant="careful",
)
native_evaluation.seal_suite(variants, "final")
failures = b'{"key": "0"}\n'
fake_modal.files[f"/decision-evaluations/{variants.id}-final/failures.jsonl"] = failures
native_evaluation.collect(
variants, "final_prepare", {"failures_sha256": hashlib.sha256(failures).hexdigest()}
)
stage = native_evaluation.directory(variants, "final")
assert (stage / "plain.failures.jsonl").read_bytes() == failures
assert not (stage / "careful.failures.jsonl").exists()