7 Model specialization
This chapter covers
- Adapting models to structured outputs
- Replacing long prompts through fine-tuning
- Efficient fine-tuning with LoRA and DoRA
- Specializing LLMs without losing capabilities
Throughout the previous chapters we've worked on the model's structure through pruning and distillation to gain speed without losing general capability. Now the focus shifts: we're going to adapt the LLM's behavior to our specific data and formats.
To adapt a model to a specific domain, you have two fundamentally different options. RAG gives the model access to external information at inference time: the model itself doesn't change, but it can consult a knowledge base before responding. Fine-tuning takes a different approach: instead of changing what the model sees, it modifies the model's weights directly, reshaping its behavior. This can range from adjusting how a model responds, to introducing vocabulary and concepts from a domain it barely encountered during pretraining.
One clear example is adapting the LLM’s response to structured formats. A generalist LLM tends to converse and add unnecessary explanations—like "Here's the JSON..."—that break production pipelines. Fine-tuning corrects this at the root by transferring the instructions from the prompt directly into the model's weights. The result is robust, predictable outputs that eliminate the need for lengthy prompts.