ai-tools

Fine-tuning LLMs: ปรับแต่ง AI ให้เข้ากับธุรกิจของคุณ

25 ตุลาคม 25683 นาที763 ครั้ง

เรียนรู้การ fine-tune Large Language Models ให้ตอบคำถามเฉพาะทาง เหมาะกับธุรกิจของคุณ

Fine-tuning คืออะไร?

Fine-tuning คือการนำ pre-trained model มาฝึกเพิ่มเติมด้วยข้อมูลเฉพาะของคุณ ทำให้ model เข้าใจและตอบคำถามในโดเมนของคุณได้ดีขึ้น

🤔 เมื่อไหร่ควร Fine-tune?

ไม่ต้อง Fine-tune เมื่อ:

  • GPT-4 base model ตอบได้ดีแล้ว
  • ใช้ few-shot prompting ได้ผล
  • ใช้ RAG (Retrieval Augmented Generation) แทนได้

ควร Fine-tune เมื่อ:

  • ต้องการ output format เฉพาะ
  • มี domain knowledge ที่ซับซ้อน
  • ต้องการลด cost (ใช้ smaller model)
  • ต้องการ response ที่สั้นและรวดเร็ว
  • มี style/tone เฉพาะบริษัท

🎯 Use Cases

1. Customer Support

ฝึก model ให้ตอบคำถามลูกค้าในสไตล์บริษัท:

Input: "How do I reset my password?"
Output: "Hi there! 👋 To reset your password, just click 'Forgot Password' on the login page. Check your email for a reset link. Need more help? We're here 24/7!"

2. Code Generation

ฝึก model ให้เขียนโค้ดตาม coding standards บริษัท:

Input: "Create API endpoint for user login"
Output: [Code following company's framework and style guide]

3. Content Writing

ฝึก model ให้เขียนในสไตล์แบรนด์:

Input: "Write product description for wireless earbuds"
Output: [Description matching brand voice]

📊 Fine-tuning vs Alternatives

วิธี ข้อดี ข้อเสีย เหมาะกับ
Prompt Engineering ง่าย ไม่มีค่าใช้จ่ายเพิ่ม จำกัดขนาด context งานทั่วไป
RAG Update ได้ง่าย ไม่ต้อง retrain ช้ากว่า ขึ้นกับ retrieval Q&A จาก documents
Fine-tuning Performance ดี รวดเร็ว ต้องข้อมูลเยอะ แพง งานเฉพาะทาง

🛠️ วิธี Fine-tune

1. เตรียมข้อมูล

ต้องการ training data ในรูปแบบ:

{
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is fine-tuning?"},
    {"role": "assistant", "content": "Fine-tuning is..."}
  ]
}

จำนวนข้อมูลแนะนำ:

  • Minimum: 50-100 examples
  • Good: 500+ examples
  • Great: 1000+ examples

2. Upload ข้อมูล

from openai import OpenAI

client = OpenAI()

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

print(f"File ID: {file.id}")

3. สร้าง Fine-tuning Job

# Create fine-tuning job
job = client.fine_tuning.jobs.create(
  training_file=file.id,
  model="gpt-3.5-turbo",
  hyperparameters={
    "n_epochs": 3
  }
)

print(f"Job ID: {job.id}")

4. รอและ Monitor

# Check status
status = client.fine_tuning.jobs.retrieve(job.id)
print(f"Status: {status.status}")

# List events
events = client.fine_tuning.jobs.list_events(job.id)
for event in events.data:
    print(event.message)

5. ใช้งาน Fine-tuned Model

response = client.chat.completions.create(
  model="ft:gpt-3.5-turbo:your-org:custom-model:abc123",
  messages=[
    {"role": "user", "content": "Hello!"}
  ]
)

print(response.choices[0].message.content)

💡 Best Practices

1. Data Quality > Quantity

  • 100 examples ที่ดี > 1000 examples ที่แย่
  • ตรวจสอบความถูกต้อง
  • ลบ duplicates
  • Balance ข้อมูล

2. Start Small

  • เริ่มด้วย 50-100 examples
  • ทดสอบผลลัพธ์
  • เพิ่มข้อมูลค่อยๆ

3. Validation Set

  • แยก 10-20% ไว้ validate
  • ไม่ใช้ใน training
  • เช็คว่า model ไม่ overfit

4. Hyperparameters

n_epochs: 3-4 (default: 3)
- น้อยเกินไป: underfit
- มากเกินไป: overfit

learning_rate_multiplier: auto
batch_size: auto

💰 ค่าใช้จ่าย

Training Cost (GPT-3.5-turbo):

  • $0.008 per 1K tokens
  • ตัวอย่าง: 100K tokens ≈ $0.80

Usage Cost:

  • Input: $0.003 per 1K tokens
  • Output: $0.006 per 1K tokens
  • (เท่ากับ base model)

⚠️ Common Mistakes

1. ข้อมูลน้อยเกินไป

  • ❌ 10-20 examples
  • ✅ อย่างน้อย 50 examples

2. ข้อมูลไม่สมดุล

  • ❌ 90% positive, 10% negative
  • ✅ Balance ทุก categories

3. No Validation

  • ❌ ใช้ทุกข้อมูล train
  • ✅ เก็บ validation set ไว้

4. Overfitting

  • ❌ Train epochs เยอะเกิน
  • ✅ Monitor loss, stop เมื่อ validate loss เพิ่ม

📈 การวัดผล

Metrics:

  • Training Loss: ควรลดลงเรื่อยๆ
  • Validation Loss: ไม่ควรเพิ่มขึ้น
  • Manual Evaluation: ลองใช้จริง

A/B Testing:

เปรียบเทียบ:
- Base model vs Fine-tuned model
- คะแนนจากผู้ใช้
- Response time
- Cost

🔄 Iteration

Process:

  1. รวบรวมข้อมูล initial set
  2. Fine-tune model
  3. ทดสอบในงานจริง
  4. รวบรวม failures/edge cases
  5. เพิ่มเข้า training data
  6. Fine-tune version ใหม่
  7. วนซ้ำ

🚀 Advanced Tips

1. System Message

ใช้ system message สร้าง persona:

{
  "messages": [
    {
      "role": "system",
      "content": "You are a friendly customer support agent. Always be helpful, concise, and use emojis appropriately."
    },
    ...
  ]
}

2. Multi-turn Conversations

รวมบทสนทนาหลาย turns:

{
  "messages": [
    {"role": "user", "content": "Hi"},
    {"role": "assistant", "content": "Hello! How can I help?"},
    {"role": "user", "content": "What's your return policy?"},
    {"role": "assistant", "content": "We offer 30-day returns..."}
  ]
}

3. Function Calling

Fine-tune ให้ใช้ functions ได้ดีขึ้น

สรุป

Fine-tuning เหมาะเมื่อ:

  • ✅ ต้องการ specific behavior
  • ✅ มีข้อมูล quality ดีพอ
  • ✅ ต้องการ optimize cost/latency

แต่ลองใช้ prompt engineering และ RAG ก่อน เพราะ:

  • ง่ายกว่า
  • ถูกกว่า
  • ยืดหยุ่นกว่า

Fine-tuning เป็น powerful tool แต่ใช้เมื่อจำเป็นจริงๆ!

Tags

#Fine-tuning#LLM#Machine Learning#OpenAI#GPT

แชร์บทความนี้