1. Clear Objective
Tell AI exactly what to build
เมื่อจบบล็อกนี้ คุณจะ:
Prompt Engineering คือศิลปะในการสื่อสารกับ AI อย่างมีประสิทธิภาพ เพื่อให้ได้ผลลัพธ์ที่ต้องการ
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"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
Act as a [ROLE][TASK]Output in [FORMAT]Here's an example of what I want:Input: [example]Output: [example]
Now do the same for:Input: [your input]Think step by step:1. First, [step 1]2. Then, [step 2]3. Finally, [step 3]| 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 |
สำหรับงานที่ซับซ้อน แบ่งเป็นขั้นตอน:
Step 1: Design the data modelStep 2: Create the API endpointsStep 3: Add validationStep 4: Write testsเพิ่ม context เกี่ยวกับ domain:
For a healthcare application (HIPAA compliant):- Patient data must be encrypted- Audit logs required for all access- Session timeout: 15 minutesIteration 1: Basic implementationIteration 2: Add error handlingIteration 3: Optimize performanceIteration 4: Add documentationAI เรียกเก็บเงิน per token (หน่วยของข้อความที่ model อ่าน/สร้าง) — ยิ่ง prompt ยาวและ output ยาว เท่าไหร่ ยิ่งมีค่าใช้จ่ายมากขึ้น การเข้าใจว่าอะไร cost มากจึงช่วย control budget ได้
Goal: เขียน prompt ให้ระบุ constraints ครบ แล้ว generate createUser
ดึงไฟล์ quest ลงเครื่อง:
npx degit Poom5741/ai-sdlc-course/quests/block-2-prompt-engineering/quest-04-fix-prompt my-questcd my-questวิเคราะห์ bad prompt ใน problem.js — “Make a function that handles users”
เขียน improved prompt ระบุ constraints ครบ (input shape, email format, empty/null handling, success/error return shape) แล้วใช้ AI generate
ตรวจสอบ:
node test.jsDeliverable: createUser ที่ผ่านทุกข้อ constraint (handles / null / invalid email) + improved prompt ที่เขียนเอง
Goal: สร้าง URL validator สองชั้น (core + edge) ด้วยสอง prompts
ดึงไฟล์ quest ลงเครื่อง:
npx degit Poom5741/ai-sdlc-course/quests/block-2-prompt-engineering/quest-05-multi-step my-questcd my-questStep 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 ผ่าน)
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
Goal: Validate เบอร์โทรและบัตรประชาชนไทย โดยใส่ domain rules ลงใน prompt
ดึงไฟล์ quest ลงเครื่อง:
npx degit Poom5741/ai-sdlc-course/quests/block-2-prompt-engineering/quest-06-domain-specific my-questcd my-questอ่าน domain rules ใน problem.js (เบอร์ 10 หลัก prefix 06/08/09; บัตร 13 หลัก checksum)
เขียน prompt ที่ใส่ rules ทั้งสองชุด verbatim แล้ว generate isValidThaiPhone + isValidThaiId
ตรวจสอบ:
node test.jsDeliverable: validator สองตัวที่ผ่าน edge cases ของ format ไทย (รวม checksum ที่ AI ตัุงไม่ได้)
Block 3: Security → Learn to identify and fix security vulnerabilities in AI-generated code.
Continue to Block 3 →