Skip to main content

Chapter 1: Instruction Tuning

Teaching Models to Follow Instructions​

Instruction tuning trains models to follow user instructions effectively.


What is Instruction Tuning?​

Training on instruction-response pairs to create helpful assistants:

{
"instruction": "Write a poem about the ocean",
"response": "Waves crash upon the shore..."
}

Dataset Requirements​

QualityRequirement
DiverseCover many instruction types
AccurateCorrect responses
HelpfulGenuinely useful answers
SafeNo harmful content

DatasetSizeQuality
Dolly 15k15KHigh
OpenAssistant161KHigh
Alpaca52KMedium
OpenHermes1M+High

Training Example​

from trl import SFTTrainer
from datasets import load_dataset

dataset = load_dataset("databricks/dolly-15k")

def format_instruction(example):
return {
"text": f"""### Instruction:
{example['instruction']}

### Response:
{example['response']}"""
}

dataset = dataset.map(format_instruction)

trainer = SFTTrainer(
model=model,
train_dataset=dataset,
dataset_text_field="text",
...
)

Continue to Chapter 2 →