import json, sys def convert(src, dst): with open(src, encoding="utf-8") as f, open(dst, "w", encoding="utf-8") as g: for line in f: rec = json.loads(line) conv = rec["conversations"] human = [c["value"] for c in conv if c["from"] == "human"] gpt = [c["value"] for c in conv if c["from"] == "gpt"] if not human or not gpt: continue out = {"instruction": human[0], "input": "\n\n".join(human[1:]) if len(human) > 1 else "", "output": "\n\n".join(gpt)} g.write(json.dumps(out, ensure_ascii=False) + "\n") if __name__ == "__main__": for split in ("train", "val", "test"): convert(f"data/{split}.jsonl", f"data/{split}_alpaca.jsonl") print("done")