Big models are smart, but slow, and expensive to run. Small models are fast, and cheap. What if a big model could teach a small one? That's called distillation.
The big model is called the teacher. The small one is the student. First, the teacher answers many real inputs. Then we fine-tune the student on those answers. The student learns to copy the teacher, on just our task.
Here's the idea in code. A pipeline loads a large teacher model. On a laptop, this would be far too slow, so it's shown, not run. We read a file of unlabelled customer messages. For each one, the teacher writes an answer. And we save the pairs, ready to train our small student.
distil.pypython
import jsonfrom transformers import pipelinefrom chat import SYSTEMteacher = pipeline("text-generation", model="Qwen/Qwen2.5-72B-Instruct") # big, slowunlabelled = open("new_messages.txt").read().splitlines()with open("distilled.jsonl", "w") as f: for message in unlabelled: messages = [{"role": "system", "content": SYSTEM}, {"role": "user", "content": message}] answer = teacher(messages, max_new_tokens=40)[0]["generated_text"][-1]["content"] f.write(json.dumps({"message": message, "answer": answer}) + "\n")# then: check the answers, and fine-tune a small student on distilled.jsonl
Always check the teacher's answers, because it makes mistakes too. Read the licence of the teacher model, because some forbid training other models on their outputs. And remember, the student usually can't beat its teacher.
Let's recap. A teacher model labels inputs. A small student learns from those labels. Check the answers, and the licence. Next, when you shouldn't fine-tune at all.