Chuyển đến nội dung chính

Bài 5: Prompt Engineering Techniques

Zero-shot, few-shot, Chain-of-Thought prompting. System prompts, prompt templates, negative prompting. Prompt engineering best practices cho AWS AI Practitioner exam.

Prompt Engineering Techniques

Prompt Engineering Techniques: Zero-shot, Few-shot và Chain-of-Thought

1. What is Prompt Engineering?

Prompt Engineering là nghệ thuật thiết kế input (prompt) để nhận được output mong muốn từ Foundation Models. Đây là cách rẻ nhất và nhanh nhất để customize FM behavior — không cần training hay fine-tuning.

1.1. Components of a Prompt

┌────────────────────────────────────────────┐
│  SYSTEM PROMPT (optional)                  │
│  "You are a helpful AWS solutions          │
│   architect. Answer concisely."            │
├────────────────────────────────────────────┤
│  CONTEXT (optional)                        │
│  Background info, documents, data          │
├────────────────────────────────────────────┤
│  USER PROMPT (required)                    │
│  The actual question or instruction        │
├────────────────────────────────────────────┤
│  EXAMPLES (optional, for few-shot)         │
│  Input → Output pairs                     │
├────────────────────────────────────────────┤
│  OUTPUT FORMAT (optional)                  │
│  "Respond in JSON", "Use bullet points"    │
└────────────────────────────────────────────┘

2. Prompting Techniques

2.1. Zero-shot Prompting

Gửi prompt không có ví dụ. Model dựa hoàn toàn vào knowledge đã học.

Prompt: "Classify the sentiment of this review:
'The product arrived damaged and customer service was unhelpful.'

Sentiment:"

Output: "Negative"

When to use: Simple, well-defined tasks mà model đã hiểu rõ.

2.2. Few-shot Prompting

Cung cấp một vài examples trước khi đưa ra task thực tế. Giúp model hiểu expected format và logic.

Prompt: "Classify these reviews:

Review: 'Amazing quality, fast shipping!' → Positive
Review: 'Terrible experience, never again.' → Negative
Review: 'It's okay, nothing special.' → Neutral

Review: 'The product exceeded my expectations!' →"

Output: "Positive"

When to use: Khi cần model tuân theo specific format hoặc logic pattern mà zero-shot không đạt đủ chất lượng.

2.3. One-shot Prompting

Variation của few-shot nhưng chỉ cung cấp 1 example. Dùng khi bạn muốn set pattern nhưng context window bị hạn chế.

2.4. Chain-of-Thought (CoT) Prompting

Yêu cầu model suy nghĩ step-by-step trước khi trả lời. Đặc biệt hiệu quả cho math, logic, và reasoning tasks.

WITHOUT CoT:
Q: "If a store has 3 boxes with 12 apples each, and gives
    away 15 apples, how many are left?"
A: "21" (might be wrong without reasoning)

WITH CoT:
Q: "Think step by step: If a store has 3 boxes with 12
    apples each, and gives away 15 apples, how many are left?"
A: "Step 1: Total apples = 3 × 12 = 36
    Step 2: After giving away = 36 - 15 = 21
    Answer: 21 apples"

Exam tip: "Which prompting technique improves reasoning accuracy?" → Chain-of-Thought. Key phrase: "think step by step" hoặc "explain your reasoning".

3. System Prompts & Personas

System prompt defines the model's role, behavior, constraints, and output format. Nó "sets the stage" trước khi user interaction.

System Prompt:
"You are a financial advisor AI for XYZ Bank.
Rules:
- Only answer questions about banking and investments
- Never provide specific stock recommendations
- Always include a disclaimer
- Respond in a professional tone
- If asked about non-financial topics, politely redirect"

System Prompt Best Practices:

PracticeWhy
Define a clear roleConstrains model behavior to a domain
Set boundariesPrevents off-topic or harmful responses
Specify output formatEnsures consistent, parseable outputs
Include examplesClarifies expected behavior
Add guardrailsPrevents misuse (PII, harmful content)

4. Advanced Prompting Techniques

4.1. Negative Prompting

Chỉ rõ những gì model KHÔNG nên làm. Đặc biệt useful trong image generation.

Text generation:
"Summarize this article. Do NOT include opinions
or personal commentary. Do NOT exceed 100 words."

Image generation (Stable Diffusion):
Prompt: "Professional headshot, studio lighting"
Negative prompt: "blurry, cartoon, distorted, low quality"

4.2. Prompt Templates

Reusable prompt structures với placeholders cho dynamic content:

Template:
"Given the following {document_type}:
---
{content}
---
Extract the following information:
- {field_1}
- {field_2}
- {field_3}
Respond in JSON format."

4.3. Prompt Chaining

Chia complex tasks thành multiple sequential prompts, output của prompt trước trở thành input của prompt sau.

Step 1: "Extract key entities from this document: {doc}"
         → Output: list of entities

Step 2: "For each entity {entities}, find the sentiment
         expressed about it in this text: {doc}"
         → Output: entity-sentiment pairs

Step 3: "Create a summary report of sentiment analysis
         for these entities: {entity_sentiments}"
         → Output: Final report

5. Comparison Table for Exam

TechniqueExamples Given?Best ForExam Keyword
Zero-shotNoneSimple, well-known tasks"no examples provided"
One-shot1 exampleSetting format with minimal context"single example"
Few-shot2-5 examplesPattern following, classification"examples provided", "demonstrations"
Chain-of-ThoughtWith reasoning stepsMath, logic, complex reasoning"step by step", "reasoning"
Negative promptingN/AAvoiding unwanted outputs"do not include", "avoid"
Prompt chainingN/AComplex multi-step tasks"break into steps", "sequential"

6. Inference Parameters Review

Prompt engineering cũng bao gồm tuning inference parameters:

ParameterLow ValueHigh Value
TemperatureDeterministic, factual (0.0-0.3)Creative, diverse (0.7-1.0)
Top-pFocused vocabulary (0.1-0.3)Diverse vocabulary (0.9-1.0)
Top-kLimited choices (e.g., 10)More choices (e.g., 250)
Max tokensShort responsesLong responses
Stop sequencesDefine when to stop generating

Exam tip: "A customer support chatbot gives inconsistent answers" → Lower temperature (closer to 0). "A creative writing app produces boring text" → Raise temperature (closer to 1).

7. Prompt Engineering Best Practices

  1. Be specific: "Summarize in 3 bullet points" > "Summarize this"
  2. Provide context: Include relevant background information
  3. Define output format: JSON, markdown, table, bullet points
  4. Use delimiters: Separate sections with --- or ``` to avoid prompt injection
  5. Iterate: Test and refine prompts based on outputs
  6. Avoid ambiguity: Don't assume model knows your intent
  7. Use examples: When zero-shot doesn't work, add few-shot examples

8. Practice Questions

Q1: A developer is working on a classification task, but the model's zero-shot responses are inconsistent. Which prompting technique should the developer try NEXT?

  • A) Reduce the temperature to 0
  • B) Use few-shot prompting with example inputs and outputs ✓
  • C) Fine-tune the model on custom data
  • D) Switch to a different model provider

Explanation: Few-shot prompting is the logical next step after zero-shot fails — providing examples helps the model understand the expected pattern. Fine-tuning is more expensive and complex. Temperature adjustment alone may not fix classification logic.

Q2: A customer wants their AI application to solve complex mathematical word problems more accurately. Which prompting technique would MOST improve the results?

  • A) Zero-shot prompting
  • B) Negative prompting
  • C) Chain-of-Thought prompting ✓
  • D) Prompt chaining

Explanation: Chain-of-Thought prompting encourages the model to show its reasoning step by step, which significantly improves accuracy on mathematical and logical reasoning tasks.

Q3: Which of the following is a benefit of using a system prompt in a generative AI application?

  • A) It eliminates the need for user input
  • B) It reduces the model's inference cost
  • C) It defines the model's role, behavior, and constraints ✓
  • D) It replaces the need for fine-tuning

Explanation: System prompts set the model's role, behavioral constraints, and output format — establishing consistent behavior across all user interactions without any model training.