| """ |
| Prepare training data for MLX-LM. |
| |
| MLX-LM expects a directory containing: |
| train.jsonl - chat-format training data |
| valid.jsonl - chat-format validation data (optional) |
| test.jsonl - chat-format test data (optional) |
| |
| Each line is a JSON object with one of: |
| {"messages": [...]} (chat format, our existing format) |
| {"prompt": ..., "completion": ...} |
| {"text": "..."} |
| |
| This script splits our existing corpus into train/valid splits and writes |
| them into the MLX-LM expected directory layout. |
| |
| Usage: |
| python scripts/prepare_mlx_data.py \ |
| --input data/synthetic/corpus_50k.jsonl \ |
| --output data/mlx_dataset \ |
| --valid-count 100 \ |
| --seed 42 |
| """ |
|
|
| import argparse |
| import json |
| import random |
| from pathlib import Path |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Prepare MLX-LM training data") |
| parser.add_argument("--input", type=Path, required=True) |
| parser.add_argument("--output", type=Path, required=True) |
| parser.add_argument("--valid-count", type=int, default=100) |
| parser.add_argument("--max-samples", type=int, default=None) |
| parser.add_argument("--seed", type=int, default=42) |
| args = parser.parse_args() |
|
|
| random.seed(args.seed) |
|
|
| rows = [] |
| with args.input.open("r", encoding="utf-8") as f: |
| for line in f: |
| line = line.strip() |
| if not line: |
| continue |
| rows.append(line) |
|
|
| if args.max_samples: |
| rows = rows[: args.max_samples] |
|
|
| random.shuffle(rows) |
| valid_rows = rows[: args.valid_count] |
| train_rows = rows[args.valid_count :] |
|
|
| args.output.mkdir(parents=True, exist_ok=True) |
|
|
| train_path = args.output / "train.jsonl" |
| valid_path = args.output / "valid.jsonl" |
|
|
| with train_path.open("w", encoding="utf-8") as f: |
| f.write("\n".join(train_rows) + "\n") |
| with valid_path.open("w", encoding="utf-8") as f: |
| f.write("\n".join(valid_rows) + "\n") |
|
|
| print(f"Wrote {len(train_rows)} train rows to {train_path}") |
| print(f"Wrote {len(valid_rows)} valid rows to {valid_path}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|