Fine-tuning basics · 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
  • Python
  • In the all labs plan

The first 2 chapters of Fine-tuning basics 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.

  1. Task 1

    to_chat.py: a row as a chat example

    Write to_chat(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 chulha and no other keys.

  2. Task 2

    write_jsonl.py: write the three files

    Split ready_rows() with split_rows (both given in pipeline.py) and, for train, val and test in that order, write train.jsonl, val.jsonl and test.jsonl in this chapter's folder: one json.dumps(to_chat(row["text"], row["tag"]), separators=(",", ":"), ensure_ascii=False) per row, in order, each line ending with \n. Print one line per file:

    NAME.jsonl: N lines, B bytes (B: len(text.encode("utf-8")))

    Your files must be byte for byte the given ones, and the lab API must accept your val.jsonl as a training file.

    Trap: json.dumps on its own puts a space after every , and :, which the given files do not have.

  3. Task 3

    validate.py: check a file the way the API does

    Write validate_jsonl(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: len(chat_ids(messages).ids)). 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: None.

  4. Task 4 · interview question

    Interview: all_problems.py, every bad line at once

    Mangala's desk fixes one bad line, uploads again, and the next job fails on the next one. Write all_problems(text): one message per bad line, in order, the message validate_jsonl gives for that line with its real line number; blank lines skipped; the file-wide 10-example rule left out.

    Under if __name__ == "__main__":, read the file named on the command line (sys.argv[1]), 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 are free. This one comes with the all labs plan.