okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-1402-w' into f-140253623e56f

main
2.8 KiB64 linesraw
1#!/usr/bin/env python3
2"""Small verifiable coding check: the model writes a function, a fixed test suite runs it locally.
3
4    python coding_check.py --url http://127.0.0.1:18096 --out coding-check.json
5"""
6import argparse
7import json
8import re
9import time
10import urllib.request
11
12TASK = ("Write a Python function `roman_to_int(s: str) -> int` that converts a Roman numeral (I, V, X, L, C, D, M, "
13        "including subtractive forms like IV, IX, XL, XC, CD, CM) to an integer, and raises ValueError for an empty "
14        "string or any character that is not a Roman numeral. Reply with one Python code block only.")
15TESTS = [("III", 3), ("IV", 4), ("IX", 9), ("LVIII", 58), ("MCMXCIV", 1994), ("MMXXVI", 2026), ("CDXLIV", 444)]
16ERRORS = ["", "ABC", "MMX1"]
17
18
19def main():
20    ap = argparse.ArgumentParser()
21    ap.add_argument("--url", default="http://127.0.0.1:18096")
22    ap.add_argument("--out", default="coding-check.json")
23    ap.add_argument("--effort", default="low")
24    a = ap.parse_args()
25    body = {"model": "strata", "messages": [{"role": "user", "content": TASK}], "temperature": 0,
26            "reasoning_effort": a.effort, "max_tokens": 4096}
27    t0 = time.perf_counter()
28    req = urllib.request.Request(a.url + "/v1/chat/completions", data=json.dumps(body).encode(),
29                                 headers={"Content-Type": "application/json"})
30    with urllib.request.urlopen(req, timeout=900) as r:
31        answer = json.loads(r.read())
32    elapsed = time.perf_counter() - t0
33    text = answer["choices"][0]["message"].get("content") or ""
34    m = re.search(r"```(?:python)?\n(.*?)```", text, re.S)
35    code = m.group(1) if m else text
36    results, passed = [], 0
37    namespace = {}
38    try:
39        exec(compile(code, "<model>", "exec"), namespace)
40        f = namespace["roman_to_int"]
41        for s, want in TESTS:
42            got = f(s)
43            ok = got == want
44            passed += ok
45            results.append({"input": s, "expected": want, "got": got, "ok": ok})
46        for s in ERRORS:
47            try:
48                f(s)
49                results.append({"input": s, "expected": "ValueError", "got": "no error", "ok": False})
50            except ValueError:
51                passed += 1
52                results.append({"input": s, "expected": "ValueError", "got": "ValueError", "ok": True})
53    except Exception as e:
54        results.append({"error": repr(e)})
55    total = len(TESTS) + len(ERRORS)
56    out = {"task": TASK, "reasoning_effort": a.effort, "elapsed_s": round(elapsed, 2), "usage": answer.get("usage"),
57           "passed": passed, "total": total, "results": results, "code": code}
58    with open(a.out, "w", encoding="utf-8") as fh:
59        json.dump(out, fh, indent=2)
60    print(f"coding check: {passed}/{total} tests passed in {elapsed:.1f} s")
61
62
63if __name__ == "__main__":
64    main()