msrh-zindi-magic / scripts /build_v8_k7_fewshot.py
MagicCard's picture
Docs
028081c
Raw
History Blame Contribute Delete
1.41 kB
#!/usr/bin/env python3
'''Build v8 variants of K=7 fewshot train + test by replacing v1 prefix with v8.
v8 = shortened v6 anchored-extraction (33 words vs v6 70 words).
'''
import json, pathlib
V1 = 'Use the retrieved contexts as your primary sources — copy exact phrasing where the contexts already address the question. Be concise and factually accurate.'
V8 = 'The retrieved contexts are your source of truth — copy or paraphrase their exact phrasing to answer. Reply in the same language and script as the question. Plain prose, no disclaimers or meta-commentary.'
DATA_DIR = pathlib.Path('/mnt/msrh/Magic_submission/LF/data')
PAIRS = [
('msrh_rag_train_afrie5_TV_k7_fewshot.json', 'msrh_rag_train_afrie5_TV_k7_fewshot_v8.json'),
('msrh_rag_test_k3_AfriE5_TV_fewshot_k7.json', 'msrh_rag_test_k3_AfriE5_TV_fewshot_k7_v8.json'),
]
for src_name, dst_name in PAIRS:
src = DATA_DIR / src_name
dst = DATA_DIR / dst_name
n_ok = n_miss = 0
with open(src) as fin, open(dst, 'w') as fout:
for ln in fin:
r = json.loads(ln)
c = r['messages'][0]['content']
if V1 in c:
r['messages'][0]['content'] = c.replace(V1, V8)
n_ok += 1
else:
n_miss += 1
fout.write(json.dumps(r, ensure_ascii=False) + '\n')
print(f'{src_name:55s} -> {dst_name:55s} ok={n_ok} miss={n_miss}')