Sub topic 8
Map-reduce: summarise the parts, then combine
This chapter assumes you can call an LLM from Python, know what a token is, and have seen the chunking function from the previous chapter. Here you will build a map-reduce summariser and learn where it breaks.
The idea
Map-reduce splits one hard job into many small ones. The map step summarises each chunk on its own. The reduce step combines those partial summaries into one. If the combined text is still too long for one call, you run the whole procedure again on it.
Because each map call sees only one chunk, the calls are independent. You can run them in parallel, which makes this strategy faster than refine for long documents.
The implementation
The function below uses chunk_paragraphs from the previous chapter. call_llm is any function that takes a prompt string and returns the model's text. count_tokens returns the token count of a string.
def summarise(text, call_llm, count_tokens, max_tokens=3000):
if count_tokens(text) <= max_tokens:
return call_llm('Summarise the following text in a short paragraph. Use only information in the text.\n\n' + text)
chunks = chunk_paragraphs(text, max_tokens, count_tokens)
partials = [
call_llm('Summarise this section in 5 bullet points. Use only information in the text.\n\n' + c)
for c in chunks
]
combined = '\n\n'.join(partials)
if count_tokens(combined) >= count_tokens(text):
raise ValueError('Partial summaries are not shorter than the input')
return summarise(combined, call_llm, count_tokens, max_tokens)Read it in three parts:
- Base case. If the text fits in the budget, one call produces the final summary.
- Map. Otherwise the text is chunked, and each chunk becomes five bullet points.
- Reduce. The bullets are joined, and the function calls itself on the result. Each pass shrinks the text, so the recursion ends.
The two prompts differ on purpose. The map prompt asks for bullets, which keeps partial summaries compact and easy to merge. The final prompt asks for a short paragraph, which is the output format the reader wants.
The guard against non-shrinking text
The check count_tokens(combined) >= count_tokens(text) stops infinite recursion. It can trigger when the budget is very small, because each chunk's five bullets may be as long as the chunk. It also triggers if the model ignores the length instruction. Raise an error and fix the cause, for example by raising max_tokens or asking for fewer bullets.
Also check that max_tokens leaves room for the prompt text and the model's reply. A chunk filled to the full context window leaves no space for either.
Common errors
- Lost links between chunks. A pronoun or a running argument that spans a chunk boundary is summarised without its context. The map step cannot see what came before.
- Order and emphasis drift. The reduce step sees only bullets. It cannot tell which points mattered most in the original, so it may weight them equally.
- Compounding errors. A mistake in a partial summary passes into every later pass. Nothing in the reduce step can check it against the source.
- Uneven chunks.
chunk_paragraphskeeps paragraphs whole. A single paragraph larger than the budget becomes an oversized chunk, which may exceed the context window. - Wrong prompt for the job. Asking every map call for the same bullets suits a report. It suits a legal contract poorly if you need every obligation listed.
Choosing it
Use map-reduce when the document is far larger than the context window, when parts can be summarised independently, and when speed matters. A report with separate sections fits well. A novel, where each chapter depends on earlier ones, fits less well. In that case the refine strategy in the next chapter keeps order better, at the cost of sequential calls.
Exercise
A 40,000-token document is summarised with max_tokens=4000. Roughly how many map calls run in the first pass, and why might the function raise ValueError instead of finishing?
Show answer
About 10 map calls, since 40,000 / 4,000 is 10, a little more if paragraph boundaries leave chunks under budget. The ValueError appears if the joined partial summaries are not shorter than the original, for example when the model writes long bullets or the budget is so small that five bullets per chunk take as many tokens as the chunk itself.