概述
这很重要,因为它消除了常见的块化权衡:小块准确匹配查询,而更大的父块为模型提供足够的上下文以正确回答。
深入探讨
Fixed-size chunking forces a trade-off. Small chunks, say a few sentences, produce sharp embeddings: the vector represents one idea, so a query about that idea matches it closely. But a two-sentence chunk rarely contains enough context for the model to answer well; it may omit the definition three paragraphs earlier or the exception in the next sentence. Large chunks carry that context, but their embeddings blur several ideas together, so retrieval gets less precise and the relevant passage can be outscored by a chunk that is merely on-topic. Small-to-big retrieval splits the two jobs. The system indexes small child chunks for search, and each child keeps a pointer to a larger parent: a section, a page, or the whole document. At query time it finds the best children, then looks up and returns their parents. If several children share one parent, the parent is sent once, which also removes duplicates. LangChain's ParentDocumentRetriever implements this with a vector store for children and a separate document store for parents. LlamaIndex offers related patterns: sentence-window retrieval, which returns a matched sentence plus a fixed number of neighboring sentences, and auto-merging retrieval, which replaces many retrieved leaf chunks with their shared parent node when enough of them match. The approach tends to beat fixed-size chunking on structured documents such as manuals, contracts, policies and textbooks, where meaning depends on the surrounding section. It helps less when documents are already short, or when parents are so large that a few of them overflow the context window or bury the answer in irrelevant text. A common misconception is that this is just using bigger chunks. It is not: the retrieval unit and the generation unit are deliberately different sizes, so you keep precise matching without starving the model of context.
现实世界的实施
内部IT帮助机器人将每个故障排除步骤索引为单独的子块,但当步骤匹配时,它会返回整个流程,因此模型不会让用户在没有步骤1至3的情况下执行步骤4。
法律研究工具匹配关于终止通知期限的单个条款,然后将完整合同段落(包括定义和改变条款含义的例外)传递给模型。
大学课程助手从讲义中搜索句子级块并返回周围页面,因此关于公式的答案也包含其适用条件。
保险索赔助手从单个保单文件检索多个小匹配项,按父块分组,并一次性发送该保单段落,而不是五个重叠片段。
父文档和小到大检索的未来
小到大正成为默认而非技巧,主要框架已将层级或窗口检索作为配置选项公开。更大的上下文窗口降低了发送更大父块的代价,使该模式更易采用。关于自动选择父边界仍有开放问题,例如使用文档布局分析或学习分割,而不是依赖标题。它正越来越多地与重排序器和上下文嵌入方法结合使用,尝试在索引时让小块了解其周围环境。在自己的文档上测试,而非通用基准,仍是选择子块和父块大小的可靠方式。
付诸实践
为什么这很重要
- 成本与预算. 多年来,架构决策决定着性能和运营成本。
- 更清晰的判决. 技术教育帮助团队选择正确的堆栈,而不仅仅是最新的堆栈。
- 质量控制. 更好的工程选择可以减少生产中的可靠性事故。
留意
- 优化一项基准测试可以隐藏更广泛的系统弱点。
- 基础设施和维护成本常常被低估。
- 随着系统变得更加复杂,安全性和可观察性差距可能会扩大。
后续步骤
- 在实施之前定义延迟、质量和成本目标。
- 在实际负载和数据条件下进行基准测试。
- 仪器监控错误、漂移和用户影响。
- 在扩展之前准备回滚和事件响应路径。
不断探索
常见问题
什么是父文档和小到大检索?
父文档检索,也称为小到大检索,搜索小而精确的文本块,但将更大的段落或文档交给语言模型。它很重要,因为它消除了常见的块化权衡:小块准确匹配查询,而更大的父块为模型提供足够的上下文以正确回答。
小到大检索解决了什么核心权衡?
小块文本能生成聚焦的嵌入,但上下文太少;大块文本能提供上下文,但嵌入会变得模糊。从小块到大块的搜索策略先检索小块,再返回大块,从而兼得两者。
In a small-to-big system, what is embedded and searched at query time?
Children are indexed for precise matching; parents are only looked up after a child matches.
If three retrieved child chunks all belong to the same parent section, what should the system do?
Grouping by parent means the section is included once, which saves tokens and removes duplicate fragments.
How does LangChain's ParentDocumentRetriever store the two levels of text?
Children are embedded in a vector store for search, while full parents sit in a document store and are fetched by ID.
What does sentence-window retrieval return?
Sentence-window retrieval matches at sentence level and then expands to a window of surrounding sentences to supply context.