การจัดการโครงสร้างเอกสารที่ซับซ้อน
สร้างกลยุทธ์สำหรับแบ่งและค้นคืนข้อมูลจากเอกสารที่มีโครงสร้างซับซ้อน เช่น ตารางหรือส่วนเนื้อหาที่ซ้อนกัน ได้อย่างมีประสิทธิภาพ
การจัดการโครงสร้างเอกสารที่ซับซ้อน เป็นบทเรียน LLM Apps in Production (RAG + Vector DB + Caching) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LLM Apps in Production (RAG + Vector DB + Caching) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LLM Apps in Production (RAG + Vector DB + Caching) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Simple Text: Complex Documents
When building RAG systems, we often deal with documents that aren't just plain, flowing text. Think about financial reports, scientific papers, or legal contracts.
These documents frequently contain tables, nested sections (like chapters and sub-chapters), and other intricate structures. Standard text chunking methods often struggle with these, breaking context and making retrieval less effective.
Tables: A Challenge for RAG
Tables are a prime example of complex structures. They present data in a structured, grid-like format where relationships between rows and columns are crucial.
- Lost Context: A simple character-based chunker might split a table row, separating a value from its header, making the chunk meaningless.
- Poor Embeddings: Without proper context, the generated embeddings for table fragments might not accurately represent the data.
Table-Aware Chunking Strategies
To effectively handle tables, we need specific strategies:
- Extraction: Identify and extract tables as distinct entities.
- Serialization: Convert tables into a more LLM-friendly text format, like Markdown or structured JSON, preserving their relationships.
- Summarization: For very large tables, generate a concise summary to be embedded, linking back to the full table.
This ensures the LLM receives the full, meaningful context of the table.
Extracting Table Data (Python)
Here's a simple Python example showing how you might process a table represented as a string, converting it into a more structured list of rows.
import csv
import io
def process_table_string(table_str):
# Use StringIO to treat the string as a file
f = io.StringIO(table_str)
reader = csv.reader(f, delimiter='|')
rows = []
for i, row in enumerate(reader):
# Strip whitespace and filter empty strings
cleaned_row = [item.strip() for item in row if item.strip()]
if cleaned_row and i > 0: # Skip header line
rows.append(cleaned_row)
return rows
if __name__ == "__main__":
data = """
Name | Age | City
-------|-----|--------
Alice | 30 | New York
Bob | 24 | London
Charlie| 35 | Paris
"""
processed_data = process_table_string(data)
for row in processed_data:
print(row)Understanding Nested Documents
Documents often have a natural hierarchy. Think of a book with chapters, sections, and subsections. Each part builds on the previous one, and its meaning is often tied to its parent context.
Standard chunking might split a subsection from its main section's heading, making the retrieved chunk less informative or even confusing without the proper context.
Preserving Document Hierarchy
To handle nested structures effectively, we use hierarchical chunking:
- Semantic Boundaries: Instead of fixed character counts, chunk based on logical divisions like headings (H1, H2, H3).
- Parent Context: Include the title of the parent section in the child chunk. For example, a chunk from 'Section 2.1' might start with 'Chapter 2: Introduction - Section 2.1: Subtopic'.
- Metadata: Store the full path or hierarchy level in the chunk's metadata.
Chunking by Sections (Python)
This Python example demonstrates a simple way to split a document into chunks based on markdown-style headings. Each chunk will contain a section's content.
def chunk_by_headings(document_text):
lines = document_text.split('\n')
chunks = []
current_chunk = []
current_heading = ""
for line in lines:
if line.startswith('# '): # Main heading
if current_chunk:
chunks.append({'heading': current_heading, 'content': '\n'.join(current_chunk).strip()})
current_heading = line.strip()
current_chunk = [line]
elif line.startswith('## '):
# Sub-heading, can be part of the current chunk, or signal a new sub-chunk
# For simplicity, we'll just add it to the current chunk content here
# More advanced logic might create nested chunks or separate entries
current_chunk.append(line)
else:
current_chunk.append(line)
if current_chunk:
chunks.append({'heading': current_heading, 'content': '\n'.join(current_chunk).strip()})
return chunks
if __name__ == "__main__":
doc = """
# Chapter 1: Introduction
This is the introduction text.
## Section 1.1: Background
More details about the background.
# Chapter 2: Methods
Here we describe the methods used.
## Section 2.1: Data Collection
How data was collected.
"""
document_chunks = chunk_by_headings(doc)
for i, chunk in enumerate(document_chunks):
print(f"--- Chunk {i+1} ---")
print(f"Heading: {chunk['heading']}")
print(f"Content snippet: {chunk['content'][:50]}...")
print()Metadata for Richer Context
Metadata is extra information attached to a chunk that describes it without being part of the chunk's main text. It's incredibly powerful for complex documents.
- Document Title: Which source document does this chunk come from?
- Page Number: Where in the original document was this found?
- Parent Section/Chapter: What larger context does this chunk belong to?
- Table ID: If it's a table, which table is it?
Metadata allows for targeted filtering during retrieval and provides valuable context to the LLM.
Beyond Single-Vector Retrieval
For highly complex content, simple text chunks might not be enough. Multi-vector retrieval is an advanced technique where you create different types of embeddings for the same content.
For example, you could have a small, concise summary of a table embedded for quick retrieval, and the full, detailed table content stored separately. The RAG system retrieves the summary, and if relevant, then fetches the full table to pass to the LLM.
Complex Document Check
You've learned about various strategies for handling complex document structures in RAG. Let's test your understanding.
Recap: Mastering Complex Docs
Congratulations! You've explored critical strategies for handling complex document structures in RAG.
- We saw how tables can lose context with standard chunking and learned to extract and serialize them.
- We discussed nested documents and the importance of hierarchical chunking to preserve relationships.
- Finally, we highlighted the power of metadata to enrich chunks and enable more precise retrieval.
By applying these techniques, your RAG system can deliver more accurate and contextually relevant responses, even from the most intricate documents!
คำถามที่พบบ่อย
บทเรียน “การจัดการโครงสร้างเอกสารที่ซับซ้อน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการโครงสร้างเอกสารที่ซับซ้อน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LLM Apps in Production (RAG + Vector DB + Caching) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LLM Apps in Production (RAG + Vector DB + Caching) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการโครงสร้างเอกสารที่ซับซ้อน”
สร้างกลยุทธ์สำหรับแบ่งและค้นคืนข้อมูลจากเอกสารที่มีโครงสร้างซับซ้อน เช่น ตารางหรือส่วนเนื้อหาที่ซ้อนกัน ได้อย่างมีประสิทธิภาพ คุณปฏิบัติ LLM Apps in Production (RAG + Vector DB + Caching) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LLM Apps in Production (RAG + Vector DB + Caching) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน LLM Apps in Production (RAG + Vector DB + Caching) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการโครงสร้างเอกสารที่ซับซ้อน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน LLM Apps in Production (RAG + Vector DB + Caching) นี้ได้ไหม
ได้ บทเรียน LLM Apps in Production (RAG + Vector DB + Caching) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเขียนคำค้นใหม่และการจัดอันดับซ้ำ
- รูปแบบ RAG หลายขั้นตอนและแบบใช้เอเจนต์
- การจัดการโครงสร้างเอกสารที่ซับซ้อน
- การค้นหาด้วยตนเองและการอ้างอิงแหล่งที่มา