1. Clear Objective
บอก AI ชัดเจนว่าจะสร้างอะไร
เมื่อจบ block นี้ คุณจะสามารถ:
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
บอก AI ชัดเจนว่าจะสร้างอะไร
2. Input Specification
กำหนด parameters ทั้งหมดพร้อม types
3. Output Format
ระบุโครงสร้าง return
4. Constraints
ระบุ rules และ limitations
Act as a [ROLE][TASK]Output in [FORMAT]นี่คือตัวอย่างที่ฉันต้องการ:Input: [ตัวอย่าง]Output: [ตัวอย่าง]
ทำแบบเดียวกันสำหรับ:Input: [input ของคุณ]คิดทีละ step:1. ก่อน, [step 1]2. แล้ว, [step 2]3. สุดท้าย, [step 3]| ความผิดพลาด | ทำไมถึงล้มเหลว | แก้ไข |
|---|---|---|
| ค่อนข้าง vague | AI เดาผิด | ระบุ inputs/outputs ให้ชัดเจน |
| ไม่มีตัวอย่าง | ไม่มีจุดอ้างอิง | ใส่ตัวอย่าง 1-2 ตัว |
| ไม่มี constraints | พฤติกรรมไม่คาดคิด | ระบุ rules ทั้งหมด |
| ขอบเขตผิด | ทำมากเกินไป | แบ่งเป็น tasks เล็กๆ |
สำหรับ tasks ที่ซับซ้อน แบ่งเป็น steps:
Step 1: ออกแบบ data modelStep 2: สร้าง API endpointsStep 3: เพิ่ม validationStep 4: เขียน 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 คิดค่าต่อ token (หน่วยข้อความที่ model อ่าน/สร้าง) — prompt และ output ยาวขึ้น ต้นทุนสูงขึ้น การเข้าใจสิ่งที่คิดตังแพงกว่าช่วยควบคุมงบประมาณ
ราคาตัวอย่าง (ต่อ 1M tokens):
| Model | Input | Output | เมื่อไหร่ควรใช้ |
|---|---|---|---|
| GPT-4o | $2.50 | $10.00 | Complex tasks, code generation |
| GPT-4o-mini | $0.15 | $0.60 | Simple tasks, classification |
| Claude 3.5 Sonnet | $3.00 | $15.00 | Complex tasks, analysis |
| Claude 3.5 Haiku | $0.25 | $1.25 | Routine tasks, drafts |
ปัจจัยที่ส่งผลต่อต้นทุน:
| Task | แนะนำ | เหตุผล |
|---|---|---|
| Simple classification | Haiku/Mini | เร็ว, ถูก, พอใช้ |
| Code generation | Sonnet/GPT-4o | สมดุล คุณภาพ/ต้นทุน |
| Complex reasoning | Opus/GPT-4 | คุณภาพสูงเมื่อต้องการ |
| Drafting text | Haiku/Mini | Iterate เร็ว, ต้นทุนต่ำ |
เป้าหมาย: เขียน prompt พร้อม constraints ครบถ้วน จากนั้นสร้าง createUser
Download quest files:
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”
เขียน prompt ที่ดีขึ้น พร้อม constraints ครบ (input shape, email format, empty/null handling, success/error return shape) จากนั้นใช้ AI สร้าง
ตรวจสอบ:
node test.jsส่งมอบ: createUser ผ่าน constraints ทั้งหมด (handles / null / invalid email) + prompt ที่ปรับปรุงแล้ว
เป้าหมาย: สร้าง URL validator แบบสองชั้น (core + edge) ด้วยสอง prompts
Download quest files:
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:// with no host → reject all.” → implement → node test.js (ทั้งหมดผ่าน)
ส่งมอบ: isValidUrl สองชั้น ผ่าน Layer 1 + Layer 2 edge cases
เป้าหมาย: Validate เบอร์โทรศัพท์ไทยและหมายเลขบัตรประชาชน โดยระบุ domain rules ใน prompt
Download quest files:
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 ทั้งสองชุด จากนั้นสร้าง isValidThaiPhone + isValidThaiId
ตรวจสอบ:
node test.jsส่งมอบ: Validators สองตัว ผ่าน Thai format edge cases (รวม checksum ที่ AI เดาไม่ได้)
Block 3: Security → เรียนรู้การระบุและแก้ไขช่องโหว่ความปลอดภัยในโค้ดที่สร้างด้วย AI
ไป Block 3 →