import json INPUT_PATH = "/root/test/weitiao/data_process_bq/data3/result/M_sharegpt_data3_all_order_perfect_neutral_fade.json" OUTPUT_PATH = "/root/test/weitiao/data_process_bq/data3/result/M_chatml_data3_all_order_perfect_neutral_fade.json" def convert_one_sample(sample): messages = [] for msg in sample.get("conversations", []): role_map = { "system": "system", "human": "user", "gpt": "assistant" } from_role = msg.get("from") if from_role not in role_map: continue messages.append({ "role": role_map[from_role], "content": msg.get("value", "") }) def convert_choice(choice): if not choice: return [] return [{ "role": "assistant", "content": choice.get("value", "") }] return { "messages": messages, "chosen": convert_choice(sample.get("chosen")), "rejected": convert_choice(sample.get("rejected")) } def main(): with open(INPUT_PATH, "r", encoding="utf-8") as f: data = json.load(f) assert isinstance(data, list), "输入 JSON 顶层必须是 list" converted = [] for idx, sample in enumerate(data): try: converted.append(convert_one_sample(sample)) except Exception as e: print(f"[WARN] 第 {idx} 条样本转换失败: {e}") with open(OUTPUT_PATH, "w", encoding="utf-8") as f: json.dump(converted, f, ensure_ascii=False, indent=2) print(f"反向转换完成,共 {len(converted)} 条") print(f"输出路径:{OUTPUT_PATH}") if __name__ == "__main__": main()