llm

Fine-tuning

Learn what Fine-tuning means in AI and machine learning, with examples and related concepts.

Definition

Fine-tuning is the process of taking a pre-trained model and training it further on a smaller, task-specific dataset to specialize its behavior.

A base LLM like GPT-4 or Claude is trained on the entire internet. Fine-tuning narrows that broad knowledge: you feed it hundreds or thousands of examples showing the exact input/output behavior you want, and the model adjusts its weights to match.

Common use cases: matching a specific writing style, learning domain terminology (medical, legal), or consistently outputting in a structured format.

How It Works

Pre-trained Model (general knowledge)

  + Your Dataset (500-10,000 examples)

  Fine-tuning Process (hours on GPU)

  Specialized Model (your behavior)

Each training example is an input-output pair:

{"messages": [
  {"role": "system", "content": "You are a customer support agent for Acme Corp."},
  {"role": "user", "content": "How do I reset my password?"},
  {"role": "assistant", "content": "Hi! To reset your password: 1. Go to acme.com/reset 2. Enter your email 3. Click the reset link sent to your inbox. Takes about 2 minutes!"}
]}

Why It Matters

When to Use Fine-tuning vs RAG vs Prompting

ApproachCostSpeedBest For
Prompt EngineeringFreeInstantMost tasks, start here
RAGLowHoursFactual Q&A, current data
Fine-tuningHighDaysStyle, format, behavior
LoRAMediumHoursResource-efficient fine-tuning

Example

# Fine-tuning with OpenAI API
from openai import OpenAI

client = OpenAI()

# 1. Upload training data
file = client.files.create(
    file=open("training_data.jsonl", "rb"),
    purpose="fine-tune"
)

# 2. Create fine-tuning job
job = client.fine_tuning.jobs.create(
    training_file=file.id,
    model="gpt-4o-mini-2024-07-18"
)

# 3. Use the fine-tuned model
response = client.chat.completions.create(
    model="ft:gpt-4o-mini:my-org:custom-model:abc123",
    messages=[{"role": "user", "content": "How do I cancel my subscription?"}]
)

Key Takeaways


Part of the DeepRaft Glossary — AI and ML terms explained for developers.