Zum Inhalt springen

Lektion 17 · Fine-tuning

When to Fine-tune Llama and How to Prepare Data

Decide between prompting, retrieval and fine-tuning, then prepare clean conversational training data, split it correctly and check for leaks and private data.

Fine-tuning continues training a model on your examples so it behaves differently. It is powerful and easy to misuse. Most projects should try better prompts and retrieval first.

Which tool for which problem?

Problem Try first
The model lacks current or private facts Retrieval
The answer format is inconsistent Clear prompt, schema, examples
A special tone or style is needed at scale Few-shot, then fine-tuning
A narrow task should run on a smaller model Fine-tuning
The prompt has grown very long and repetitive Fine-tuning may help

Fine-tuning is a weak way to add facts. The model may mix them up, and you cannot easily update one fact. See fine-tuning vs prompting.

Before you start

You need all of these:

  1. A test set that is never used in training.
  2. A baseline score from your best prompt on the model without fine-tuning.
  3. Enough good examples. Many narrow tasks start with a few hundred, but the right number depends on the task, so start small and measure.
  4. The right to use the data, and a license check for the model. Remember the Llama naming rule if you distribute the result.

If you cannot measure improvement, do not fine-tune yet.

The data format

For chat models, use conversations that look like production traffic. Hugging Face's TRL library accepts a conversational format with a messages list. In a JSONL file, each line holds one example. It is shown here on several lines to make it easier to read:

JSON
{"messages": [{"role": "system", "content": "You label bicycle shop messages."},
              {"role": "user", "content": "My chain keeps slipping."},
              {"role": "assistant", "content": "repair"}]}

According to the TRL documentation, the trainer applies the chat template to conversational data. Use the same system message and format in training and in production.

Data quality rules

  • Consistent: similar inputs get the same style of answer.
  • Correct: a wrong label teaches a wrong habit.
  • Varied: different wording, lengths and edge cases.
  • Balanced: do not give one class nearly all the examples without a reason.
  • Honest: include examples where the right answer is "I don't know".
  • Clean: no secrets, and no personal data without a clear legal basis and need.

A model can memorize training text and repeat it later. Assume that anything in the training data might come back out.

Check data with code

Python Validate training examples

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pattern matching finds only simple cases. Use a real review process for sensitive data.

Split without leaking

Make training, validation and test sets. Near-duplicates must not appear on both sides, or your scores look better than reality. When examples come from the same customer or document, put the whole group in one split.

Python A stable group split

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A hash keeps each group's assignment the same as you add data.

Synthetic data

You may generate examples with a model and then review them. Check the terms of the model that generates them (the checked Llama licenses apply the naming rule when outputs are used to improve a distributed model) and watch for repetitive phrasing. Keep real examples in your test set.

An example decision

A shop wants every message sorted into four labels. A good prompt with a few examples gets most cases right, but confuses "return" and "repair" for messages about broken new items. First, the team writes a clearer definition of each label and adds two examples of that confusing case. They measure again. If the errors remain, and there are enough correctly labeled real messages, a fine-tune is a reasonable experiment.

Now imagine the shop wants the model to know current prices. That is not a fine-tuning problem. Prices change, so the program should look them up with retrieval or a tool.

Writing labeling guidelines

If several people create examples, give them one written guide: what each label means, what to do with unclear cases and how long answers should be. Have two people label the same small sample and compare. If they often disagree, the task is not yet defined well enough, and a model cannot learn a rule that people cannot state.

Keep a data record

For each dataset version, note where the data came from, which filters you applied, who reviewed it, how many examples each category has and what personal data was removed. Give the file a checksum. If a user later asks for their data to be removed, or a bad batch of labels turns up, this record tells you which models are affected.

Cost of ownership

A fine-tuned model is something you have to maintain. When a better base model appears, you may need to train again and retest. When your labels or policies change, your data must change too. Include this work in your decision. A prompt change takes minutes to try and to undo, which is a real advantage.

Signs you are ready. You are ready when you can state the behavior you want in one sentence, show examples where the current system fails, measure that failure and supply clean examples of the correct behavior. If any of these is missing, work on that first.

Final check. Read twenty random training examples yourself before you train. It is the cheapest quality test you have.

Common mistakes

Do not tune settings on the test set. Once you have used it to make decisions, it no longer measures how well you do on new data. Use the validation set for decisions.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What should you try first when the model lacks fresh facts?

    Retrieval supplies current text without changing weights.
  2. 2. Why split by group, such as by customer?

    Leakage between splits inflates scores.
  3. 3. What is a risk of training on private data?

    Models can memorize training text.

Summary

  • Prefer prompting and retrieval before fine-tuning.
  • Have a baseline and an untouched test set first.
  • Use consistent conversational data that matches production.
  • Clean, validate and split data without leaks.

Next: Fine-tune Llama with LoRA and QLoRA

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.