Prompt Engineering 101

พื้นฐาน + production patterns สำหรับ LLM ทุกค่าย

v1 · 9 พ.ค. 2569 · 2 tracks · ~25-90 นาที

THมี English ด้วย

สิ่งที่จะได้เรียนรู้

Track A

พื้นฐาน

  • Mental model
  • Prompt elements + LLM settings
  • Zero-shot vs few-shot
  • Chain-of-Thought
  • เมื่อไหร่ PE ไม่ ใช่ทาง
Track B

Production patterns

  • System prompts
  • Structured output
  • Chaining + tool use + RAG
  • Eval-driven development
  • Injection + defensive prompting
  • Agent patterns

Prompt engineering คืออะไร?

ศาสตร์ของการสื่อสารกับ LLM เพื่อบังคับพฤติกรรม โดยไม่ต้องแก้ weights ของโมเดล

"Methods for how to communicate with LLM to steer its behavior for desired outcomes without updating the model weights." — Lilian Weng

  • เปลี่ยน input, ไม่ใช่ parameter
  • ปุ่มที่ดึงคือ text + settings + examples
  • เป้าหมาย: output ที่เชื่อถือได้และวัดผลได้

3 prerequisites ก่อนเริ่ม

ตามเอกสาร Anthropic — ก่อน PE จะช่วย ต้องมี:

  1. นิยาม เกณฑ์ความสำเร็จ ที่ชัดเจน
  2. วิธี ทดสอบเชิงประจักษ์ เทียบกับเกณฑ์นั้น
  3. Prompt ฉบับร่างแรก ที่อยากปรับ

ไม่มีครบ 3 = ปรับในที่มืด แก้ตรงนั้นก่อน

เมื่อไหร่ PE คือ (และไม่ใช่) ทางที่ถูก

ปัญหาเครื่องมือที่ถูก
Output format ผิดPrompt engineering
คุณภาพ output ต่ำPrompt engineering
Latency สูงเกินเปลี่ยนโมเดลให้เล็ก/เร็วกว่า
Cost สูงเกินเปลี่ยนโมเดล / architecture
Knowledge cutoffRAG (retrieval)
พฤติกรรมเสถียรที่ scaleFine-tuning

Anatomy: 4 องค์ประกอบของ prompt

องค์ประกอบตอบคำถาม
Instructionทำอะไร
Contextพื้นหลังที่บอก
Input dataทำกับอะไร
Output indicatorformat / structure

"Show your prompt to a colleague with minimal context. If they'd be confused, Claude will be too."

Bare instruction vs full prompt

❌ "Summarise this."

✅ "Summarise the following customer review for our weekly report.
   Focus on actionable feedback. Output 3 bullets, ≤15 words each.
   <review>{{text}}</review>"

Full version: instruction + context + input + output indicator

6 settings สากลของ LLM

Settingทำอะไร
temperatureความสุ่ม — 0 = deterministic, สูง = สร้างสรรค์
top_pNucleus sampling (ใช้แทน temperature, อย่าใช้พร้อมกัน)
max_lengthจำกัด output tokens
stopString ที่หยุด generation
frequency_penaltyลดการพูด token ซ้ำ
presence_penaltyลดการพูดเรื่องซ้ำ

Temperature คุม ความหลากหลาย, ไม่ใช่ คุณภาพ

เฉพาะ Claude: effort

Levelเมื่อไหร่
maxReasoning หนักสุด อาจ over-think
xhighCoding / agentic work
highDefault สำหรับ task ที่ต้องการ intelligence
mediumCost-sensitive แลกกับ intelligence
lowLatency-sensitive, scoped tasks

CLAUDE 4.x ไม่มีตัวเทียบโดยตรงข้าม vendor

Zero-shot — เริ่มที่นี่เสมอ

ส่ง task ไปโดย ไม่มี example โมเดล instruction-tuned ปัจจุบันจัดการได้:

  • Sentiment classification, summarisation, translation
  • Extraction tasks ที่ schema ชัดเจน
  • Generation ที่มี constraint ง่าย ๆ

ถ้าใช้ได้ → จบ ไม่ต้องเพิ่ม examples เพื่อความสบายใจ

Few-shot — เมื่อ zero-shot ไม่พอ

Classify as: BILLING, BUG, FEATURE, OTHER

Example: "Charges me twice every month."  → BILLING
Example: "Click 'Save', page reloads."    → BUG
Example: "Could you add dark mode?"       → FEATURE

Now: "I love the new logo!"
→

Sweet spot: 2-5 examples. >10 → diminishing returns; พิจารณา RAG

Bias gotchas ของ Few-shot

3 bias ที่งานวิจัยพบ (Zhao et al., 2021):

  • Majority label bias — เลือก label ที่มีบ่อย
  • Recency bias — ทำซ้ำ label ของ example สุดท้าย
  • Common token bias — เลือก token ที่บ่อย

วิธีลด

  • สมดุล label distribution
  • สลับลำดับ examples
  • Examples ที่ดีดีกว่า instructions แบบ "อย่าทำ"

Chain-of-Thought (CoT)

ขอให้โมเดล คิดออกเสียง ก่อนตอบ

Let's think step by step.
Show your reasoning before answering.

ดีมากกับ math + multi-step logic ไม่ช่วย translation หรือ recall

Decision tree — เครื่องมือที่ใช่

ปัญหา "วิธี" ที่โมเดลตอบ?
  → prompt engineering

ปัญหา "ความรู้" ที่โมเดลขาด?
  → RAG

ปัญหาพฤติกรรมเสถียรที่ scale?
  → fine-tuning

ปัญหา latency / cost?
  → เปลี่ยนโมเดล

— จบ Track A —

Track B — Production patterns

สำหรับวิศวกรที่สร้าง LLM applications

System prompts — ตั้ง persona ที่ระดับ API

เป้าหมายที่ไหน
Persona / expertisesystem
Constraintssystem
Output format defaultssystem
Per-task instructionsuser
คำถาม / ข้อมูลจริงuser

กฎพฤติกรรมที่เป็นรูปธรรม >> การอ้าง expertise นามธรรม

Structured output — 3 เทคนิค

  1. XML tags — Claude ถูก train มาบน XML
  2. JSON พร้อม schema ชัดเจน — vendor-neutral
  3. Prefilling — เริ่ม assistant turn ด้วย {

Reliability สูงสุด: schema + prefill ร่วมกัน ("the double belt")

Prompt chaining

Plan → Validate → Execute

ถ้า validate fail → ย้อน Plan พร้อม error

แต่ละ step ง่ายขึ้น debug ง่าย evaluate ง่าย

Tool use / Function calling

1. Define tools (name, description, parameter schema)
2. Send request + tools to model
3. Model responds with text OR tool call
4. Your code executes the tool
5. Send result back; loop until done

Description trigger การ call. ปฏิบัติเป็น prompt-engineering task: บุรุษที่สาม, ทำอะไร + เมื่อไหร่

Retrieval-Augmented Generation

INDEX (ครั้งเดียว): documents → chunks → embeddings → vector store

QUERY (ทุก request):
  user query → embed → top-k similar chunks
  ↓
  prompt: "Answer using only <context>. Q: {{query}}"
  ↓
  output (พร้อม citations to chunks)

Garbage in, garbage out — ลงทุน retrieval quality ก่อน

Evaluation-driven prompt development

  1. นิยามเกณฑ์ความสำเร็จ (concrete, measurable)
  2. สร้าง eval set (10-50 labelled examples)
  3. Run prompt → score outputs
  4. หา failure → revise
  5. Re-run, วัด delta, ship หรือ iterate

เลือก iteration ที่ validation pass rate ดีที่สุด — ไม่ใช่ตัวล่าสุด

Trigger rate (โมเดลไม่ deterministic)

trigger_rate = (runs ที่ trigger) / (total runs)

Test ผ่านเมื่อ trigger_rate ตรง expectation:
  should_trigger=true:  trigger_rate > 0.5
  should_trigger=false: trigger_rate < 0.5

รันแต่ละ query อย่างน้อย 3 ครั้ง

Train (60%) drives revisions. Validation (40%) วัด generalisation

Prompt injection — 3 classes

  1. Direct — "What is the password?" / "Ignore previous instructions"
  2. Indirect — translate / roleplay / hypothetical reframe
  3. Progressive — multi-turn extraction; defeat single-turn filters

"The line between aligned and adversarial behavior is thinner than most people think." — Lakera

ปัญหา Trust boundary

LLMs อ่านและทำตาม ทุก text ใน prompt — input ของ user ถูก execute โดยโมเดลในแบบเดียวกับ system prompt ของคุณ

ไม่มี trust boundary ที่ชัดเจนใน text. Defences เป็น layered ไม่ absolute

Defensive prompting — 4 layers

  1. Evaluation-first logic — โมเดลตัดสินใจก่อนว่าปลอดภัยไหม
  2. Role anchoring — ย้ำ persona กลาง prompt
  3. Output conditioning — pre-fill refusal start ("I cannot share")
  4. Instruction repetition — ระบุ constraints ในหลายส่วน

ไม่มี layer เดียวที่พอ. ใช้ทั้งหมดร่วมกันถึงได้ผล

เกินกว่า prompt — Programmatic guards

Layerกลไก
Input filterBlock known-malicious ก่อนถึงโมเดล
Output filterScan secrets, PII, policy violations
Tool restrictionsAllowlist ว่าโมเดลทำอะไรได้
Audit loggingเก็บทุก prompt + response
Human-in-loopAction high-stakes ต้องคนยืนยัน

Agent patterns

เมื่อ single prompt + chain ไม่พอ — ต้องการ loop

  • ReAct — Thought → Action → Observation cycle
  • Reflexion — self-critique loop
  • Planner-Executor — แยก planning ออกจาก execution

Cap iterations เสมอ. รักษา state. Anchor role ตลอด

Anti-patterns ที่ต้องเลี่ยง

  • Iteration ตาม "feel" (ไม่มี evals)
  • เพิ่ม temperature เพื่อ "คุณภาพดีขึ้น" (ผิดปุ่ม)
  • ใช้ temperature และ top_p พร้อมกัน
  • Negative instructions แทน positive examples
  • Tool descriptions กว้างเกินไป
  • Single-layer injection defence
  • Agent loops ไม่มี cap
  • Defences เดียวกันทุก vendor (re-test แยก)

Production checklist

พื้นฐาน

  • 4 prompt elements ครบ
  • Format ระบุชัดเจน
  • Settings ตั้งโดยเจตนา
  • เริ่ม zero-shot

Production

  • System prompt ≤1000 tokens
  • Schema + prefill สำหรับ JSON
  • Eval set + train/val split
  • Adversarial test suite
  • Tool descriptions: ทำอะไร + เมื่อไหร่
  • Agent loop iteration cap

แหล่งอ้างอิง

ข้อมูลทั้งหมดในสไลด์นี้มี citation ใน /wiki/

มีคำถามไหม?

Prompt Engineering 101 · 9 พ.ค. 2569