Generative AI, LLMs & RAG
AI Agents & Orchestration
Tool calling และ agent loop, orchestration patterns ตั้งแต่ single agent, planner–executor ถึง multi-agent, Model Context Protocol (MCP), guardrails และ human-in-the-loop และเมื่อไรที่ไม่ควรใช้ agent
RAG ทำให้ LLM รู้ data ขององค์กร ส่วน AI agent ทำให้ LLM ลงมือทำ ได้ เช่น ดูสถานะบัตร, create ticket หรือ draft email โดยให้ model ตัดสินใจเองว่าจะเรียก tool ใดและเมื่อไร capability ที่เพิ่มขึ้นมาพร้อม risk ที่เพิ่มขึ้น หน้าที่ของ architect คือกำหนด boundary ให้ชัด
Tool calling: the model asks, your system acts
Model ไม่ได้เรียก API เอง app ส่ง tool definitions พร้อม schema ไปกับ prompt, model ตอบกลับมาว่า “อยากเรียก tool นี้ด้วย parameters นี้” แล้ว orchestrator ของเราเป็นผู้ validate และเรียกจริง
{
"name": "get_card_status",
"description": "ดูสถานะบัตรของลูกค้าที่ authenticated แล้ว (read-only)",
"parameters": {
"type": "object",
"properties": {
"card_last4": { "type": "string", "pattern": "^[0-9]{4}$" }
},
"required": ["card_last4"]
}
}
สังเกตว่า tool นี้ ไม่รับ customer ID จาก model orchestrator ใส่ identity ของลูกค้าจาก authenticated session เอง ถ้าให้ model ส่ง ID มา user อาจหลอกให้ดู data ของคนอื่นได้
The agent loop
Pattern นี้มักเรียกว่า ReAct (Reason + Act) model สลับระหว่างการ reasoning ว่าจะทำอะไรต่อ กับการเรียก tool แล้วอ่าน observation ทุกรอบเพิ่ม tokens และ latency จึงต้องมี max iterations เสมอ
Orchestration patterns
| Pattern | ลักษณะ | เหมาะกับ |
|---|---|---|
| Deterministic workflow + LLM | โค้ดกำหนด steps, LLM ทำเฉพาะบาง step เช่น classification หรือ summarization | งานที่ steps รู้ล่วงหน้า audit ง่ายที่สุด |
| Single agent + tools | LLM ตัวเดียวเลือก tools เองใน loop | งานที่ steps ต่างกันตามคำถาม tools ไม่เกินราวสิบกว่าตัว |
| Planner–executor | ตัวหนึ่ง plan เป็น steps อีกตัว execute ทีละ step | งานยาวหลาย steps ต้องการ plan ที่ review ได้ก่อนลงมือ |
| Multi-agent | หลาย agents แต่ละตัวเชี่ยวชาญด้านหนึ่ง มี supervisor ประสาน | ระบบใหญ่ที่หลายทีมดูแลคนละ domain |
MCP: a standard way to connect tools
Model Context Protocol (MCP) เป็น open standard สำหรับเชื่อม AI apps กับ tools และ data sources ทีมเจ้าของระบบเขียน MCP server ครั้งเดียว แล้ว agents หรือ AI apps หลายตัวเรียกใช้ได้ ในมุม architecture MCP server คือ อีกหนึ่ง API ที่ต้องมี governance ต้องมี authentication, authorization, logging และผ่าน gateway เหมือน API อื่น (บทที่ 6 (APIs, Microservices & Legacy Integration))
Guardrails & human-in-the-loop
Guardrails คือ controls ที่ enforce ด้วยโค้ดและ infrastructure ไม่ใช่ด้วยการขอร้องใน prompt
- Least privilege agent ได้เฉพาะ tools ที่จำเป็น แยก read tools กับ write tools และ credentials ของ tool มี scope แคบที่สุด
- Input & output validation ทั้ง harmful content, PII และ parameter schema
- Resource limits max iterations, timeout และ token budget ต่อ request
- Audit log บันทึกทุก tool call พร้อม parameters และ results
Human-in-the-loop (HITL) ให้คน approve ก่อนทำ action ที่มีผลจริง แบ่งตาม risk level
| Risk level | ตัวอย่าง | Control |
|---|---|---|
| Read-only | ดูสถานะบัตร, search policies | agent ทำได้เอง |
| Reversible write | create ticket, draft email | agent ทำได้ แต่ log และ review ย้อนหลัง |
| Money or permissions | อายัดบัตร, refund ค่าธรรมเนียม | agent propose, คน approve |
When NOT to use an agent
- Steps ตายตัวและรู้ล่วงหน้า → ใช้ workflow ธรรมดา ถูกกว่าและ audit ง่ายกว่า
- ต้องการผลลัพธ์ deterministic 100% เช่น คำนวณดอกเบี้ย → ใช้โค้ด ให้ LLM แค่อธิบายผล
- Latency ต้องต่ำมาก → loop หลายรอบอาจใช้เวลาหลายวินาที
- ยังไม่มี eval set และ audit log → ยังไม่พร้อมให้ AI ทำงานแทนคน