Exercise: a summary tree against flat retrieval
- Track
- Knowledge Graphs & GraphRAG
- By
- Sourav Banerjee
- Last updated
- 30 Nov 2026
01 Context
Problem statement
Larkspur Cloud's operations lead asks questions that span the whole handbook, such as "compare the three plans" or "pull together everything about regions". No single chunk answers them. You build a RAPTOR-style summary tree over the handbook with a local model, answer six whole-corpus questions from it, and compare the answers with flat retrieval over the same chunks.
The rule you apply comes from lesson 1 of T13.1: compare flat retrieval and the tree at the same final budget, and follow a summary back to its passages before you trust a number in it. A summary can reach the right files and still drop the fact the question asks for.
02 Outcomes
What you will learn
- Build a summary tree by clustering, summarising and repeating, and keep each node's level, children and source files.
- Search the collapsed tree and the flat chunks with the same retriever and the same
k. - Score answers against checkable facts instead of a judge model.
- Separate what retrieval found (facts in context) from what the model kept (facts in answer).
- Spot lossy compression, where a summary covers a file but no longer carries its numbers.
03 Setup
Before you start
Lessons
- T13.1 Hierarchical & Memory-Augmented Retrieval: lesson 1 of T13.1 RAPTOR: clustering and recursive summarisation for abstraction levels, the build loop, collapsed-tree retrieval and lossy compression.
- T13.3 GraphRAG Techniques: lesson 3 of T13.3 Microsoft GraphRAG: communities, global vs local search for the other way to pre-summarise a corpus, community reports, and why global questions need it.
- T5.1 Why RAG & the RAG Lifecycle: lesson 2 of T5.1 Indexing from scratch with TF-IDF and nearest neighbours for the flat baseline.
Dataset
The Larkspur Cloud handbook (13 files) and six whole-corpus questions, in data/. Each question lists the handbook files its answer draws on and the facts a full answer must contain, such as 600, 99.95 or Singapore.
Code
load_handbook, load_jsonl, normalise and tfidf_retriever from src/lab_kit.py, the recursive splitter from langchain_text_splitters, GaussianMixture, TfidfVectorizer and TruncatedSVD from scikit-learn, and ChatOllama from langchain_ollama.
Environment
The course's default environment (uv run jupyter lab), plus Ollama running locally with ollama pull qwen2.5:3b done. No API keys. The module lab uses a hosted chat model and hosted embeddings. Here qwen2.5:3b writes the summaries and answers, and TF-IDF stands in for the embeddings in both clustering and retrieval.
04 Architecture
Experiment workflow
The chunks, the retriever, k = 4, the answer prompt, the model and the questions stay fixed. Only the pool that retrieval ranks changes between the two main rows.
Extension: a third row that adds each summary hit's best source chunk to the tree's context.
05 Build
Implementation tasks
Task 1: Load the data
Load the handbook from
data/larkspur/handbook/and the questions fromdata/global_questions.jsonl.Task 2: Build level 0
Split every handbook file with the recursive splitter at 500 characters and 50 of overlap. Store each chunk as a node with an id,
level = 0, its text, its source file and an empty child list.RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)Task 3: Build the tree
Run the lesson's loop for at most three rounds. Vectorise the current level, cluster it with a Gaussian mixture into
min(10, n // 2)clusters, summarise each cluster withqwen2.5:3b, and treat the summaries as the next level. Every summary node records its children and the union of their source files. Count the summary calls and time the build.GaussianMixture(n_components=min(10, len(texts) // 2), covariance_type="diag", random_state=0) "Summarize the following text concisely:\n\n{text}"New here: the vectors are TF-IDF reduced to at most 8 dimensions with
TruncatedSVD(random_state=0), because a Gaussian mixture cannot fit thousands of sparse dimensions from 30 points.Task 4: Two retrievers
Build one TF-IDF retriever over the level-0 chunks only (flat) and one over every node in the tree (collapsed). Both keep the top 4.
Task 5: Answer and score
Answer each question once per row with the same prompt. Count a fact when one of its spellings appears in the text as a whole word or number, so
60does not match600. Record the share of facts in the retrieved context, the share in the answer, and the share of the question's handbook files that the retrieved nodes descend from.New here: the fact check is a regular expression with word boundaries over
normalise(text), a few lines in the notebook.Task 6: Read the disagreements
For every question, list the facts the flat context held and the tree's context did not. Open the summary that took the place of the chunk and check what it kept.
Task 7: Extension: follow each summary back
Keep the tree's top 4, then for each summary hit add its highest-scoring level-0 chunk that is not already in the context. This row has a larger budget, so compare it only with care.
06 Measure
Record results
What you enter stays in this browser only.
| Retrieval | Summary hits | Context words | Facts in context | Facts in answer | Files reached |
|---|---|---|---|---|---|
| Flat chunks, k = 4 | 0 | ||||
| Collapsed tree, k = 4 | |||||
| Tree plus one source per summary (extension) |
Summary hits counts summary nodes among the retrieved nodes over all six questions. The three shares are means over the six questions. Files reached credits a summary with every file under it.
07 Verify
Done when
0 of 4 done
What you enter stays in this browser only.
08 Debug
Common mistakes
Trusting files reached
A summary is credited with every file under it, so files reached rises whenever summaries enter the context. It says nothing about whether the facts survived the summary.
Different budgets
Comparing the tree at k = 8 with flat at k = 4 measures the budget, not the tree. Hold k fixed for the main rows.
Reading a fact check as a correctness check
A string check sees that 24 hours appears, not which plan it is attached to. Read the answers for the questions that score well, too.
Building the tree on the questions
The tree is built from the handbook alone. Writing or tuning summaries with the questions in view makes the comparison meaningless.
09 Transfer
Interview angle
“When would you index a corpus as a summary tree instead of flat chunks?”
Show a model answer
When questions ask for themes that span many documents and no chunk holds the answer. Measure it at the same budget as flat retrieval, keep source links on every summary, and fetch the source passages before quoting a number, because summaries drop specifics.
10 Review
Worked solution
The worked solution is solution/p12_exercise_solution.ipynb (open in Colab). Locally, open it from exercise/solution/ so ../data/ resolves, with Ollama running and qwen2.5:3b pulled, and run it top to bottom. In Colab, clone the repository first and install Ollama in the runtime, because the notebook reads the corpus and src/lab_kit.py from the repository and calls a local model.
Show our run and the reasoning
Our run: qwen2.5:3b through Ollama 0.40.6, langchain-ollama 1.1.0, scikit-learn 1.9.1 and langchain-text-splitters 1.1.3, offline.
| Retrieval | Summary hits | Context words | Facts in context | Facts in answer | Files reached |
|---|---|---|---|---|---|
| Flat chunks, k = 4 | 0 | 238 | 0.75 | 0.70 | 0.54 |
| Collapsed tree, k = 4 | 14 | 433 | 0.53 | 0.45 | 0.61 |
| Tree plus one source per summary (extension) | 14 | 567 | 0.56 | 0.44 | 0.61 |
The tree had 47 nodes: 30 chunks, 10 level-1 summaries, 5 level-2 and 2 level-3. It took 17 summary calls and 680 seconds to build on a shared Ollama server. Flat retrieval makes no model calls to index.
| Question | Flat, facts in answer | Tree, facts in answer | Summaries in the tree's top 4 |
|---|---|---|---|
| w01 Compare the three plans | 0.91 | 1.00 | 4 |
| w02 Deadlines a customer must meet | 0.00 | 0.00 | 3 |
| w03 How an outage is handled | 0.62 | 0.50 | 2 |
| w04 Everything about regions | 0.86 | 0.14 | 1 |
| w05 Credits and refunds not paid as cash | 0.80 | 0.40 | 2 |
| w06 Security measures | 1.00 | 0.67 | 2 |
The tree reached more files and lost more facts. Summaries took 14 of the 24 retrieval slots, and they cover more of the handbook than chunks do, so files reached rose from 0.54 to 0.61. But the facts in context fell from 0.75 to 0.53. On w04 the regions chunk, which names all five regions, was pushed out by the status-page summary L2.4, and the five region names left the context. On w05 a summary replaced the refunds chunk, and "original payment method" and "5 to 10" business days went with it. On w06 two summaries took the place of the API-key chunk, and neither kept the 24-hour key overlap or the audit log. Elsewhere in the tree, L1.3 turned that overlap into "a new key takes effect after 24 hours", which is wrong. That is lossy compression, as lesson 1 of T13.1 describes it.
The one tree win, w01, needs a second look. Its answer has every checked fact, but it says Business answers tickets within 24 hours and Starter within 4, which is the wrong way round. The level-1 summary L1.6 already said Starter answers within 4 hours, so the error came from the summary, not the answer step. The flat answer got Starter wrong too. A fact check cannot see either mistake. w02 fails for both rows, because TF-IDF matches "time limits" to the API rate-limit chunks.
Adding one source chunk per summary barely helped. It raised facts in context from 0.53 to 0.56, and the answers kept no more of them. The best-scoring chunk under a summary was usually not the one holding the missing fact. For this small handbook, flat retrieval is the better default, and the tree earns a place only for questions whose answers are themes rather than numbers. A 3B model writes loose summaries, and a stronger summariser or a prompt that asks it to keep every number would change these rows.