0Pricing
LangChain / RAG / Vector DBs · Lekcja

Wyszukiwanie dokumentu nadrzędnego i okna zdań

Oddziel fragmenty przeszukiwane od fragmentów zwracanych, aby LLM otrzymywał precyzyjne dopasowania wraz z bogatym kontekstem.

Wyszukiwanie dokumentu nadrzędnego i okna zdań to bezpłatna lekcja LangChain / RAG / Vector DBs na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej LangChain / RAG / Vector DBs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs LangChain / RAG / Vector DBs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Wyszukiwanie dokumentu nadrzędnego i okna zdań” jest bezpłatna?

Tak — pełny tekst „Wyszukiwanie dokumentu nadrzędnego i okna zdań” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu LangChain / RAG / Vector DBs, przejdź na CoddyKit PRO. Kurs LangChain / RAG / Vector DBs zawiera 4 lekcji w sumie.

Co nauczysz się w „Wyszukiwanie dokumentu nadrzędnego i okna zdań”?

Oddziel fragmenty przeszukiwane od fragmentów zwracanych, aby LLM otrzymywał precyzyjne dopasowania wraz z bogatym kontekstem. Ćwiczysz LangChain / RAG / Vector DBs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć LangChain / RAG / Vector DBs?

Nie wymagamy żadnego doświadczenia. LangChain / RAG / Vector DBs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Wyszukiwanie dokumentu nadrzędnego i okna zdań”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji LangChain / RAG / Vector DBs?

Tak. Każda lekcja LangChain / RAG / Vector DBs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Strategie wyszukiwania wielozapytaniowego
  2. Kompresja kontekstu z użyciem LLM
  3. Wyszukiwanie hybrydowe i ponowne rangowanie
  4. Wyszukiwanie dokumentu nadrzędnego i okna zdań
← Powrót do LangChain / RAG / Vector DBs