Block 2: Prompt Engineering

10:45 - 12:15 90 minutes 3 Quests

🎯 Learning Goals

เมื่อจบบล็อกนี้ คุณจะ:

  • เข้าใจหลักการของ Prompt Engineering
  • เขียน prompts ที่มีประสิทธิภาพสำหรับ code generation
  • ใช้ multi-step prompting สำหรับงานที่ซับซ้อน
  • สร้าง domain-specific prompts

📖 Concept: Prompt Engineering (30 min)

What is Prompt Engineering?

Prompt Engineering คือศิลปะในการสื่อสารกับ AI อย่างมีประสิทธิภาพ เพื่อให้ได้ผลลัพธ์ที่ต้องการ

Why Prompt Quality Matters

Bad Prompt: "Make a function"
Good Prompt: "Create a JavaScript function called calculateDiscount that takes:
- price (number, required)
- discountPercent (number, 0-100, required)
- memberType ('gold' | 'silver' | 'bronze', optional, default: 'bronze')

Rules:
- Gold members get extra 10% off
- Discount cannot exceed 100%
- Return { originalPrice, discountAmount, finalPrice }
- Throw error for invalid inputs"

The Anatomy of a Good Prompt

target 1. Clear Objective

Tell AI exactly what to build

input 2. Input Specification

Define all parameters with types

document 3. Output Format

Specify return structure

warning 4. Constraints

List rules and limitations

Prompt Patterns

Pattern 1: Role-Task-Format

Act as a [ROLE]
[TASK]
Output in [FORMAT]

Pattern 2: Few-Shot Examples

Here's an example of what I want:
Input: [example]
Output: [example]

Now do the same for:
Input: [your input]

Pattern 3: Chain of Thought

Think step by step:
1. First, [step 1]
2. Then, [step 2]
3. Finally, [step 3]

Common Prompting Mistakes

Mistake Why It Fails Fix
Too vague AI guesses wrong Be specific about inputs/outputs
No examples No reference point Include 1-2 examples
Missing constraints Unexpected behavior List all rules
Wrong scope Too much at once Break into smaller tasks

🛠️ Advanced Techniques

Multi-Step Prompting

สำหรับงานที่ซับซ้อน แบ่งเป็นขั้นตอน:

Step 1: Design the data model
Step 2: Create the API endpoints
Step 3: Add validation
Step 4: Write tests

Domain-Specific Prompting

เพิ่ม context เกี่ยวกับ domain:

For a healthcare application (HIPAA compliant):
- Patient data must be encrypted
- Audit logs required for all access
- Session timeout: 15 minutes

Iterative Refinement

Iteration 1: Basic implementation
Iteration 2: Add error handling
Iteration 3: Optimize performance
Iteration 4: Add documentation

🎮 Code Quests

🟢 Quest 2.1: Fix the Vague Prompt

Goal: Transform a vague prompt into a specific one

  1. Analyze the bad prompt
    Make a function that handles users
  2. Identify what's missing
    • What does "handle" mean?
    • What data does a user have?
    • What validation is needed?
    • What should it return?
  3. Write an improved prompt
    Create a function called createUser that:
    - Takes { name: string, email: string }
    - Validates email format (user@domain.com)
    - Returns { success: true, id, name, email } on success
    - Returns { success: false, error: 'message' } on failure
  4. Use your prompt with an AI tool
  5. Test the generated code

Deliverable: Improved prompt + working code


🟡 Quest 2.2: Multi-Step Prompting

Goal: Break a complex task into steps

  1. The complex task: Build a todo list manager
  2. Break it down:
    • Step 1: Define data structure
    • Step 2: Implement CRUD operations
    • Step 3: Add filtering
    • Step 4: Implement file storage
    • Step 5: Create CLI interface
  3. Write prompts for each step
  4. Implement incrementally
  5. Test after each step

Deliverable: Working todo manager with all features


🔴 Quest 2.3: Domain-Specific Prompting

Goal: Write prompts for specialized domains

  1. Choose a domain: E-commerce, Healthcare, or Finance
  2. Research domain rules
    • E-commerce: product validation, tax calculation
    • Healthcare: HIPAA compliance, patient data
    • Finance: transaction validation, interest calculation
  3. Write domain-specific prompts
    Create a healthcare patient validator:
    - Validates HIPAA-compliant fields
    - Checks insurance ID format (XXX-XXXX-XXX)
    - Validates DOB (past date, max age 150)
    - Returns validation errors with compliance codes
  4. Implement the validator
  5. Test with domain-specific cases

Deliverable: Domain-specific validator with tests


✅ Block 2 Checklist

  • ☐ Understand prompt anatomy
  • ☐ Quest 2.1 completed
  • ☐ Quest 2.2 completed
  • ☐ Quest 2.3 completed

🚀 Next Block

Block 3: Security → Learn to identify and fix security vulnerabilities in AI-generated code.

Continue to Block 3 →