0Pricing
LangChain / RAG / Vector DBs · درس

استرجاع المستند الأصلي ونافذة الجملة

افصل بين المقاطع التي تبحث فيها والمقاطع التي تعيدها، حتى يحصل LLM على مطابقات دقيقة مع سياق غني.

استرجاع المستند الأصلي ونافذة الجملة درس مجاني في LangChain / RAG / Vector DBs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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

الأسئلة الشائعة

هل درس «استرجاع المستند الأصلي ونافذة الجملة» مجاني؟

نعم — نص درس «استرجاع المستند الأصلي ونافذة الجملة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة LangChain / RAG / Vector DBs، انتقل إلى CoddyKit PRO. تتضمن دورة LangChain / RAG / Vector DBs 4 دروس في المجموع.

ماذا ستتعلم في «استرجاع المستند الأصلي ونافذة الجملة»؟

افصل بين المقاطع التي تبحث فيها والمقاطع التي تعيدها، حتى يحصل LLM على مطابقات دقيقة مع سياق غني. تتمرن على LangChain / RAG / Vector DBs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ LangChain / RAG / Vector DBs؟

لا تُشترط خبرة سابقة. LangChain / RAG / Vector DBs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «استرجاع المستند الأصلي ونافذة الجملة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس LangChain / RAG / Vector DBs هذا؟

نعم. كل درس في LangChain / RAG / Vector DBs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. استراتيجيات الاسترجاع متعدد الاستعلامات
  2. الضغط السياقي باستخدام LLMs
  3. البحث الهجين وإعادة الترتيب
  4. استرجاع المستند الأصلي ونافذة الجملة
← العودة إلى LangChain / RAG / Vector DBs