Lý thuyết
40 phút
Bài 4/6

Prompt Engineering Fundamentals

Nắm vững các kỹ thuật prompt engineering cơ bản - cấu trúc prompt, few-shot learning, và chain-of-thought

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

Text
1C - Context: Bối cảnh, vai trò
2R - Request: Yêu cầu cụ thể
3A - Action: Hành động mong muốn
4F - Format: Định dạng output
5T - Tone: Giọng điệu

Ví dụ áp dụng CRAFT

Bad prompt:

Text
1Viết email cho sếp

Good prompt (CRAFT):

Text
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ý do
7- Đề xuất người backup công việc
8- Cam kết hoàn thành deadlines trước khi nghỉ
9
10[Format]
11- Tiêu đề email
12- Nội dung 150-200 từ
13- Có phần kính thưa, nội dung, kết
14
15[Tone] Lịch sự, chuyên nghiệp nhưng thân thiện

2. 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.

Text
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.

Text
1Phân loại sentiment của review:
2
3Review: "Hàng đẹp, đóng gói cẩn thận"
4Sentiment: Positive
5
6Review: "Giao chậm 3 ngày, không hài lòng"
7Sentiment: Negative
8
9Review: "Sản phẩm bình thường, giá hợp lý"
10Sentiment: Neutral
11
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:

Text
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:

Text
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:

Text
1Bước 1: Ban đầu có 23 quả táo
2Bướ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áo

Magic 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

Text
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

RoleUse case
Expert in XTechnical deep-dive
Teacher/TutorExplain concepts simply
EditorWriting improvement
Devil's advocateChallenge ideas
InterviewerQ&A, practice

Advanced: System Prompts

Python
1# OpenAI API với system prompt
2response = 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

Text
1Phân tích SWOT cho startup AI chatbot.
2
3Format output dưới dạng:
4## Strengths
5- [điểm 1]
6- [điểm 2]
7
8## Weaknesses
9...
10
11## Opportunities
12...
13
14## Threats
15...

4.2. JSON Output

Text
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

Text
1So sánh 3 cloud providers (AWS, Azure, GCP) theo các tiêu chí:
2- Pricing
3- Ease of use
4- ML services
5
6Output dạng markdown table.

5. Iteration và Refinement

Workflow prompt engineering

Text
1Draft → Review → Refine → Test → Deploy

Techniques

1. Ask for alternatives:

Text
1Đưa ra 3 cách khác nhau để viết email này.

2. Ask for critique:

Text
1Đánh giá response của bạn. Có gì cần cải thiện?

3. Specify what to avoid:

Text
1Viết blog post về AI.
2KHÔNG bao gồm:
3- Technical jargon phức tạp
4- Claims không có sources
5- Hơn 500 từ

4. Request revisions:

Text
1Viết lại đoạn văn trên nhưng:
2- Ngắn gọn hơn 50%
3- Tone professional hơn
4- Thêm 1 ví dụ cụ thể

6. Common Mistakes và Fixes

❌ Mistake 1: Quá mơ hồ

Text
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

Text
1❌ "Tôi đang làm project về e-commerce và cần... [500 từ context]"
2✅ "Task: Generate product descriptions cho e-commerce
3 Input: Product name, features (bullet points)
4 Output: 100-word description, persuasive tone"

❌ Mistake 3: Không specify format

Text
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

Text
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 code
4 2. Explain từng phần
5 3. So sánh với alternatives"

7. Practical Templates

Template 1: Code Generation

Text
1Language: Python
2Task: [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

Text
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

Text
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 evidence
11- Recommendations
12
13Be objective and cite reasoning.

8. Hands-on Practice

Exercise 1: Improve this prompt

Original:

Text
1Write about machine learning

Your improved version:

Text
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

  1. CRAFT trong prompt engineering là gì?

    • Context, Request, Action, Format, Tone
    • Create, Review, Analyze, Format, Test
    • Clear, Relevant, Accurate, Fast, Tested
  2. 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
  3. 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
  4. Magic phrase cho CoT là?

    • "Be creative"
    • "Let's think step by step"
    • "Give me the answer"
    • "Be concise"

🎯 Key Takeaways

  1. Structure matters - CRAFT framework giúp organize prompts
  2. Examples teach - Few-shot learning rất powerful
  3. Step-by-step wins - CoT cho complex reasoning
  4. Roles shape output - System prompts định hướng AI
  5. 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!