Files
pipewright/examples/coding_tasks/fakes.py
T

58 lines
3.1 KiB
Python

"""Scripted fake model for the coding-tasks example (offline tests).
- synthesis requests get four fixed tasks (one with a broken reference solution, so
validation has something to reject);
- agent requests get a write_file tool call, then finish. The agent always solves
`add`, never solves `reverse_words`, and solves `classify` on alternate attempts,
so the pass-rate routing has one task in each bucket.
"""
import json
TASKS = [
{"name": "add", "statement": "Return the sum of a and b.", "signature": "def add(a, b):",
"solution": "def add(a, b):\n return a + b\n",
"tests": "from solution import *\nassert add(2, 3) == 5\nassert add(-1, 1) == 0\nprint('ok')\n"},
{"name": "classify", "statement": "Return 'fizz' for multiples of 3, 'buzz' for multiples of 5, "
"'fizzbuzz' for both, else str(n).", "signature": "def classify(n):",
"solution": "def classify(n):\n s = ('fizz' if n % 3 == 0 else '') + ('buzz' if n % 5 == 0 else '')\n"
" return s or str(n)\n",
"tests": "from solution import *\nassert classify(3) == 'fizz'\nassert classify(10) == 'buzz'\n"
"assert classify(15) == 'fizzbuzz'\nassert classify(7) == '7'\nprint('ok')\n"},
{"name": "reverse_words", "statement": "Reverse the order of words in s, collapsing whitespace.",
"signature": "def reverse_words(s):",
"solution": "def reverse_words(s):\n return ' '.join(reversed(s.split()))\n",
"tests": "from solution import *\nassert reverse_words('a b c') == 'c b a'\nassert reverse_words('') == ''\n"
"print('ok')\n"},
{"name": "broken_max", "statement": "Return the largest element of xs.", "signature": "def biggest(xs):",
"solution": "def biggest(xs):\n return min(xs)\n",
"tests": "from solution import *\nassert biggest([1, 5, 2]) == 5\nprint('ok')\n"},
]
ATTEMPTS = {
"add": ["def add(a, b):\n return a + b\n"],
"classify": [TASKS[1]["solution"], "def classify(n):\n return str(n)\n"],
"reverse_words": ["def reverse_words(s):\n return s[::-1]\n"],
}
_counter: dict[str, int] = {}
def respond(payload: dict):
rf = payload.get("response_format") or {}
if rf.get("type") == "json_schema":
return {"role": "assistant", "content": json.dumps({"items": TASKS})}
messages = payload["messages"]
prompt = next(m["content"] for m in messages if m["role"] == "user")
if any(m["role"] == "tool" for m in messages):
return {"role": "assistant", "content": "", "tool_calls": [
{"id": "call_2", "type": "function", "function": {"name": "finish", "arguments": '{"summary": "done"}'}}]}
name = next((n for n in ATTEMPTS if f"def {n}(" in prompt), None)
if name is None:
return {"role": "assistant", "content": "I cannot solve this."}
i = _counter.get(name, 0)
_counter[name] = i + 1
code = ATTEMPTS[name][i % len(ATTEMPTS[name])]
return {"role": "assistant", "content": "", "tool_calls": [
{"id": "call_1", "type": "function",
"function": {"name": "write_file", "arguments": json.dumps({"path": "solution.py", "content": code})}}]}