Step 11 of 16
Parse sources, create deterministic chunks, enforce token limits, batch embeddings, and version indexes.
แยก ingestion เป็น pipeline ของตัวเอง แล้ว version parser, chunker, embedding model และ index ครับ
Level: Advanced
RAG ที่ตอบดีเริ่มจาก ingestion pipeline ครับ แยก pipeline นี้ออกจาก agent request เพื่อให้ parse, chunk, embed และ index ได้ใหม่โดยไม่กระทบผู้ใช้
สร้าง restart-safe ingestion worker ที่ใช้ persistent source state, deterministic chunk IDs และ versioned vector collection ครับ
record SourceDocument(
string SourceId,
string Version,
string Title,
Uri SourceUri,
string Content,
IReadOnlyDictionary<string, string> Metadata);
record DocumentChunk(
string ChunkId,
string SourceId,
string Version,
int Position,
string Text,
string ContentHash);
สร้าง ChunkId แบบ deterministic จาก source ID, version และ position ครับ เมื่อ ingest ซ้ำจะ upsert record เดิมได้ และลบ chunks ของ version เก่าได้ครบ
เริ่มจาก heading และ paragraph boundary ก่อน fixed character count เก็บ title และ parent headings ไว้กับแต่ละ chunk เพื่อรักษาบริบท หาก chunk ใหญ่เกิน embedding model limit ให้ split ซ้ำด้วย token-aware splitter
Pipeline ที่แนะนำ:
อย่าส่ง HTML ทั้งหน้า, navigation, cookie banner หรือ duplicated footer เข้า embedding ครับ Noise เหล่านี้เพิ่ม cost และทำให้ retrieval สับสน
เก็บ embedding model name, dimensions, chunker version และ indexed timestamp ข้าง collection การเปลี่ยน model หรือ dimensions ต้องสร้าง index version ใหม่ ไม่ควรผสม vectors คนละ space
ทดสอบ parser ด้วย fixture หลายรูปแบบ และทดสอบ chunker ว่าไม่ทำ code block, table row หรือ heading context หายครับ จากนั้นวัด distribution ของ token count แทนการดูตัวอย่างเพียงไฟล์เดียว
In-memory vector store ใช้เฉพาะ local development และ tests ครับ Production adapter ต้องรองรับ metadata filter, atomic upsert, delete by source/version, index alias หรือ equivalent cutover และ backup policy
Ingest fixture เดิมสองรอบแล้วตรวจว่า record count เท่าเดิม แก้ source หนึ่งไฟล์แล้วตรวจว่า chunks เก่าถูกแทนที่ครบ Kill worker ระหว่าง embedding batch แล้ว resume โดยไม่สร้าง orphan records ครับ
กำหนด maximum document size, supported MIME types, malware scan, parser timeout, token guard, batch size, provider rate limit และ dead-letter path บันทึก source checksum ก่อนเปิด index version ให้ search
ส่ง ingestion state machine, parser fixtures, chunker tests, persistent vector-store adapter, restart test และ index manifest