File size: 2,075 Bytes
87ab2b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import argparse,json,sys
from pathlib import Path
from common import *
def export(config,split,fmt,path,allow_eval=False):
if config=='benchmark' and not allow_eval:raise ValueError('Benchmark export requires --allow-evaluation-export; never use for training')
if fmt!='jsonl' and config!='sft':raise ValueError('Training/chat formats apply only to SFT')
assert not path.exists(),'Refuse to overwrite export'
rows=read_rows(ROOT/f'data/{config}/{split}.parquet')
with path.open('x') as f:
for r in rows:
if fmt=='jsonl':o=r
elif fmt in ['messages','openai','trl','axolotl']:o={'messages':r['messages']}
elif fmt=='chatml':
# Escape structural delimiters in content; reject injection into text format.
if any(token in m['content'] for m in r['messages'] for token in ['<|im_start|>','<|im_end|>']):raise ValueError('Unsafe ChatML delimiter in content')
o={'id':r['id'],'text':''.join('<|im_start|>'+m['role']+'\n'+m['content']+'<|im_end|>\n' for m in r['messages'])}
else:raise ValueError(fmt)
f.write(json.dumps(o,ensure_ascii=False)+'\n')
dump(str(path)+'.manifest.json',{'config':config,'split':split,'format':fmt,'rows':len(rows),'source_sha256':digest((ROOT/f'data/{config}/{split}.parquet').read_bytes()),'benchmark_evaluation_only':config=='benchmark','note':'messages/trl/axolotl preserve multi-turn conversations; apply the target tokenizer chat template in the trainer'})
print({'rows':len(rows),'format':fmt,'path':str(path)})
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('--config',choices=['sft','rag','agentic','benchmark'],default='sft');p.add_argument('--split',default='train');p.add_argument('--format',choices=['jsonl','messages','openai','trl','axolotl','chatml'],default='messages');p.add_argument('--output',type=Path,required=True);p.add_argument('--allow-evaluation-export',action='store_true');a=p.parse_args();export(a.config,a.split,a.format,a.output,a.allow_evaluation_export)
|