Nhắc vài mẫu và vai trò hội thoại

Nhập môn các mô hình Claude

Nikhil Rangarajan

Data Scientist

Vai trò hội thoại với định dạng message

  • Mảng messages mô phỏng hội thoại nhiều lượt
  • Mỗi message có một vai trò
  • Tuân theo kiểu phản hồi cụ thể
messages = [
    {"role": "user", 
 "content": "What's the weather like?"},
    {"role": "assistant", 
 "content": "I don't have access to 
     current weather data. You might 
     want to check a weather app."},
    {"role": "user", 
 "content": "How's traffic downtown?"}]

response = client.messages.create(
    model="claude-sonnet-4-6",
    messages=messages)
Nhập môn các mô hình Claude

Giới thiệu về few-shot prompting

  • Few-shot prompting dạy Claude qua ví dụ
  • Cặp user/assistant minh họa hành vi mong muốn
  • Hiệu quả với đầu vào mới

Một user và một assistant

Nhập môn các mô hình Claude

Hiểu zero-shot vs few-shot vs one-shot

$$

  • Zero-shot: Một nhắc lệnh, không ví dụ
  • One-shot: Một ví dụ trước khi làm
  • Few-shot: Nhiều ví dụ cho thấy mẫu
  • Nhiều ví dụ thường tăng tính nhất quán
Nhập môn các mô hình Claude

Ví dụ mã few-shot

messages = [{"role": "user", "content": "Translate to French: Hello"}] # Zero-shot

# One-shot messages = [{"role": "user", "content": "Translate to French: Good morning"}, {"role": "assistant", "content": "Bonjour"}, {"role": "user", "content": "Translate to French: Hello"}]
# Few-shot (nhiều ví dụ) messages = [{"role": "user", "content": "Translate to French: Good morning"}, {"role": "assistant", "content": "Bonjour"}, {"role": "user", "content": "Translate to French: Thank you"}, {"role": "assistant", "content": "Merci"}, {"role": "user", "content": "Translate to French: Hello"}]
Nhập môn các mô hình Claude

Ứng dụng thực tế của few-shot

  • Nhất quán định dạng: Hiển thị cấu trúc đầu ra mong muốn
  • Khớp giọng điệu: Thể hiện phong cách giao tiếp cụ thể
  • Lý luận phức tạp: Dẫn dắt giải bài nhiều bước
  • Chuyên môn lĩnh vực: Dạy ứng dụng kiến thức chuyên sâu
# Dạy định dạng phản hồi email
messages = [{"role": "user", "content": "Customer complaint: Late delivery"},
    {"role": "assistant", "content": "Dear Customer,\n\nWe apologize for the delay. 
     We'll track your order and update you within 24 hours.
     \n\nBest regards,\nSupport Team"},
    {"role": "user", "content": "Customer complaint: Wrong item received"}]
Nhập môn các mô hình Claude

Thực hành tốt cho few-shot prompting

Nhắc lệnh

  • Dùng 2–5 ví dụ cho hầu hết tác vụ
  • Đảm bảo ví dụ chất lượng và nhất quán
  • Đa dạng ví dụ để làm rõ mẫu, không chỉ lặp lại
  • Kiểm thử biên để xác minh độ vững
Nhập môn các mô hình Claude

Ayo berlatih!

Nhập môn các mô hình Claude

Preparing Video For Download...