Train from the API or the CLI
Pro
Everything the Training page does also works from a script: the HTTP API on the Runtime, the TypeScript and Go client libraries, and msty-nexusctl. Use it to retrain a model each week from new data, or to run the same fine-tune on several Macs.
You need an Administration API key from API keys. The same plan and hardware rules apply as in the Console.
The steps
Section titled “The steps”A run takes four calls. Each one is described in the tables below.
- Add a dataset: upload a file, import one from the Datasets folder or a link.
- Check the run with a preflight. It returns the time and memory estimate and any warnings, and a preflight ID.
- Start the run with the preflight ID, a name, and the
aliasyour apps call. - Read the run until it’s done. Then use its version, if it isn’t in use yet.
Routes
Section titled “Routes”All routes are on the Runtime, such as http://127.0.0.1:3939.
Datasets
| Route | What it does |
|---|---|
GET /v1/training/datasets |
List datasets |
POST /v1/training/uploads, then PUT /v1/training/uploads/{id}/chunks/{index} and POST /v1/training/uploads/{id}/complete |
Upload a file in parts |
POST /v1/training/datasets/import |
Import from the Datasets folder or an https:// link |
GET /v1/training/dataset-folder |
The files in the Datasets folder |
GET /v1/training/datasets/{id} |
One dataset, with its checks. privateData.repeated lists the kinds of detail that repeat in many rows and aren’t counted. |
GET /v1/training/datasets/{id}/preview |
The first rows |
POST /v1/training/datasets/{id}/redact |
Save a redacted copy |
DELETE /v1/training/datasets/{id} |
Delete a dataset |
Runs
| Route | What it does |
|---|---|
GET /v1/training/capabilities |
What this machine can do. hasBattery says whether it has a battery. presets lists each option’s settings. |
GET /v1/training/base-models |
The models you can fine-tune here, and why others can’t be |
POST /v1/training/preflights |
Check a run before you start it: baseModelId, datasetId, preset (fast, balanced or quality) and overrides |
POST /v1/training/runs |
Start a run: preflightId, name and alias |
GET /v1/training/runs, GET /v1/training/runs/{id} |
Runs. Each has version (alias and number) and inUse. |
GET /v1/training/runs/{id}/metrics |
The loss values for the chart |
PATCH /v1/training/runs/{id} |
Rename a run |
POST /v1/training/runs/{id}/stop, /cancel, /continue |
Stop and save, cancel, or continue a stopped run |
POST /v1/training/runs/{id}/library, DELETE /v1/training/runs/{id}/library |
Add the model to your library, or remove it |
POST /v1/training/runs/{id}/use |
Make this version the one apps get |
DELETE /v1/training/runs/{id} |
Delete a run |
The comparison is in the run’s comparison. For v2 and later, againstVersion is the number of the version it was compared with. Each item has the prompt and both answers. seen is true for a Trained on question. A New question has no seen.
In the TypeScript and Go client libraries, the same calls have the same names, such as createTrainingPreflight, createTrainingRun and useTrainingRunVersion.
Errors to expect
Section titled “Errors to expect”| Code | What to do |
|---|---|
CAPABILITY_NOT_ENTITLED |
Fine-tuning needs Pro or Enterprise. |
TRAINING_UNAVAILABLE |
This machine can’t train, or the Trainer Runtime isn’t installed. |
TRAINING_BASE_MODEL_UNSUPPORTED |
Pick a model from base-models that can be fine-tuned. |
TRAINING_DATASET_INVALID |
The dataset has no usable rows. |
TRAINING_DATASET_IN_USE |
Delete the runs that use the dataset first. |
TRAINING_PREFLIGHT_STALE |
Run the preflight again, then start the run. |
TRAINING_RESOURCES_BUSY |
Another run is training on this machine. |
TRAINING_ALIAS_TAKEN |
Another model already answers to this alias. Pick another. |
TRAINING_VERSION_NOT_SERVED |
Restart MLX so it serves this version, then call use again. |
The CLI
Section titled “The CLI”msty-nexusctl training covers datasets and runs. See CLI to install it and sign in.
| Command | What it does |
|---|---|
training capabilities, training base-models |
What this machine can do, and which models it can fine-tune |
training datasets list|folder|preview|delete |
Datasets and the Datasets folder |
training datasets import --name --format --file |
Upload a file |
training datasets import-folder <file>, import-link <https-link> |
Add a file from the Datasets folder or a link |
training preflight --model --dataset [--preset quick|balanced|thorough] |
Check a run. --epochs, --learning-rate, --lora-rank and --max-length change the option’s values. |
training runs create --preflight <id> --name <name> |
Start a run |
training runs list|get|metrics|checkpoints|rename|stop|cancel|continue|delete |
Follow and manage runs |
training runs library-add|library-remove <id> |
Add a run’s model to your library, or remove it |
The CLI can’t set Answers as or switch versions yet. Use the API or the Console for that.