0Pricing
LangChain / RAG / Vector DBs · บทเรียน

การจัดการข้อมูลกำกับเอกสารและการกรอง

เรียนรู้การเพิ่มข้อมูลกำกับเอกสาร เติมรายละเอียด และกรองข้อมูล เพื่อให้ pipeline ของ RAG จำกัดการค้นคืนไว้ยังแหล่งข้อมูลที่ถูกต้อง

การจัดการข้อมูลกำกับเอกสารและการกรอง เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LangChain / RAG / Vector DBs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Metadata Matters

Every document chunk in LangChain carries a page_content string and a metadata dictionary. While the content feeds the embedding model, metadata drives filtering, attribution, and traceability.

  • Source file or URL
  • Page number or section
  • Author, date, language

The Document Object

A LangChain Document is a lightweight container. You can construct one directly and pass any JSON-serializable values in metadata.

from langchain_core.documents import Document

doc = Document(
    page_content="Annual revenue grew 12%.",
    metadata={"source": "report.pdf", "page": 4, "year": 2025}
)
print(doc.metadata["source"])

Automatic Metadata from Loaders

Most loaders inject metadata for free. A PyPDFLoader adds source and page, while a WebBaseLoader adds the URL and page title.

from langchain_community.document_loaders import PyPDFLoader

loader = PyPDFLoader("handbook.pdf")
pages = loader.load()
print(pages[0].metadata)
# {"source": "handbook.pdf", "page": 0}

Enriching Metadata After Load

You often need to add fields the loader does not know about, like a category or tenant id. Iterate and mutate the dictionary.

for d in pages:
    d.metadata["department"] = "finance"
    d.metadata["sensitive"] = False
print(pages[0].metadata["department"])

Metadata Survives Splitting

When you split documents, the splitter copies the parent metadata onto each child chunk. This means filters set before splitting still apply afterward.

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(chunk_size=200)
chunks = splitter.split_documents(pages)
print(chunks[0].metadata["source"])

Filtering at Retrieval Time

Vector stores accept a filter argument so you only search the relevant slice. This is faster and reduces irrelevant matches.

results = vectorstore.similarity_search(
    "vacation policy",
    k=3,
    filter={"department": "hr"}
)

Self-Query Retrieval

A SelfQueryRetriever lets the LLM translate a natural-language query into both a semantic search and a metadata filter automatically.

You describe the metadata schema once, and the model decides when to filter.

Cleaning Noisy Metadata

Loaders sometimes produce verbose or nested metadata that vector stores reject. Flatten or whitelist the keys you need.

def clean(d):
    keep = {"source", "page", "department"}
    d.metadata = {k: v for k, v in d.metadata.items() if k in keep}
    return d

cleaned = [clean(d) for d in chunks]

Metadata for Citations

Storing the source and page lets you cite where an answer came from. After retrieval, format the metadata into a human-readable reference.

for r in results:
    src = r.metadata["source"]
    pg = r.metadata.get("page", "?")
    print(f"[{src} p.{pg}]")

Type Constraints

Many vector databases only allow scalar metadata values: strings, numbers, and booleans. Lists or dicts must be serialized to JSON strings or removed.

  • Good: {"page": 4}
  • Reject: {"tags": ["a","b"]}

A Practical Filter Pipeline

Combine enrichment, cleaning, and filtered search into one flow so every query is scoped to the correct subset of your corpus.

docs = PyPDFLoader("policy.pdf").load()
for d in docs:
    d.metadata["region"] = "EU"
chunks = splitter.split_documents(docs)
# index chunks, then:
vectorstore.similarity_search("data retention", filter={"region": "EU"})

Quick Check

Test your understanding of metadata handling.

Recap

You learned to work with document metadata:

  • Loaders auto-add fields like source and page
  • Enrich and clean metadata for filtering and citations
  • Metadata propagates through splitting
  • Use filter or a self-query retriever to scope searches

คำถามที่พบบ่อย

บทเรียน “การจัดการข้อมูลกำกับเอกสารและการกรอง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการข้อมูลกำกับเอกสารและการกรอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อมูลกำกับเอกสารและการกรอง”

เรียนรู้การเพิ่มข้อมูลกำกับเอกสาร เติมรายละเอียด และกรองข้อมูล เพื่อให้ pipeline ของ RAG จำกัดการค้นคืนไว้ยังแหล่งข้อมูลที่ถูกต้อง คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LangChain / RAG / Vector DBs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน LangChain / RAG / Vector DBs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการข้อมูลกำกับเอกสารและการกรอง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน LangChain / RAG / Vector DBs นี้ได้ไหม

ได้ บทเรียน LangChain / RAG / Vector DBs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การโหลดเอกสารหลากหลายประเภท
  2. ทำความเข้าใจกลยุทธ์การแบ่งข้อความ
  3. การปรับแต่งการแบ่งเอกสาร
  4. การจัดการข้อมูลกำกับเอกสารและการกรอง
← กลับไปที่ LangChain / RAG / Vector DBs