Datasets:
Download code/mt_numeric.py from drawais/circuitqa: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/datasets/drawais/circuitqa/resolve/main/code/mt_numeric.py
- Command line
-
hf download hf://datasets/drawais/circuitqa/code/mt_numeric.py
-
curl -L -o mt_numeric.py https://huggingface.co/datasets/drawais/circuitqa/resolve/main/code/mt_numeric.py
1.92 kB
| #!/usr/bin/env python3 | |
| """Numeric fidelity metrics for the multi-tool set (complement to slot accuracy): | |
| num_recall : share of the reference answer's numbers present in the prediction | |
| num_precision : share of the prediction's numbers that occur in the tool outputs or question | |
| (a prediction number found nowhere in the context is unsupported) | |
| all_backends : prediction uses at least one value unique to EACH backend block | |
| python3 mt_numeric.py --ref compositional/test.jsonl --pred preds.jsonl | |
| (the paper scores end-to-end outputs against compositional/test.jsonl, i.e. against the outputs of the | |
| correct calls; --ref compositional/test_e2e.jsonl scores them against the outputs of the router's calls) | |
| """ | |
| import argparse, json, sys, os, statistics as st | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from mt_qa import nums | |
| ap = argparse.ArgumentParser(); ap.add_argument("--ref", required=True); ap.add_argument("--pred", required=True) | |
| a = ap.parse_args() | |
| ref = {json.loads(l)["question_hash"]: json.loads(l) for l in open(a.ref)} | |
| rec, prec, allb, n = [], [], 0, 0 | |
| for l in open(a.pred): | |
| p = json.loads(l); r = ref.get(p["question_hash"]) | |
| if not r: continue | |
| n += 1; pr = p.get("prediction", "") | |
| gn, pn = nums(r["answer"]), nums(pr) | |
| ctx = nums(r["tool_result"] + "\n" + r["question"]) | |
| if gn: rec.append(len(gn & pn) / len(gn)) | |
| if pn: prec.append(len(pn & ctx) / len(pn)) | |
| blocks = r["tool_result"].split("\n\n[") | |
| sets = [nums(b) for b in blocks] | |
| ok = True | |
| for i, s in enumerate(sets): | |
| u = s - set().union(*[t for j, t in enumerate(sets) if j != i]) - nums(r["question"]) | |
| if u and not (pn & u): ok = False | |
| allb += ok | |
| print(json.dumps({"n": n, "num_recall": round(100 * st.mean(rec), 1), "num_precision": round(100 * st.mean(prec), 1), | |
| "all_backends_used": round(100 * allb / n, 1)})) | |