File size: 2,436 Bytes
a6e2a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Prompt export and strict reference-action baseline; stdlib only."""
import json,sys
from pathlib import Path
from collections import defaultdict
R=Path(__file__).resolve().parent
INPUTS=['id','domain','task_type','initial_state','goal','action_history','failure_state','failure_type']
def canon(x):return json.dumps(x,sort_keys=True,separators=(',',':'),allow_nan=False)
def read(path):
    def pairs(items):
        d={}
        for k,v in items:
            if k in d:raise ValueError('duplicate JSON key')
            d[k]=v
        return d
    return [json.loads(x,object_pairs_hook=pairs,parse_constant=lambda x: (_ for _ in ()).throw(ValueError('nonfinite'))) for x in Path(path).read_text('utf8').splitlines() if x.strip()]
def score(rows,predictions):
    refs={r['id']:r for r in rows};pred={};totals=defaultdict(lambda:[0,0,0])
    for p in predictions:
        if p['id'] not in refs or p['id'] in pred:raise ValueError('unknown or duplicate prediction ID')
        if type(p.get('recovery_available')) is not bool:raise ValueError('availability must be bool')
        if 'recovery_action' not in p or (p['recovery_action'] is not None and not isinstance(p['recovery_action'],dict)):raise ValueError('recovery_action must be object or null')
        pred[p['id']]=p
    for r in rows:
        p=pred.get(r['id']);a=p is not None and p['recovery_available']==r['recovery_available'];b=a and canon(p['recovery_action'])==canon(r['recovery_action'])
        for group in ['overall',r['domain']]:
            totals[group][0]+=1;totals[group][1]+=int(a);totals[group][2]+=int(b)
    return {'metric':'strict reference action match; alternative valid plans may score zero','predictions':len(pred),'missing':len(rows)-len(pred),'scores':{g:{'n':n,'availability_accuracy':a/n,'recovery_action_exact_match':b/n} for g,(n,a,b) in totals.items()}}
if __name__=='__main__':
    mode,split,path=sys.argv[1:];assert split in ['train','validation','test','holdout']
    rows=read(R/(split+'.jsonl'))
    if mode=='make-prompts':
        Path(path).write_text(''.join(json.dumps({'instruction':'Infer whether recovery is available and return recovery_available (bool) and recovery_action (object or null).','input':{k:r[k] for k in INPUTS}},ensure_ascii=False)+'\n' for r in rows),encoding='utf8')
    elif mode=='score':print(json.dumps(score(rows,read(path)),indent=2))
    else:raise ValueError('mode must be make-prompts or score')