File size: 3,361 Bytes
1c2019e | 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #!/usr/bin/env python3
"""jsonl (list of {system, prompt, response}) -> Alpaca jsonl converter.
Each input line is expected to be a JSON list containing one dict with keys:
- system -> instruction
- prompt -> input
- response -> output
Falls back gracefully if the line is itself a dict.
"""
import argparse
import glob
import json
import os
import random
import sys
from pathlib import Path
def iter_records(path: str):
with open(path, "r", encoding="utf-8") as fp:
for line_no, line in enumerate(fp, 1):
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
except Exception as e:
print(f"[WARN] {path}:{line_no} JSON parse error: {e}", file=sys.stderr)
continue
if isinstance(obj, list):
if not obj:
continue
obj = obj[0]
if not isinstance(obj, dict):
print(f"[WARN] {path}:{line_no} unexpected type {type(obj)}", file=sys.stderr)
continue
system = obj.get("system", "") or ""
prompt = obj.get("prompt", "") or ""
response = obj.get("response", "") or ""
if not prompt and not response:
continue
yield {
"instruction": system,
"input": prompt,
"output": response,
"history": [],
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--input", required=True,
help="jsonl file/dir/glob (directory will be scanned for *.jsonl)")
parser.add_argument("--output", required=True, help="output Alpaca jsonl path")
parser.add_argument("--shuffle", action="store_true")
parser.add_argument("--shuffle-seed", type=int, default=2026)
args = parser.parse_args()
in_path = Path(args.input)
if in_path.is_dir():
files = sorted(str(p) for p in in_path.rglob("*.jsonl"))
elif any(c in args.input for c in "*?["):
files = sorted(glob.glob(args.input, recursive=True))
else:
files = [args.input]
if not files:
print(f"[ERROR] no jsonl files found under {args.input}", file=sys.stderr)
sys.exit(1)
print(f"[INFO] found {len(files)} jsonl files", file=sys.stderr)
for f in files:
print(f" - {f}", file=sys.stderr)
records = []
per_file_counts = {}
for f in files:
before = len(records)
for rec in iter_records(f):
records.append(rec)
per_file_counts[f] = len(records) - before
print(f"[INFO] {os.path.basename(f)}: {per_file_counts[f]} records", file=sys.stderr)
print(f"[INFO] total records: {len(records)}", file=sys.stderr)
if args.shuffle:
random.seed(args.shuffle_seed)
random.shuffle(records)
print(f"[INFO] shuffled with seed {args.shuffle_seed}", file=sys.stderr)
out_path = Path(args.output)
out_path.parent.mkdir(parents=True, exist_ok=True)
with open(out_path, "w", encoding="utf-8") as fp:
for r in records:
fp.write(json.dumps(r, ensure_ascii=False) + "\n")
print(f"[OK] wrote {len(records)} records to {out_path}", file=sys.stderr)
if __name__ == "__main__":
main()
|