Fine-tuning ও Custom Model Training

লেসন ৩১ / ৪৬

🎯 Fine-tune কী?

Pre-trained model-কে আপনার specific task/data-এ further train। Better performance specific use case-এ।

🆚 RAG vs Fine-tune vs Prompt

Approachকখন
Better promptingপ্রথমে চেষ্টা — সবচেয়ে cheap
Few-shotPattern dorkar (10-50 example)
RAGUp-to-date knowledge, citation
Fine-tuneStyle/format consistency, latency, cost

✅ কখন Fine-tune

  • Specific output format consistent
  • Domain language/jargon
  • Tone/style match
  • Smaller faster model দিয়ে বড় model match
  • API cost reduce
  • Privacy (own model)

❌ কখন Fine-tune করবেন না

  • Knowledge add (RAG ভালো)
  • Frequently changing facts
  • Small dataset (<100 example)
  • Just better quality (try GPT-4 first)
  • One-time task

📚 Dataset Preparation

// JSONL format
{"messages": [
  {"role": "system", "content": "You are a customer support."},
  {"role": "user", "content": "How do I reset password?"},
  {"role": "assistant", "content": "Click 'Forgot password'..."}
]}
{"messages": [...]}

// 100-1000+ high-quality examples
// Diverse, edge case included

🔬 OpenAI Fine-tune

// Upload file
const file = await openai.files.create({
  file: fs.createReadStream("data.jsonl"),
  purpose: "fine-tune"
});

// Create job
const job = await openai.fineTuning.jobs.create({
  training_file: file.id,
  model: "gpt-4o-mini-2024-07-18"
});

// Poll status, then use:
// model: "ft:gpt-4o-mini:org:custom-name:abc123"

🪶 LoRA (Low-Rank Adaptation)

Full fine-tune-এর চেয়ে ১০০x cheap। Small adapter weights train, base frozen।

  • Memory efficient
  • Multiple adapter swap
  • Open source standard (PEFT library)
  • QLoRA — 4-bit quantized + LoRA

🛠️ Open Source Fine-tune

# Hugging Face PEFT + LoRA
from peft import LoraConfig, get_peft_model
from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3-8B")
config = LoraConfig(r=16, lora_alpha=32, target_modules=["q_proj", "v_proj"])
model = get_peft_model(model, config)
# Train on your data with Trainer

📊 Evaluation

  • Hold-out test set (separate from train)
  • Same metric as task (accuracy, F1, BLEU)
  • LLM-as-judge
  • Human eval (sample)
  • Compare baseline (no fine-tune)
  • A/B test in production

⚠️ Pitfall

  • Overfitting (small data)
  • Catastrophic forgetting (general capability lost)
  • Bias amplification
  • Cost spiral (training compute)
  • Stale (data outdated quickly)
  • Hard to debug

💰 Cost

  • OpenAI GPT-4o-mini: $3/1M training token
  • OpenAI GPT-4o: $25/1M
  • Inference: 2x base price
  • Open source LoRA: free + GPU rental ($1-5/hour)

🎓 Alternative — Distillation

Big model (GPT-4) → generate training data → fine-tune small model। সস্তা, fast inference।

🔮 RFT (Reinforcement Fine-Tuning)

OpenAI 2025 — reward model দিয়ে fine-tune। Domain expert behavior শেখানো।

💡 Decision Tree ⭐

1. Better prompt try করুন
2. Few-shot example add
3. RAG (knowledge issue হলে)
4. Stronger model (GPT-4o → o1)
5. Still issue? → Fine-tune
6. Validate ROI বাজেটের against