Datasets:
File size: 1,922 Bytes
676defa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #!/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)}))
|