父文档与句子窗口检索
将搜索的文本块与返回的文本块解耦,让 LLM 获得精准匹配和丰富上下文。
父文档与句子窗口检索 是 CoddyKit 上的免费 LangChain / RAG / Vector DBs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 textSentence-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
用 AI 导师学习 LangChain / RAG / Vector DBs — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「父文档与句子窗口检索」课时是免费的吗?
是的 — 「父文档与句子窗口检索」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LangChain / RAG / Vector DBs 课程的其余内容,请升级到 CoddyKit PRO。 LangChain / RAG / Vector DBs 课程共包含 4 节课。
「父文档与句子窗口检索」这节课中我会学到什么?
将搜索的文本块与返回的文本块解耦,让 LLM 获得精准匹配和丰富上下文。 你通过在浏览器中直接运行的动手代码来练习 LangChain / RAG / Vector DBs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 LangChain / RAG / Vector DBs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 LangChain / RAG / Vector DBs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「父文档与句子窗口检索」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 LangChain / RAG / Vector DBs 课中编写并运行代码吗?
能。每节 LangChain / RAG / Vector DBs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 多查询检索策略
- 使用 LLM 进行上下文压缩
- 混合搜索与重新排序
- 父文档与句子窗口检索