Prompt Engineering Fundamentals
Prompt Engineering là nghệ thuật giao tiếp với AI để đạt được kết quả mong muốn. Đây là kỹ năng quan trọng nhất khi làm việc với LLMs.
🎯 Mục tiêu
- Hiểu cấu trúc của một prompt tốt
- Áp dụng các kỹ thuật: Zero-shot, Few-shot, Chain-of-thought
- Tránh các lỗi phổ biến
- Thực hành với các use cases thực tế
1. Cấu Trúc Prompt Cơ Bản
1.1. Công thức CRAFT
1C - Context: Bối cảnh, vai trò2R - Request: Yêu cầu cụ thể3A - Action: Hành động mong muốn4F - Format: Định dạng output5T - Tone: Giọng điệuVí dụ áp dụng CRAFT
❌ Bad prompt:
1Viết email cho sếp✅ Good prompt (CRAFT):
1[Context] Bạn là nhân viên marketing đã làm việc 2 năm tại công ty.2 3[Request] Viết email xin nghỉ phép 3 ngày (từ 15-17/1) để đi du lịch với gia đình.4 5[Action] Email cần:6- Giải thích lý do7- Đề xuất người backup công việc8- Cam kết hoàn thành deadlines trước khi nghỉ9 10[Format] 11- Tiêu đề email12- Nội dung 150-200 từ13- Có phần kính thưa, nội dung, kết14 15[Tone] Lịch sự, chuyên nghiệp nhưng thân thiện2. Các Kỹ Thuật Prompt Chính
2.1. Zero-shot Prompting
Không có ví dụ - Dựa hoàn toàn vào instruction.
1Phân loại sentiment của review sau:2"Sản phẩm tuyệt vời, giao hàng nhanh, sẽ mua lại!"3 4Sentiment:Output: Positive
Khi dùng: Tasks đơn giản, phổ biến.
2.2. Few-shot Prompting
Cung cấp ví dụ để AI học pattern.
1Phân loại sentiment của review:2 3Review: "Hàng đẹp, đóng gói cẩn thận"4Sentiment: Positive5 6Review: "Giao chậm 3 ngày, không hài lòng"7Sentiment: Negative8 9Review: "Sản phẩm bình thường, giá hợp lý"10Sentiment: Neutral11 12Review: "Tệ quá, không như mô tả, đã hoàn hàng"13Sentiment:Output: Negative
Khi dùng: Tasks phức tạp hơn, cần định nghĩa rõ output format.
2.3. Chain-of-Thought (CoT)
Yêu cầu AI suy nghĩ từng bước trước khi đưa ra kết luận.
❌ Without CoT:
1Một cửa hàng có 23 quả táo. Họ bán 7 quả vào buổi sáng và 2nhận thêm 15 quả từ nhà cung cấp. Hỏi còn bao nhiêu?✅ With CoT:
1Một cửa hàng có 23 quả táo. Họ bán 7 quả vào buổi sáng và 2nhận thêm 15 quả từ nhà cung cấp. Hỏi còn bao nhiêu?3 4Hãy giải quyết từng bước:Output:
1Bước 1: Ban đầu có 23 quả táo2Bước 2: Bán 7 quả → 23 - 7 = 16 quả3Bước 3: Nhận thêm 15 quả → 16 + 15 = 31 quả4Đáp án: 31 quả táoMagic phrase: "Let's think step by step" hoặc "Giải thích reasoning của bạn"
3. Role Prompting
Gán vai trò cho AI
1Bạn là Senior Python Developer với 10 năm kinh nghiệm, 2chuyên về clean code và best practices.3 4Review đoạn code sau và đề xuất improvements:5[code]Các role hữu ích
| Role | Use case |
|---|---|
| Expert in X | Technical deep-dive |
| Teacher/Tutor | Explain concepts simply |
| Editor | Writing improvement |
| Devil's advocate | Challenge ideas |
| Interviewer | Q&A, practice |
Advanced: System Prompts
1# OpenAI API với system prompt2response = client.chat.completions.create(3 model="gpt-4",4 messages=[5 {6 "role": "system",7 "content": "Bạn là data analyst expert. Luôn giải thích code và đưa ra alternatives."8 },9 {10 "role": "user", 11 "content": "Viết SQL query để tìm top 10 customers"12 }13 ]14)4. Output Formatting
4.1. Structured Output
1Phân tích SWOT cho startup AI chatbot.2 3Format output dưới dạng:4## Strengths5- [điểm 1]6- [điểm 2]7 8## Weaknesses9...10 11## Opportunities12...13 14## Threats15...4.2. JSON Output
1Extract thông tin từ text sau thành JSON:2 3Text: "Nguyễn Văn A, sinh năm 1990, email: vana@email.com, 4làm việc tại Công ty XYZ"5 6Output JSON format:7{8 "name": "",9 "birth_year": 0,10 "email": "",11 "company": ""12}4.3. Table Format
1So sánh 3 cloud providers (AWS, Azure, GCP) theo các tiêu chí:2- Pricing3- Ease of use 4- ML services5 6Output dạng markdown table.5. Iteration và Refinement
Workflow prompt engineering
1Draft → Review → Refine → Test → DeployTechniques
1. Ask for alternatives:
1Đưa ra 3 cách khác nhau để viết email này.2. Ask for critique:
1Đánh giá response của bạn. Có gì cần cải thiện?3. Specify what to avoid:
1Viết blog post về AI.2KHÔNG bao gồm:3- Technical jargon phức tạp4- Claims không có sources5- Hơn 500 từ4. Request revisions:
1Viết lại đoạn văn trên nhưng:2- Ngắn gọn hơn 50%3- Tone professional hơn4- Thêm 1 ví dụ cụ thể6. Common Mistakes và Fixes
❌ Mistake 1: Quá mơ hồ
1❌ "Giúp tôi với code"2✅ "Debug lỗi TypeError ở dòng 15 trong Python script xử lý CSV"❌ Mistake 2: Quá dài, lan man
1❌ "Tôi đang làm project về e-commerce và cần... [500 từ context]"2✅ "Task: Generate product descriptions cho e-commerce3 Input: Product name, features (bullet points)4 Output: 100-word description, persuasive tone"❌ Mistake 3: Không specify format
1❌ "List các framework JavaScript phổ biến"2✅ "List top 5 JavaScript frameworks 2024.3 Format: Table với columns: Name, Use case, Pros, Cons"❌ Mistake 4: Ask nhiều thứ cùng lúc
1❌ "Viết code, explain, và so sánh với alternatives"2✅ Tách thành 3 prompts riêng hoặc structure rõ:3 "1. Viết code4 2. Explain từng phần5 3. So sánh với alternatives"7. Practical Templates
Template 1: Code Generation
1Language: Python2Task: [mô tả cụ thể]3Requirements:4- [requirement 1]5- [requirement 2]6Input example: [example]7Expected output: [example]8Constraints: [any limitations]9 10Generate clean, documented code with error handling.Template 2: Content Writing
1Topic: [topic]2Audience: [target audience]3Purpose: [inform/persuade/entertain]4Length: [word count]5Tone: [formal/casual/professional]6Must include: [key points]7Must avoid: [things to skip]8Format: [structure]Template 3: Analysis
1Analyze [subject] from perspective of [role/expertise].2 3Consider:41. [aspect 1]52. [aspect 2]63. [aspect 3]7 8Provide:9- Key findings (bullet points)10- Supporting evidence11- Recommendations12 13Be objective and cite reasoning.8. Hands-on Practice
Exercise 1: Improve this prompt
Original:
1Write about machine learningYour improved version:
1[Apply CRAFT framework]Exercise 2: Few-shot for custom task
Create a few-shot prompt để AI phân loại email thành:
- Urgent
- Normal
- Spam
Exercise 3: CoT for complex problem
Write a CoT prompt để AI giải bài toán: "Nếu 5 máy in 5 phút in được 5 trang, thì 100 máy in 100 phút in được bao nhiêu trang?"
📝 Quiz
-
CRAFT trong prompt engineering là gì?
- Context, Request, Action, Format, Tone
- Create, Review, Analyze, Format, Test
- Clear, Relevant, Accurate, Fast, Tested
-
Khi nào nên dùng Few-shot prompting?
- Khi task quá đơn giản
- Khi cần định nghĩa rõ output format
- Khi không có examples
- Khi token limit thấp
-
Chain-of-Thought giúp gì?
- Chạy nhanh hơn
- Tiết kiệm tokens
- Cải thiện reasoning cho tasks phức tạp
- Giảm hallucination hoàn toàn
-
Magic phrase cho CoT là?
- "Be creative"
- "Let's think step by step"
- "Give me the answer"
- "Be concise"
🎯 Key Takeaways
- Structure matters - CRAFT framework giúp organize prompts
- Examples teach - Few-shot learning rất powerful
- Step-by-step wins - CoT cho complex reasoning
- Roles shape output - System prompts định hướng AI
- Iterate always - Prompts cần refine liên tục
🚀 Bài tiếp theo
Advanced Prompt Techniques - Học các kỹ thuật nâng cao: Self-consistency, Tree of Thought, và Prompt Chaining!
