Sub topic 3
Preparing Training Data
Assume you can call a chat-style API, read Python, and understand embeddings and context windows. This chapter focuses on fine-tuning choices after prompting and retrieval-augmented generation (RAG) have been evaluated.
Preparing training data
Fine-tuning changes model behavior through examples. Each example should show the input and the exact output you want. Remove contradictory, unsafe, and low-quality examples. Keep the output style consistent.
Chat fine-tuning data commonly uses JSONL, where each line is one JSON object:
{"messages": [{"role": "system", "content": "Write concise release notes."}, {"role": "user", "content": "Fixed a crash when exporting empty reports."}, {"role": "assistant", "content": "Fix: exporting an empty report no longer crashes the app."}]}Reserve a validation set that the training process does not use. Test it against the original model and the tuned model.
Parameter-efficient tuning with LoRA
LoRA, or low-rank adaptation, trains small additional weight matrices instead of updating every model parameter. This reduces memory use and makes experiments cheaper. The base model remains unchanged, while the LoRA adapter contains the task-specific behavior.
LoRA is appropriate when the model has the needed knowledge but repeatedly follows the wrong style, format, or procedure. It does not reliably add current facts. Use RAG for facts that change or must be cited.
Compare cost, latency, freshness, and control
| Approach | Main cost | Freshness | Control |
|---|---|---|---|
| Prompting | Tokens per request | Immediate | Instructions and examples |
| RAG | Indexing and retrieval | High | Retrieved context |
| Fine-tuning | Training and evaluation | Low unless retrained | Learned behavior |
RAG can add retrieval latency. Fine-tuning can reduce prompt length but requires data maintenance and deployment work.
A decision framework
- Build a fixed evaluation set and record a prompting baseline.
- Improve the prompt and add a few representative examples.
- If the failure is missing or private knowledge, add RAG.
- If the failure is stable behavior or formatting, consider fine-tuning.
- Compare quality, cost, latency, and operational effort before choosing.
Combining the approaches
A common system uses a fine-tuned model for consistent output, RAG for current facts, and a short prompt for task instructions. Evaluate the complete pipeline, not each component in isolation.
Maintenance and common failure modes
Watch for stale indexes, irrelevant retrievals, prompt injection in documents, data leakage, and training examples that teach accidental behavior. Re-run the evaluation set after changing data, prompts, models, or adapters.
Exercise
A model knows the policy but often returns invalid JSON. Which approach should you try first?
Show answer
Improve the prompt and add structured examples. Consider fine-tuning only if the formatting failure remains consistent across a strong baseline prompt.