RAG Architecture
Hybrid • Evidence-grounded • Permission-aware • Citation-first
RAG placement in the engineering system
Bridge, not source of truthจำแนก retrieval intent, risk, domain และ budget
RLS, consent, tenant scope, effective date, authority
Exact + full-text + vector + case/relationship lookup
Relevance, authority, freshness, context fit, diversity
Chunks, clauses, evidence IDs, citations, limitations
Interpret knowledge → synthesize recommendation
Faithfulness, citation entailment, safety, uncertainty
Offline ingestion & indexing pipeline
Curated1. Register authoritative source
Document ID, type, owner, authority, jurisdiction, version, effective dates, classification
2. Parse by semantic structure
กฎหมายแบ่งตามข้อ/วรรค มาตรฐานตามตัวชี้วัด งานวิจัยตาม section และกรณีศึกษาตาม episode
3. Quality and governance checks
Hash, duplicate, OCR/text quality, citation anchors, consent, PII, reviewer approval
4. Build searchable representations
PostgreSQL full-text index, pgvector embeddings, metadata filters, relationships และ citation map
5. Publish index version
เฉพาะสถานะ approved/published จึงถูกค้นได้ พร้อม rollback และ re-index history
Four knowledge zones
Normative zone
กฎหมาย ระเบียบ มาตรฐาน Rubric — exact clause, version และ applicability เป็นหลัก
Theory & research zone
หลักการ ทฤษฎี งานวิจัย และ evidence strength สำหรับ A07
Approved case zone
กรณีนิเทศที่ de-identified, verified และมี outcome ใช้เทียบความคล้ายคลึง
User/context zone
บริบทนักศึกษาและหลักฐานที่มีสิทธิ์ ใช้เฉพาะ workflow scope ไม่รวมเข้าคลังสาธารณะ
Online adaptive retrieval flow
Query-timeIntent, entities, role, student/session scope, requested outcome
Permission, consent, active version, jurisdiction, authority level
Direct lookup, exact normative, hybrid semantic, similar case, graph-aware
Keyword/BM25-like FTS + vector similarity + rule/entity lookup
Deduplicate, diversify, authority/freshness/context scoring
Token budget, clause boundaries, citation IDs, contradictions, limitations
Coverage, conflict, injection content, PII, stale source and missing evidence
Retrieval mode decision matrix
| Query type | Primary mode | Why | Required controls |
|---|---|---|---|
| คะแนน/ข้อมูลที่มีโครงสร้าง | Direct DB / Tool lookup | ไม่ควรใช้ vector search กับค่าที่ต้องแม่นยำ | RLS + exact schema |
| กฎหมาย ระเบียบ มาตรฐาน | Exact + metadata + rule lookup | ต้องได้ข้อ เวอร์ชัน วันที่ และเขตอำนาจ | Authority + temporal validity + citation |
| ทฤษฎี/งานวิจัย/แนวปฏิบัติ | Hybrid FTS + vector | ต้องการทั้งคำตรงและความหมายใกล้เคียง | Verified source + evidence level + rerank |
| กรณีคล้ายกัน | Context-filtered case retrieval | เปรียบเทียบบริบทและ outcome | De-identification + context differences |
| ความสัมพันธ์หลายชั้น | Graph-aware / GraphRAG ภายหลัง | ใช้เมื่อ path และ ontology ซับซ้อนจริง | Verified edges + traversal policy |
Simple-first physical implementation
PostgreSQL metadata + full-text
Exact filters, versions, authority, permissions และ keyword retrieval
pgvector
Semantic retrieval ในฐานเดียวกับ metadata และ RLS
Retrieval service module
หนึ่ง logical service รองรับ modes, fusion, reranking และ context assembly
GraphRAG is optional
เพิ่มเมื่อ ontology/path queries เป็น contribution หลัก ไม่เพิ่มตั้งแต่ MVP
knowledge_documents
type / authorityversion / effective datesowner / approvalclassificationdocument_units
chapter / section / clausecitation_anchorparent_unitoriginal_textknowledge_chunks
chunk_textembeddingmetadataindex_versionknowledge_relationships
source_entityrelation_typetarget_entityverificationretrieval_logs
query / modefilters / top_krank / scoresused_in_answerrag_eval_results
dataset_itemretrieval metricsgrounding metricssafety / latency / costการเลือกฉบับของมาตรฐานในชั้นการค้นคืน
กรองก่อนค้น ไม่ใช่กรองหลังค้นใช้วันเริ่มรอบฝึกประสบการณ์ของนักศึกษาเป็นวันอ้างอิง
เลือกเฉพาะฉบับที่มีผลบังคับไม่หลังวันอ้างอิง และยังไม่ถูกแทนที่ ณ วันนั้น
ขอบเขตและแหล่งอำนาจต้องตรงกับกรณีที่กำลังตรวจ
ถ้าพบสองฉบับที่ให้ข้อความต่างกัน ให้แจ้งว่าขัดกัน ไม่เลือกเอง
รหัสมาตรฐาน ฉบับ และวันที่มีผลบังคับ ติดไปกับทุกข้ออ้าง
กรณีขัดกันหรือฉบับหมดอายุ ส่งหัวหน้าฝ่ายฝึกประสบการณ์วิชาชีพครูตามเงื่อนไข ES-03
RAG security flow
Authorization before retrieval
RLS/ACL และ tenant/student scope ต้องกรองก่อนคำนวณ similarity ไม่ใช่กรองหลังได้ผลแล้ว
Untrusted-content isolation
ข้อความในเอกสารเป็นข้อมูลอ้างอิง ไม่ใช่คำสั่ง Agent; ตรวจ indirect prompt injection
Minimum necessary context
ส่งเฉพาะ chunks ที่จำเป็น ลด PII, token cost และ data exfiltration surface
Citation and provenance
ทุก claim สำคัญเชื่อม document/unit/evidence ID และ index version
Human gate for high-impact use
กฎหมาย คะแนน สถานะ และ intervention สำคัญต้องผ่านผู้มีอำนาจ
RAG evaluation architecture
| Layer | Metrics | Evidence |
|---|---|---|
| Retrieval | Recall@K, Precision@K, MRR, nDCG, filter accuracy | Golden query–relevant unit pairs |
| Context assembly | Coverage, redundancy, contradiction capture, token efficiency | Context bundle inspection |
| Generation grounding | Faithfulness, citation correctness/entailment, unsupported claims | A12 + code + expert eval |
| Security | Cross-user isolation, injection resistance, PII leakage, stale-rule rejection | Adversarial dataset |
| Operations | Latency, cost, cache hit, index freshness, failure/retry rate | OTel + Langfuse |
| Educational outcome | Usefulness, adoption, implementation and competency growth | Research data mart |
Research-ready RAG variables
เปิด Research Analyticsท่อการค้นคืน
หน้านี้แสดงโครงของท่อ ไม่ได้แสดงค่าพารามิเตอร์
ค่าจำนวนผลลัพธ์และน้ำหนักของสองแขนเป็นค่าเชิงวิศวกรรมที่แพ็กเกจการค้นคืนเป็นเจ้าของ การพิมพ์ค่าลงหน้านี้จะสร้างแหล่งความจริงที่สอง
ท่อมี 7 ขั้น
ขั้นที่แตะแบบจำลอง 1 ขั้น
- S1 · ทำข้อความให้เป็นรูปแบบเดียวกัน · ปรับข้อความค้นหาภาษาไทยให้อยู่ในรูปที่เทียบกันได้ · กำหนดผลแน่นอน
- S2 · สร้างเวกเตอร์ · เรียกตัวสร้างเวกเตอร์ที่ตรึงด้วย snapshot ตามกฎห้ามใช้ชื่อรุ่นย่อ · แตะแบบจำลอง
- S3 · แขนความหมาย · ค้นด้วยระยะโคไซน์บนดัชนีเวกเตอร์ จับความหมายที่ใช้คำต่างกัน · กำหนดผลแน่นอน
- S4 · แขนคำศัพท์ · ค้นด้วยความคล้ายไทรแกรมและโทเคนที่ตัดคำแล้ว จับรหัสข้อและตัวเลขปี · กำหนดผลแน่นอน
- S5 · หลอมผลสองแขน · รวมอันดับจากสองแขนด้วยการหลอมอันดับส่วนกลับ · กำหนดผลแน่นอน
- S6 · จัดลำดับความสำคัญ · เรียงตามลำดับความน่าเชื่อถือ ความเฉพาะของขอบเขต วันที่มีผล แล้วจึงคะแนน · กำหนดผลแน่นอน
- S7 · กวาดหาความขัดแย้ง · ตรวจด้วยกฎเชิงข้อความ พบแล้วต้องประกาศและส่งต่อมนุษย์ ห้ามรวมผลเงียบ · กำหนดผลแน่นอน
สองแขนที่ต้องมีทั้งคู่
แต่ละแขนมีจุดอ่อนที่อีกแขนชดเชย การตัดแขนใดออกทำให้คำถามทั้งกลุ่มค้นไม่เจอ
- แขนความหมาย · จุดแข็ง จับความหมายที่ใช้คำต่างกัน · จุดอ่อน พลาดรหัสข้อกำหนดที่เป็นสตริงตายตัว
- แขนคำศัพท์ · จุดแข็ง จับรหัสข้อ ชื่อเฉพาะ ตัวเลขปี และคำที่ต้องตรงตัว · จุดอ่อน พลาดคำถามที่ใช้ภาษาพูดของนักศึกษา
กฎที่ห้ามละเมิดในท่อนี้
- ขั้นที่เหลือทั้งหมดกำหนดผลแน่นอน ไม่มีการเรียกแบบจำลองภาษาในขั้นใด
- ตัวสร้างเวกเตอร์ต้องตรึงด้วยรหัสรุ่นที่ลงทะเบียนแล้ว ห้ามใช้ชื่อรุ่นย่อ
- พบความขัดแย้งระหว่างฉบับของมาตรฐานแล้วต้องประกาศและส่งต่อมนุษย์ ห้ามรวมผลเงียบ
- ท่อนข้อความที่ติดธงว่าน่าสงสัย ห้ามถูกใช้เป็นแหล่งของการอ้างอิง