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

การค้นคืนเอกสารแม่และหน้าต่างประโยค

แยกส่วนที่ใช้ค้นหาออกจากส่วนที่ส่งคืน เพื่อให้ LLM ได้ผลลัพธ์ที่ตรงจุดพร้อมบริบทที่ครบถ้วน

บทเรียน 4 จาก 413 ขั้นตอน

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

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

The Chunk-Size Dilemma

Small chunks search precisely but lack context; large chunks give context but dilute relevance. Parent document retrieval resolves this tension by searching small and returning large.

Two Chunk Sizes

Index small child chunks for accurate similarity matching, but keep a link to the larger parent chunk that surrounds each one.

  • Search on child embeddings
  • Return parent text to the LLM

ParentDocumentRetriever

LangChain provides a ready-made retriever. You give it a child splitter, an optional parent splitter, a vector store, and a doc store for the parents.

from langchain.retrievers import ParentDocumentRetriever

retriever = ParentDocumentRetriever(
    vectorstore=vectorstore,
    docstore=store,
    child_splitter=child_splitter,
    parent_splitter=parent_splitter,
)

Adding Documents

The retriever splits each document into parents and children, embeds the children, and stores parents keyed by id so they can be fetched on a hit.

retriever.add_documents(docs)
results = retriever.invoke("What is the refund window?")
print(len(results[0].page_content))  # large parent text

Sentence-Window Retrieval

A variant indexes single sentences but, on retrieval, expands each hit to include the surrounding sentences. The model sees the exact match plus neighbors.

Storing the Window

During indexing you save the neighboring text in metadata so it can be stitched back at query time.

doc.metadata["window"] = " ".join(
    sentences[max(0, i-2): i+3]
)
doc.page_content = sentences[i]

Swapping Content After Search

After similarity search returns the matched sentence, replace its content with the stored window before passing it to the LLM.

for r in results:
    r.page_content = r.metadata["window"]

When to Use Each

Parent document suits structured docs with natural sections. Sentence-window suits dense prose where precise sentences matter most.

Avoiding Duplicate Parents

Multiple child hits can map to the same parent. Deduplicate by parent id so the LLM is not handed the same passage twice.

seen = set()
unique = []
for d in results:
    pid = d.metadata["parent_id"]
    if pid not in seen:
        seen.add(pid)
        unique.append(d)

Cost and Context Limits

Returning larger parents consumes more of the LLM context window. Balance the parent size against your token budget and the number of results k.

Putting It Together

Index fine-grained children, retrieve precisely, then expand to parents or windows. Your generation step receives focused yet contextual passages.

docs = retriever.invoke("cancellation terms")
context = "\n\n".join(d.page_content for d in docs)
answer = llm.invoke(f"Context:\n{context}\n\nQuestion: ...")

Quick Check

Test your understanding of decoupled retrieval.

Recap

You learned to decouple search and return units:

  • Parent document: search children, return parents
  • Sentence-window: match sentences, expand to neighbors
  • Deduplicate parents and watch context limits
เริ่มต้นได้ฟรี

เรียนรู้ LangChain / RAG / Vector DBs ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

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

บทเรียน “การค้นคืนเอกสารแม่และหน้าต่างประโยค” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การค้นคืนเอกสารแม่และหน้าต่างประโยค”

แยกส่วนที่ใช้ค้นหาออกจากส่วนที่ส่งคืน เพื่อให้ LLM ได้ผลลัพธ์ที่ตรงจุดพร้อมบริบทที่ครบถ้วน คุณปฏิบัติ 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. การบีบอัดบริบทด้วย LLM
  3. การค้นหาแบบผสมและการจัดอันดับใหม่
  4. การค้นคืนเอกสารแม่และหน้าต่างประโยค
← กลับไปที่ LangChain / RAG / Vector DBs