文件放不进 context window:
怎么找到一段具体内容,怎么总结整本书?
先弄清楚:每个概念是什么
- Token budget
- context window 里能留给 evidence 的额度。面试官给的数字是 32K。扣掉 instruction、问题和回答,剩下的才能放 chunk。
- 按结构切 chunk
- 沿标题和章节切,不是每隔固定长度切一刀,一个 recipe 是一个 chunk。基础见 RAG。
- Tree index
- leaf 是原文 chunk,parent 是 children 的 LLM summary,一层层往上直到 root。书有目录时,标题层级就是现成的 tree。
看图:tree 上怎么找,整本书怎么总结
30 秒回答
I'd start with the simplest thing that works. Split the file along its structure, embed each chunk, score every chunk against the query, sort, and fill the token budget with the best ones; the model answers only from those. If recall is poor, I add BM25 for exact names and pull in the whole section around a hit. A summary tree helps broad questions: each parent is an LLM summary of its children, and I rank nodes from all levels together. For a whole-book summary there is no query to rank by, so I summarize each chapter in parallel, then summarize the summaries, adding a level if they still don't fit.
Case 1:找 croissant recipe
| 版本 | 做法 | 还剩的问题 |
|---|---|---|
| v1 Rank + budget | 按标题切 chunk,每块和 query 算 similarity,排序后从高到低装满 token budget。面试官说这是 easiest approach | 一个 recipe 被切成几块;专名靠 embedding 不稳 |
| v2 Hybrid + 取整节 | 加 BM25 匹配专名,命中后取所在的整节 | 问得宽时,没有哪个 leaf 足够像 |
| v3 Tree | 建 tree index,各层节点都能被检索到 | 建 index 要多次调用 LLM;summary 会丢细节 |
面试官的追问:tree 上怎么走
| 追问 | 回答 |
|---|---|
| Chapter 3 和 3.b 的 embedding | Chapter 3 的来自它的 summary,概括 3.a–3.c。3.b 的来自自己的原文。所以具体问题和 3.b 更像,宽问题和 chapter 3 更像 |
| Flow | 在 root 层取 similarity 最高的 k 个,只在它们的 children 里再取 top-k,一层层往下 |
| Where to stop | 到 leaf 停,或者 token budget 装满 |
| What to take | Tree traversal 把每层选中的节点都留下。Collapsed tree 不分层,所有节点一起按 similarity 排序,按 token budget 截断 |
| What to show | 选中节点的文本带上标题路径,按原文顺序排。parent 和它的 child 都选中时只留 child,这一条是设计取舍,RAPTOR 没有这一步 |
RAPTOR 的实验里 collapsed tree 比 tree traversal 好。它是在 QASPER 的 20 篇上比的,主实验把上限定在 2000 token,大约是 top-20 个节点。Collapsed tree 就是 v1 的做法用在 tree 的节点上。
Case 2:总结整本书
- 切:按章切,一章还放不下就再按节切
- Map:每块各自 summarize,可以并行
- Reduce:把这些 summary 合起来再 summarize 一次,还超 token budget 就分组多做一层
有 tree 时,root 层的 summary 就是答案,建 index 时算好并缓存。另一种做法是 refine,带着当前的 summary 顺序读下一块。它不能并行,结果还受顺序影响。
手算一遍。书有 200K token,token budget 是 32K。按 8K 一块切成 25 块,每块压成 500 token,合起来 12.5K,一次 reduce 就放得下,一共调用 LLM 26 次。
必答点
- 先给最简单的:切 chunk → 算 similarity → 排序 → 按 token budget 截断
- chunk 沿文档结构切,命中后取整节
- 专名靠 BM25,同义改写靠 embedding
- tree:parent 是 children 的 summary;具体问题命中 leaf,宽问题命中 parent
- 没有 query 的总结用 map-reduce,summary 在建 index 时缓存
- 说了 tree 或 graph,就要答得出 flow、stop、take、show
追问
这两种情况怎么 eval?
Case 1 先看答案所在的 chunk 有没有进 context,再看回答的 groundedness(见 grounded 回答)。Case 2 看 summary 覆盖了多少 key point,以及每一句在原文里有没有依据。
排在最前面的 chunk 也不相关,怎么办?
给 similarity 设一个阈值,都低于它就说书里没找到,不硬答(见 abstention)。
文件没有目录和标题,怎么建 tree?
先按固定长度切 leaf,再按 embedding 做 clustering,每个 cluster 生成一个 summary 当 parent,重复到聚不动为止。RAPTOR 的 leaf 是 100 token,clustering 用 GMM,一个节点可以属于多个 cluster。
换一个 context 更长的模型,把整本书放进去不行吗?
题目的前提是放不下。就算放得下,context 越长成本越高,中间的内容也更容易被忽略(见 RAG 的 Lost in the Middle)。
来源作者本人 2026-11-09 一面的记录(ml-八股 09 第 40 题)· RAPTOR(tree 的构建、tree traversal 和 collapsed tree、QASPER 上的比较)