66 lines
3.6 KiB
Python
66 lines
3.6 KiB
Python
"""Agentic example: synthesize small coding tasks, validate them in a sandbox
|
|
(reference solution passes, the empty stub fails), probe each valid task with the
|
|
built-in agent at k=2, and route by pass rate.
|
|
|
|
pw run examples/coding_tasks/graph.py # fake model + local sandbox (offline, fast)
|
|
pw run examples/coding_tasks/graph.py --project examples/coding_tasks/pipewright.docker.yaml
|
|
pw run examples/coding_tasks/graph.py --project examples/coding_tasks/pipewright.live.yaml
|
|
"""
|
|
|
|
from pipewright import Graph
|
|
|
|
TEST_CMD = "python3 test_solution.py"
|
|
|
|
g = Graph("coding-tasks", description="Synthetic coding tasks, sandbox-validated and probed by an agent")
|
|
|
|
synth = g.add("synth", "llm.synthesize", model="generator", kind="task", calls=1, batch=4, key_field="name",
|
|
prompt=(
|
|
"Invent {{ batch }} small, self-contained Python exercises of mixed difficulty (variation "
|
|
"{{ i }}). For each give: `name` (snake_case id), `statement` (what to implement), "
|
|
"`signature` (the single `def ...:` line), `solution` (a complete solution.py module), and "
|
|
"`tests` (a Python script that does `from solution import *`, checks the behaviour with "
|
|
"plain `assert`s, and prints ok). Use only the standard library."),
|
|
item_schema={"type": "object", "required": ["name", "statement", "signature", "solution", "tests"],
|
|
"properties": {k: {"type": "string"} for k in
|
|
("name", "statement", "signature", "solution", "tests")}})
|
|
|
|
prepare = g.add("prepare", "map.expr", input=synth, fields={
|
|
"stub": "payload.signature + '\\n raise NotImplementedError\\n'",
|
|
"files": "{'test_solution.py': payload.tests}",
|
|
})
|
|
|
|
validate = g.add("validate", "sandbox.check", input=prepare, pool="sandbox", files_expr="payload.files",
|
|
checks=[
|
|
{"name": "gold", "files": {"solution.py": "{{ payload.solution }}"}, "command": TEST_CMD,
|
|
"expect": "pass", "timeout": 60},
|
|
{"name": "empty", "files": {"solution.py": "{{ payload.stub }}"}, "command": TEST_CMD,
|
|
"expect": "fail", "timeout": 60},
|
|
])
|
|
|
|
valid = g.add("valid", "filter.expr", input=validate, expr="labels.valid")
|
|
|
|
rollouts = g.add("rollouts", "flatmap.repeat", input=valid, k=2)
|
|
|
|
probe = g.add("probe", "agent.run", input=rollouts, harness="solver", pool="sandbox", max_turns=12, timeout=600,
|
|
prompt=("{{ payload.statement }}\n\nImplement it in solution.py with this signature:\n\n"
|
|
" {{ payload.signature }}\n\nsolution.py currently contains a stub."),
|
|
files={"solution.py": "{{ payload.stub }}"},
|
|
grade={"command": TEST_CMD, "files": {"test_solution.py": "{{ payload.tests }}"}, "timeout": 60},
|
|
retries={"provider": 3, "infra": 2})
|
|
|
|
passrate = g.add("passrate", "join.group", input=probe, by="labels.group", kind="task_result",
|
|
aggregates={"pass_rate": ["mean", "labels.passed"], "rollouts": ["count", "1"]})
|
|
|
|
bucket = g.add("bucket", "route.expr", input=passrate, default="too_hard", routes={
|
|
"too_easy": "labels.pass_rate == 1",
|
|
"learnable": "labels.pass_rate > 0",
|
|
})
|
|
|
|
for name in ("too_easy", "learnable", "too_hard"):
|
|
g.add(f"export_{name}", "sink.jsonl", path=f"out/{name}.jsonl", input=bucket.port(name),
|
|
fields=["key", "labels.pass_rate", "labels.rollouts"])
|
|
g.add("export_invalid", "sink.jsonl", path="out/invalid.jsonl", input=valid.port("rejected"),
|
|
fields=["key", "labels.checks"])
|
|
|
|
graph = g
|