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")