@polyweave/finetuning (1.0.5)
Installation
@polyweave:registry=https://hub.kl1.tenere.ai/api/packages/Polyweave/npm/npm install @polyweave/finetuning@1.0.5"@polyweave/finetuning": "1.0.5"About this package
@polyweave/finetuning
Fine-tuning orchestration and dataset management for Polyweave. Provides dataset builder primitives (JSONL/JSON formatting with normalization) and training job state machines for LLM fine-tuning workflows.
Published as @polyweave/finetuning.
Synopsis
import { createDatasetBuilder, createTrainingJob } from '@polyweave/finetuning';
const builder = createDatasetBuilder({ format: 'jsonl', normalize: row => ({ ...row, timestamp: Date.now() }) });
const dataset = builder.buildDataset([
{ prompt: 'Hello', completion: 'Hi there' }
]);
console.log(builder.validate(dataset)); // { valid: true, count: 1 }
const job = createTrainingJob({ provider: 'openai', model: 'gpt-4o-mini', dataset });
job.start();
console.log(job.getState()); // { status: 'running', ... }
job.complete();
Install
npm install @polyweave/finetuning
Why
LLM fine-tuning requires structured dataset preparation in specific formats (JSONL for most providers, JSON for others) and managed training job lifecycles. This package provides pure data-transformation functions for dataset building with optional row-level normalization, and a simple state machine for tracking training job progress through pending→running→completed/failed transitions.
API
createDatasetBuilder(options)
Creates a dataset builder that formats arrays of rows into JSONL or JSON strings with optional normalization.
createDatasetBuilder(options?: Object): { buildDataset, validate }
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
options.format |
string |
No | 'jsonl' |
Output format: 'jsonl' or 'json' |
options.normalize |
Function |
No | — | Row normalization function (row) => normalizedRow. Applied to each row before formatting. |
Returns: An object with two methods:
buildDataset(rows)
Formats an array of rows into a string.
buildDataset(rows: Array<Object>): string
- If
normalizeis provided, each row is passed throughnormalize(row)first. - If
formatis'jsonl', each row isJSON.stringify(row)and joined with'\n'. - If
formatis'json', the entire normalized array isJSON.stringify(normalized, null, 2).
validate(dataset)
Validates a dataset.
validate(dataset: string|Array): { valid: boolean, errors?: string[], count?: number }
- If dataset is a string, it is
JSON.parse'd first. - Returns
{ valid: true, count: parsed.length }if the result is an array. - Returns
{ valid: false, errors: ['Dataset must be an array of rows'] }if not an array.
Error conditions: validate() does not throw — it catches parse errors implicitly through the Array.isArray check (a non-parseable string or non-array object returns { valid: false }).
createTrainingJob(options)
Creates a training job state machine with a simple lifecycle: pending → running → completed/failed.
createTrainingJob(options?: Object): { getState, start, complete, fail }
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
options.provider |
string |
No | 'openai' |
Provider name |
options.model |
string |
No | undefined |
Model identifier |
options.dataset |
any |
No | [] |
Training dataset |
Returns: An object with four methods:
getState()
Returns a shallow clone of the current state object. State shape:
{
status: 'pending' | 'running' | 'completed' | 'failed',
provider: string,
model: string|undefined,
dataset: any,
startedAt: string|null,
completedAt: string|null,
error: any|null
}
start()
Sets status to 'running' and startedAt to new Date().toISOString().
complete()
Sets status to 'completed' and completedAt to new Date().toISOString().
fail(error)
Sets status to 'failed', error to the provided value, and completedAt to new Date().toISOString().
Error conditions: None. All methods succeed unconditionally — this is a pure state machine, not an actual API client.
Tests
npm test
Runs: node --test test/*.js
Covers: dataset builder with jsonl/json formats, normalization, validation of arrays and non-arrays, training job lifecycle (pending→running→completed, pending→running→failed), getState immutability (shallow clone).
Requirements
- Node.js >= 22.0.0
- ESM only (
"type": "module") - No peer dependencies
- No runtime dependencies
Dependencies
Development Dependencies
| ID | Version |
|---|---|
| @rigor/core | * |