Generative AI, LLMs & RAG
RAG Architecture
Design Retrieval-Augmented Generation ตั้งแต่ ingestion, chunking, embeddings, hybrid search และ reranking ไปจนถึง citations และ evaluation เลือกระหว่าง RAG, fine-tuning และ prompting และ map เข้ากับ Azure AI Search และ Azure OpenAI
LLM ไม่รู้ internal policies ขององค์กร และ knowledge ของมันหยุดอยู่ที่ training cutoff Retrieval-Augmented Generation (RAG) แก้ปัญหานี้โดย retrieve documents ที่เกี่ยวข้องก่อน แล้วส่งให้ model ตอบจาก documents นั้น เป็น pattern ที่พบบ่อยที่สุดของ GenAI ในองค์กร และเป็นแกนของ project ในบทที่ 12 (Capstone: AI Assistant for a Bank Call Center)
Two phases of RAG
- Ingestion (offline หรือตาม schedule) ดึง documents, clean, chunking, สร้าง embeddings และเก็บลง index
- Query (ทุก question) search ใน index, เลือก chunks ที่ดีที่สุด แล้วส่งให้ LLM ตอบพร้อม citations
Ingestion: garbage in, garbage out
Document parsing PDF ที่มีตาราง, scanned documents และ slides ต้องใช้ document parser หรือ OCR ที่รักษา structure ไว้ ถ้าตารางค่าธรรมเนียมกลายเป็นข้อความปนกัน ไม่มี model ใดตอบถูก
Chunking แบ่ง documents เป็นชิ้นที่ search ได้
- Chunk size ที่ใช้บ่อยอยู่ราว 300–1,000 tokens chunk เล็ก search ได้แม่นแต่อาจขาด context chunk ใหญ่มี context ครบแต่เปลือง tokens และ search ได้ไม่คม
- ใส่ overlap ราว 10–20% ระหว่าง chunks เพื่อไม่ให้ประโยคสำคัญถูกตัดกลาง
- แบ่งตาม document structure (headings, sections, clauses) ดีกว่าตัดตามจำนวนตัวอักษร
- เก็บ metadata ทุก chunk เช่น document title, page, effective date, owner และ access level ใช้ทั้ง filter ผล search และแสดง citations
Embedding แปลงข้อความเป็น vector ข้อความที่ความหมายใกล้กันจะได้ vectors ใกล้กัน ต้องใช้ embedding model ตัวเดียวกัน ทั้งตอน indexing และตอน query ถ้าเปลี่ยน model ต้อง re-index ทั้งหมด
Query: retrieve, then rerank
Hybrid search รวมการ search สองแบบ
| แบบ | เก่งเรื่อง | พลาดเรื่อง |
|---|---|---|
| Keyword (เช่น BM25) | exact match เช่น product codes, เลขมาตรา, ชื่อเฉพาะ | synonyms, ถามด้วยคำต่างจาก document |
| Vector | semantic similarity, ถามคนละคำแต่เรื่องเดียวกัน | codes และตัวเลขที่ต้องตรงทุกตัว |
ผลของสองแบบรวมกันด้วยวิธีเช่น Reciprocal Rank Fusion (RRF) ที่ให้ score ตาม rank ในแต่ละ list document ที่ rank ดีทั้งสองแบบจะขึ้นมาบนสุด
Rerank ใช้อีก model หนึ่ง (cross-encoder) อ่าน question คู่กับ chunk แล้วให้ relevance score ใหม่ แม่นกว่า search รอบแรกแต่ช้ากว่า จึงใช้กับผล search ชุดเล็ก เช่น retrieve 20–50 chunks, rerank แล้วเลือก top-k 3–5 chunks ส่งให้ LLM
Security trimming user ต้องเห็นเฉพาะ documents ที่ตนมีสิทธิ์ filter ด้วย metadata ในขั้น search ไม่ใช่หวังให้ model ไม่พูดถึง
Grounding & citations
ใน prompt สั่งให้ตอบจาก chunks ที่ให้เท่านั้น และอ้าง ID ของแต่ละ chunk app แปลง ID เป็น link ไปยัง source document user จึง verify เองได้ และถ้า search ไม่เจอ chunk ที่ score เกิน threshold ระบบควร ตอบว่าไม่พบข้อมูล แทนที่จะส่งให้ model เดา
Evaluate retrieval and generation separately
เมื่อคำตอบผิด ต้องรู้ว่าผิดเพราะ retrieve ไม่เจอ หรือ generate ผิดทั้งที่ retrieve เจอ จึงวัดสอง layers
| Layer | Metrics | คำถามที่ตอบ |
|---|---|---|
| Retrieval | recall@k, hit rate | document ที่ถูกต้องอยู่ใน top-k หรือไม่ |
| Generation | groundedness, relevance, correctness | คำตอบอิง documents จริงไหม ตรงคำถามไหม ถูกต้องไหม |
Eval set ควรมีคำถามจริงจาก users 100–300 ข้อ พร้อม expected documents และ reference answers ใช้ run ทุกครั้งที่เปลี่ยน chunking, model หรือ prompt (บทที่ 9 (MLOps & LLMOps))
RAG vs fine-tuning vs prompting
| Option | เหมาะเมื่อ | ไม่เหมาะเมื่อ |
|---|---|---|
| Prompting | งานทั่วไป knowledge อยู่ใน model แล้ว | ต้องใช้ internal data หรือ data ที่เปลี่ยนบ่อย |
| RAG | knowledge อยู่ใน documents เปลี่ยนบ่อย ต้องมี citations และ access control | งานที่ต้องเปลี่ยน “วิธีตอบ” มากกว่า “สิ่งที่รู้” |
| Fine-tuning | ต้องการ style, tone หรือ output format เฉพาะที่ prompt ทำไม่ได้ | ใช้เพื่อ “สอน facts” ที่เปลี่ยนบ่อย update ยากและให้ citations ไม่ได้ |
ลำดับที่แนะนำคือ prompting ก่อน → เพิ่ม RAG → fine-tune เมื่อมี evidence ว่าจำเป็น และหลายระบบใช้ RAG กับ fine-tuned model ร่วมกันได้
Azure mapping (ณ ปี 2026)
| RAG component | Azure service |
|---|---|
| Source documents | Blob Storage หรือ Data Lake Storage |
| Document parsing, tables, OCR | Azure AI Document Intelligence (เดิมชื่อ Form Recognizer) |
| Index, hybrid search, rerank | Azure AI Search (เดิมชื่อ Azure Cognitive Search) มี indexer ดึง documents ตาม schedule และ semantic ranker สำหรับ rerank |
| Embedding และ LLM | Azure OpenAI (จัดการผ่าน Microsoft Foundry) |
Option อื่นที่พบ เช่น เก็บ vectors ใน database ที่ใช้อยู่แล้ว (PostgreSQL + pgvector หรือ Cosmos DB) เหมาะเมื่อ data ไม่มาก และอยากลดจำนวน services ที่ต้องดูแล