AI Infrastructure & Tools10 min read

Fine-Tuning

Teach an old model new tricks β€” customize AI on your own data
scope:Advanceddifficulty:Hard

What Is Fine-Tuning?

An LLM like GPT-4 or Claude is a general-purpose model β€” it knows a lot about everything but isn't a specialist in anything. Fine-tuning is the process of giving this general model additional training on your own data so it becomes an expert in your specific domain.

Think of a doctor. In medical school, they learn everything (= pre-training). Then they specialize in cardiology with focused training (= fine-tuning). Now they're far more skilled at heart conditions than a general practitioner.

When Do You Need Fine-Tuning?

  • Specific voice/tone: Teaching the model your brand's unique writing style.
  • Specialized domain knowledge: Medical, legal, or technical terminology and patterns.
  • Consistent output format: Always responding in a specific JSON/XML structure.
  • Speed and cost: A smaller fine-tuned model often matches a larger model's performance but runs faster and cheaper.
Note: Important: Fine-tuning shouldn't be your first choice. First try good prompting (few-shot examples, system prompts). Then try RAG (searching your own documents). Only if those aren't enough should you fine-tune.

How Fine-Tuning Works

  1. Prepare data: Create input-output pairs. For example: prompt -> expected response. Usually in JSONL format.
  2. Train: The model is trained on your data for several epochs. It learns the patterns in your data.
  3. Evaluate: Test with data that wasn't used in training β€” verify the model learned well.
  4. Deploy: Start using your fine-tuned model.

Fine-Tuning vs. Other Approaches

  • Prompting: Easy, fast, no training needed. But limited for teaching complex behaviors.
  • RAG: Searches external data for answers. Easy to update. But doesn't change the model's behavior.
  • Fine-tuning: Changes the model's behavior itself. Powerful but more time and cost.

Fine-Tuning with OpenAI (Python)

from openai import OpenAI
client = OpenAI()
# 1. Upload training data (JSONL format)
# Each line in training_data.jsonl:
# {"messages": [{"role": "system", "content": "You are a..."},
# {"role": "user", "content": "..."},
# {"role": "assistant", "content": "..."}]}
file = client.files.create(
file=open("training_data.jsonl", "rb"),
purpose="fine-tune"
)
# 2. Start fine-tuning
job = client.fine_tuning.jobs.create(
training_file=file.id,
model="gpt-4o-mini-2024-07-18"
)
# 3. Use your fine-tuned model
response = client.chat.completions.create(
model="ft:gpt-4o-mini:my-org::abc123", # Your fine-tuned model
messages=[{"role": "user", "content": "Where is my order?"}]
)
Output
# Fine-tuning typically takes from a few minutes to a few hours.
# Result: The model responds in your specific tone, format,
# and with your domain knowledge.
Challenge

Quick check

What does fine-tuning do?

Continue reading