AI Solution Architect: from System Analyst to AI Architect
บทที่ 4/12

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

⏱ ประมาณ 35 นาที

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

RAG architecture สองช่วง: ingestion (offline) นำ documents มา parse และ chunk ขนาด 300–1000 tokens แล้ว embed ด้วย Azure OpenAI และเก็บใน hybrid index บน Azure AI Search ส่วน query (ทุกคำถาม) นำ question ไปทำ hybrid search ใน index เดียวกัน rerank เลือก top-k แล้วส่งให้ LLM บน Azure OpenAI ตอบพร้อม citations
Quality ของคำตอบขึ้นกับ ingestion มากกว่าที่คนส่วนใหญ่คิด
  1. Ingestion (offline หรือตาม schedule) ดึง documents, clean, chunking, สร้าง embeddings และเก็บลง index
  2. 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
Vectorsemantic 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

LayerMetricsคำถามที่ตอบ
Retrievalrecall@k, hit ratedocument ที่ถูกต้องอยู่ใน top-k หรือไม่
Generationgroundedness, 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 ที่เปลี่ยนบ่อย
RAGknowledge อยู่ใน 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 componentAzure service
Source documentsBlob Storage หรือ Data Lake Storage
Document parsing, tables, OCRAzure AI Document Intelligence (เดิมชื่อ Form Recognizer)
Index, hybrid search, rerankAzure AI Search (เดิมชื่อ Azure Cognitive Search) มี indexer ดึง documents ตาม schedule และ semantic ranker สำหรับ rerank
Embedding และ LLMAzure OpenAI (จัดการผ่าน Microsoft Foundry)

Option อื่นที่พบ เช่น เก็บ vectors ใน database ที่ใช้อยู่แล้ว (PostgreSQL + pgvector หรือ Cosmos DB) เหมาะเมื่อ data ไม่มาก และอยากลดจำนวน services ที่ต้องดูแล

แบบทดสอบ

Users มักถามด้วย product code เช่น “PL-2049” แต่บางครั้งถามด้วยภาษาพูด ควร search แบบใด
วิธีใดป้องกันไม่ให้ user เห็นเนื้อหาจาก documents ที่ไม่มีสิทธิ์ได้ดีที่สุด
Eval set แสดงว่า recall@5 อยู่ที่ 55% แต่ groundedness สูง ปัญหาหลักอยู่ที่ layer ใด
อัตราดอกเบี้ยเปลี่ยนทุกเดือนและลูกค้าต้องเห็น citations ควรใช้ approach ใด
ถ้าเปลี่ยน embedding model ต้องทำอะไรกับ index