| import json |
| from pathlib import Path |
|
|
| |
| INPUT_PATH = "/root/test/weitiao/data_process_bq/data/train01_chatml_closed_length2048turns20_replaced.json" |
| OUTPUT_PATH = "/root/test/weitiao/data_process_bq/data/train01_sharegpt_closed_length_replaced.json" |
|
|
|
|
| def convert_one_sample(sample): |
| conversations = [] |
|
|
| for msg in sample.get("messages", []): |
| role = msg.get("role") |
| content = msg.get("content", "") |
|
|
| if role == "user": |
| conversations.append({ |
| "from": "human", |
| "value": content |
| }) |
| elif role == "assistant": |
| conversations.append({ |
| "from": "gpt", |
| "value": content |
| }) |
|
|
| elif role == "system": |
| conversations.append({ |
| "from": "system", |
| "value": content |
| }) |
|
|
| def convert_choice(choice_list): |
| if not choice_list: |
| return None |
| choice = choice_list[0] |
| return { |
| "from": "gpt", |
| "value": choice.get("content", "") |
| } |
|
|
| return { |
| "conversations": conversations, |
| "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() |
|
|