Archived
31 lines
1.5 KiB
Python
31 lines
1.5 KiB
Python
import json, re, sys, urllib.request
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
N=int(sys.argv[1]); PORT=int(sys.argv[2]); OUT=sys.argv[3]
|
|
items=json.load(open("/home/zee/models/calib/gsm8k/test.json"))[:N]
|
|
PROMPT=("Solve this maths problem step by step. End with exactly: The answer is <number>\n\nProblem: %s")
|
|
def norm(a):
|
|
a=a.replace(",","").replace("$","").strip()
|
|
try: return float(a)
|
|
except: return None
|
|
def ask(q):
|
|
body=json.dumps({"model":"qwen35","messages":[{"role":"user","content":PROMPT%q}],
|
|
"max_tokens":700,"temperature":0,"reasoning_effort":"none"}).encode()
|
|
r=urllib.request.urlopen(urllib.request.Request(f"http://127.0.0.1:{PORT}/v1/chat/completions",data=body,
|
|
headers={"Content-Type":"application/json"}),timeout=600).read()
|
|
return json.loads(r)["choices"][0]["message"].get("content") or ""
|
|
def extract(t):
|
|
m=re.findall(r"answer is\s*\$?([-\d][\d,\.]*)", t, re.I)
|
|
if m: return norm(m[-1])
|
|
m=re.findall(r"([-\d][\d,\.]*)\s*$", t.strip())
|
|
return norm(m[-1]) if m else None
|
|
res={}
|
|
def one(i):
|
|
q=items[i]["q"]; g=norm(items[i]["a"])
|
|
try: t=ask(q)
|
|
except Exception: res[i]=(False,g,None,"ERR"); return
|
|
p=extract(t); res[i]=(p is not None and g is not None and abs(p-g)<1e-6, g, p, t)
|
|
with ThreadPoolExecutor(max_workers=8) as ex: list(ex.map(one, range(len(items))))
|
|
c=sum(1 for v in res.values() if v[0])
|
|
print(f"GSM8K {N}: {c}/{len(res)} = {100*c/len(res):.1f}%")
|
|
json.dump({str(k):v[:3] for k,v in res.items()},open(OUT,"w"))
|