File size: 856 Bytes
3d27cfe | 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 | import json
input_file = "/home/DataProcess/data/val_filtered_by_length2048_turns20_p2r.json"
output_file = "/home/DataProcess/data/val_filtered_by_length2048_turns20_restored.json"
with open(input_file, 'r', encoding='utf-8') as f:
data = json.load(f)
processed_data = []
for item in data:
chosen = item.get("chosen", [])
rejected = item.get("rejected", [])
if len(chosen) < 1 or len(rejected) < 1:
continue # 防御性跳过
# 公共 history(去掉最后一条)
history = chosen[:-1]
new_item = {
"messages": history,
"chosen": [chosen[-1]],
"rejected": [rejected[-1]]
}
processed_data.append(new_item)
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(processed_data, f, ensure_ascii=False, indent=2)
print(f"done, restored {len(processed_data)} items")
|