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

Lesson 5: Prompt Engineering Techniques

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

Prompt Engineering Techniques

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

1. What is Prompt Engineering?

Prompt Engineering is the art of designing input (prompts) to achieve desired outputs from Foundation Models. It is the cheapest and fastest way to customize FM behavior — no training or fine-tuning required.

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

Send a prompt without any examples. The model relies entirely on its pre-trained knowledge.

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 that the model already understands well.

2.2. Few-shot Prompting

Provide a few examples before presenting the actual task. This helps the model understand the expected format and 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: When you need the model to follow a specific format or logic pattern that zero-shot doesn't achieve with sufficient quality.

2.3. One-shot Prompting

A variation of few-shot that provides only 1 example. Used when you want to set a pattern but the context window is limited.

2.4. Chain-of-Thought (CoT) Prompting

Ask the model to think step-by-step before answering. Particularly effective for math, logic, and 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" or "explain your reasoning".

3. System Prompts & Personas

System prompt defines the model's role, behavior, constraints, and output format. It "sets the stage" before 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

Explicitly specify what the model should NOT do. Especially useful in 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 with placeholders for 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

Break complex tasks into multiple sequential prompts, where the output of the previous prompt becomes the input for the next one.

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 also includes 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 the 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.