Skip to content

Block 2 - Prompt Engineering

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

1. Clear Objective

Tell AI exactly what to build

2. Input Specification

Define all parameters with types

3. Output Format

Specify return structure

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

MistakeWhy It FailsFix
Too vagueAI guesses wrongBe specific about inputs/outputs
No examplesNo reference pointInclude 1-2 examples
Missing constraintsUnexpected behaviorList all rules
Wrong scopeToo much at onceBreak 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

💰 Token Cost Management

รู้ Token Economics

AI เรียกเก็บเงิน per token (หน่วยของข้อความที่ model อ่าน/สร้าง) — ยิ่ง prompt ยาวและ output ยาว เท่าไหร่ ยิ่งมีค่าใช้จ่ายมากขึ้น การเข้าใจว่าอะไร cost มากจึงช่วย control budget ได้

เลือก Model ระหว่าง Cost กับ Quality

  • Model ใหญ่ (เช่น high-capability frontier model) — quality ดี แต่ cost ต่อ token สูง เหมาะกับงานซับซ้อน
  • Model เล็ก/เร็ว — ถูกกว่า เหมาะกับงาน routine เช่น formatting, refactor เล็กๆ, auto-complete
  • Strategy: ใช้ model ใหญ่เฉพาะขั้นตอนที่ต้อง reasoning ลึก ใช้ model เล็กกับงานที่ซ้ำๆ

Basic Optimization

  • Trim context — ตัดส่วนที่ไม่เกี่ยวข้องออกจาก prompt เพื่อลด token ที่ model ต้องอ่าน
  • Fewer round-trips — รวมคำถามหลายข้อใน prompt เดียวแทนการถามทีละข้อ
  • Cache/reuse — อย่า re-generate อะไรที่ได้ผลดีแล้วซ้ำๆ

🎮 Code Quests

🟢 Quest 2.1: Fix the Vague Prompt

Goal: เขียน prompt ให้ระบุ constraints ครบ แล้ว generate createUser

  1. ดึงไฟล์ quest ลงเครื่อง:

    Terminal window
    npx degit Poom5741/ai-sdlc-course/quests/block-2-prompt-engineering/quest-04-fix-prompt my-quest
    cd my-quest
  2. วิเคราะห์ bad prompt ใน problem.js — “Make a function that handles users”

  3. เขียน improved prompt ระบุ constraints ครบ (input shape, email format, empty/null handling, success/error return shape) แล้วใช้ AI generate

  4. ตรวจสอบ:

    Terminal window
    node test.js

Deliverable: createUser ที่ผ่านทุกข้อ constraint (handles / null / invalid email) + improved prompt ที่เขียนเอง


🟡 Quest 2.2: Multi-Step Prompting

Goal: สร้าง URL validator สองชั้น (core + edge) ด้วยสอง prompts

  1. ดึงไฟล์ quest ลงเครื่อง:

    Terminal window
    npx degit Poom5741/ai-sdlc-course/quests/block-2-prompt-engineering/quest-05-multi-step my-quest
    cd my-quest
  2. Step 1 prompt (core): “Write isValidUrl(url) returning true for well-formed http/https URLs with a host, false otherwise.” → implement → node test.js (Layer 1 ผ่าน)

  3. Step 2 prompt (edge): “Now handle: empty, whitespace, javascript: scheme, no-protocol, https:// ไม่มี host → reject all.” → implement → node test.js (ผ่านครบ)

Deliverable: isValidUrl สองชั้นผ่าน Layer 1 + Layer 2 edge cases


🔴 Quest 2.3: Domain-Specific Prompting

Goal: Validate เบอร์โทรและบัตรประชาชนไทย โดยใส่ domain rules ลงใน prompt

  1. ดึงไฟล์ quest ลงเครื่อง:

    Terminal window
    npx degit Poom5741/ai-sdlc-course/quests/block-2-prompt-engineering/quest-06-domain-specific my-quest
    cd my-quest
  2. อ่าน domain rules ใน problem.js (เบอร์ 10 หลัก prefix 06/08/09; บัตร 13 หลัก checksum)

  3. เขียน prompt ที่ใส่ rules ทั้งสองชุด verbatim แล้ว generate isValidThaiPhone + isValidThaiId

  4. ตรวจสอบ:

    Terminal window
    node test.js

Deliverable: validator สองตัวที่ผ่าน edge cases ของ format ไทย (รวม checksum ที่ AI ตัุงไม่ได้)


✅ 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 →