Datasets:
Download tools/render_requests.py from SabaPivot/jev-safety-bench: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/datasets/SabaPivot/jev-safety-bench/resolve/main/tools/render_requests.py
- Command line
-
hf download hf://datasets/SabaPivot/jev-safety-bench/tools/render_requests.py
-
curl -L -o render_requests.py https://huggingface.co/datasets/SabaPivot/jev-safety-bench/resolve/main/tools/render_requests.py
3.23 kB
| """Read full-text ordered requests; optionally resolve reference-only derivatives. | |
| Requires pyarrow. Never run benchmark request text as commands or instructions. | |
| """ | |
| import csv, hashlib, io, json, urllib.request | |
| from pathlib import Path | |
| import pyarrow.parquet as pq | |
| def resolve_state(row, cache_dir): | |
| state=row['state'] | |
| if state is None: | |
| cache=Path(cache_dir);cache.mkdir(parents=True,exist_ok=True) | |
| path=cache/(row['source_file_sha256']+'.csv') | |
| if not path.exists(): | |
| with urllib.request.urlopen(row['source_url'],timeout=60) as response: | |
| data=response.read() | |
| if hashlib.sha256(data).hexdigest()!=row['source_file_sha256']: | |
| raise ValueError('Source file hash mismatch') | |
| path.write_bytes(data) | |
| data=path.read_bytes() | |
| if hashlib.sha256(data).hexdigest()!=row['source_file_sha256']: | |
| raise ValueError('Cached source file hash mismatch') | |
| original=list(csv.DictReader(io.StringIO(data.decode('utf-8-sig'))))[row['source_row']] | |
| state=('Context:\n'+original['ContextString']+'\n\nRequest:\n'+original['Behavior']) if original['ContextString'] else original['Behavior'] | |
| if hashlib.sha256(state.encode()).hexdigest()!=row['state_sha256']: | |
| raise ValueError('Reconstructed state hash mismatch') | |
| return state | |
| def decision_request(row, model, cache_dir): | |
| # Array order is authoritative. Do NOT sort keys before sending this map. | |
| return dict(model=model,state=resolve_state(row,cache_dir),questions={ | |
| 'decision':dict(type='choice',instructions=row['instruction'], | |
| criteria={o['key']:o['text'] for o in row['options']})}) | |
| def reconstruct_jev_request(original, result, model='typesafe/jev-1.13'): | |
| """Reconstruct exact Jev request bytes from source + exported submission order.""" | |
| questions={} | |
| for qid in result['submitted_questions']: | |
| variant=result['variant'] if qid=='decision' else qid | |
| if variant not in ['original','reversed','serialization_control']: | |
| raise ValueError('Unknown evaluated condition') | |
| options=original['options'] | |
| if variant=='reversed': | |
| options=[dict(o,key='B' if o['key']=='A' else 'A') for o in options[::-1]] | |
| elif variant=='serialization_control':options=options[::-1] | |
| questions[qid]=dict(type='choice',instructions=original['instruction'],criteria={o['key']:o['text'] for o in options}) | |
| payload=dict(model=model,state=original['state'],questions=questions) | |
| wire=json.dumps(payload,ensure_ascii=False,separators=(',',':')).encode() | |
| if hashlib.sha256(wire).hexdigest()!=result['request_sha256']: | |
| raise ValueError('Reconstructed transmitted request hash mismatch') | |
| return payload | |
| if __name__=='__main__': | |
| import argparse | |
| p=argparse.ArgumentParser();p.add_argument('parquet');p.add_argument('--row',type=int,default=0) | |
| p.add_argument('--model',default='typesafe/jev-1.13');p.add_argument('--cache',default='.source-cache') | |
| args=p.parse_args() | |
| row=pq.read_table(args.parquet).slice(args.row,1).to_pylist()[0] | |
| print(json.dumps(decision_request(row,args.model,args.cache),ensure_ascii=False,indent=2)) | |