Fine-tuning basics (TypeScript) · Chapter 6 of 20
The chat JSONL format
By the end of this chapter you can write training data in the chat format that fine-tuning APIs take, as JSONL files equal byte for byte to the lab's, and check a file the way the API does before you upload it, finding every bad line at once instead of one per failed job. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The data
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Fine-tuning basics (TypeScript) are free. This one comes with the all labs plan.
Why you need this
The rows are clean, deduplicated and split. A fine-tuning API does not take rows: it takes a file of conversations, one per line, in a fixed shape. Get the shape wrong and the job fails, usually after an upload and a wait, with a message about one line.
Mangala's desk will add new tagged messages every month, typed by hand into the same format. Irfan wants two things: the pipeline to write the files itself, exactly, and a check that runs before the upload, says what is wrong in the API's own words, and lists every problem at once.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
to-chat.ts: a row as a chat example
Export toChat(text, tag) returning one example in the chat format: { messages: [{ role: 'system', content: PROMPT }, { role: 'user', content: text }, { role: 'assistant', content: tag }] }, with PROMPT from ../../shared/data.ts and no other fields.
- Task 2
write-jsonl.ts: write the three files
Split readyRows() with splitRows (both given in pipeline.ts) and, for train, val and test in that order, write train.jsonl, val.jsonl and test.jsonl in this chapter's folder: one JSON.stringify(toChat(row.text, row.tag)) per row, in order, each line ending with \n. Print one line per file:
NAME.jsonl: N lines, B bytes (B: Buffer.byteLength of the text)
Your files must be byte for byte the given ones, and the lab API must accept your val.jsonl as a training file.
- Task 3
validate.ts: check a file the way the API does
Export validateJsonl(text, what = 'training'): go through text.split('\n') (N, a line's number from 1; blank lines skipped but counted) and return the first problem in the API's exact words: Line N: is not valid JSON. / Line N: has no "messages" list. / Line N: "messages" is empty. / Line N: message K has role "bot"; a role is system, user or assistant. / Line N: message K has no text content. / Line N: the last message must be the assistant's (the reply to learn). / Line N: has no user message. / Line N: the example is T tokens; chota-base reads at most 32. (T: chatIds(messages).ids.length). Then for the whole file with C good examples: for training and C below 10, The training file has C examples; at least 10 are needed. (1 example, not 1 examples); with C at 0, The WHAT file has no examples. Nothing wrong: null.
- Task 4 · interview question
Interview: all-problems.ts, every bad line at once
Mangala's desk fixes one bad line, uploads again, and the next job fails on the next one. Export allProblems(text): one message per bad line, in order, the message validateJsonl gives for that line with its real line number; blank lines skipped; the file-wide 10-example rule left out.
Under if (import.meta.main), read the file named on the command line (process.argv[2]), print each problem on its own line, then N problems.
What you will learn
- The chat format: a training example is a whole conversation, ending with the reply to learn.
- JSONL: one JSON value per line, and why fine-tuning uses it.
- Validation rules: what a provider checks, line by line and for the whole file.
- Byte for byte: why rebuilding the given files exactly is the proof your pipeline is right.
More in The data
Build The chat JSONL format yourself
Chulha, a Nagpur startup, tags WhatsApp messages for 40 LPG gas agencies, and a missed gas leak is not an option. You fine-tune chota, a tiny transformer, until it beats the best prompt while forgetting little. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Fine-tuning basics (TypeScript) are free. This one comes with the all labs plan.