#!/usr/bin/env python3 """Deterministic capability battery: run against one model, grade, emit JSON. usage: eval_battery.py MODEL [max_tokens]""" import json, urllib.request, time, sys, re PORT = 11234 MODEL = sys.argv[1] MAXT = int(sys.argv[2]) if len(sys.argv) > 2 else 2048 def needle_doc(): filler = "The archive records ordinary maintenance notes for the facility and its equipment. " body = [] for i in range(3000): body.append(f"Entry {i}: {filler}") if i == 1500: body.append("The secret verification code is PERIWINKLE-4471. Keep it in mind.") return "\n".join(body) CASES = [ ("arith", "Compute 1234*5678 exactly. Think briefly, then give only the integer.", lambda s: "7006652" in s.replace(",", "")), ("modular", "Compute 2^100 mod 13. Think briefly, then give only the integer.", lambda s: "3" in s), ("logic", "All Bloops are Razzies. All Razzies are Lazzies. Are all Bloops Lazzies? Answer yes or no, then one sentence why.", lambda s: re.search(r"\byes\b", s.lower()) is not None), ("fib", "Write a Python function fib(n) returning the nth Fibonacci number iteratively. Give only the code.", lambda s: ("def fib" in s) and ("return" in s) and (("for " in s) or ("while " in s))), ("prime", "Write a Python function is_prime(n) that returns True iff n is prime. Give only the code.", lambda s: ("def is_prime" in s) and ("return" in s) and bool(re.search(r"%|sqrt|range", s))), ("needle", needle_doc() + "\n\nQuestion: What is the secret verification code in the document above? Reply with the code only.", lambda s: "PERIWINKLE-4471" in s), ("multi", "用中文解释混合专家(MoE)模型的稀疏路由原理,并说明为什么激活参数远小于总参数。", lambda s: ("MoE" in s or "混合专家" in s) and len(s) > 40), ] def run(prompt): body = json.dumps({"model": MODEL, "messages": [{"role": "user", "content": prompt}], "max_tokens": MAXT, "temperature": 0.0}).encode() req = urllib.request.Request(f"http://127.0.0.1:{PORT}/v1/chat/completions", data=body, headers={"Content-Type": "application/json"}) t = time.time() d = json.load(urllib.request.urlopen(req, timeout=1800)) c = d["choices"][0] return d, c, time.time() - t out = {"model": MODEL, "cases": {}} for name, prompt, grade in CASES: try: d, c, wall = run(prompt) content = c["message"].get("content") or "" out["cases"][name] = { "pass": bool(grade(content)), "finish": c.get("finish_reason"), "wall_s": round(wall, 1), "usage": d.get("usage"), "content_head": content[:200], } except Exception as e: out["cases"][name] = {"pass": False, "error": repr(e)} print(name, json.dumps(out["cases"][name])[:220], flush=True) out["score"] = sum(1 for v in out["cases"].values() if v.get("pass")) / len(out["cases"]) print("SCORE", MODEL, round(out["score"], 3)) json.dump(out, open(f"/tmp/re_stock/eval_{MODEL}.json", "w"), indent=1)