ReduceDocumentsChain Class — langchain Architecture
Architecture documentation for the ReduceDocumentsChain class in reduce.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 1d761688_0112_f998_eecc_6712fc22bf37["ReduceDocumentsChain"] 2f364d76_a69d_403d_0a63_04792fe626bb["BaseCombineDocumentsChain"] 1d761688_0112_f998_eecc_6712fc22bf37 -->|extends| 2f364d76_a69d_403d_0a63_04792fe626bb c47d011a_bcd5_947b_be68_ac5c423fea9d["reduce.py"] 1d761688_0112_f998_eecc_6712fc22bf37 -->|defined in| c47d011a_bcd5_947b_be68_ac5c423fea9d c5960e62_1928_800b_70a8_da426922cc67["_collapse_chain()"] 1d761688_0112_f998_eecc_6712fc22bf37 -->|method| c5960e62_1928_800b_70a8_da426922cc67 cc4540ae_400a_89bd_9f59_c90678c2a044["combine_docs()"] 1d761688_0112_f998_eecc_6712fc22bf37 -->|method| cc4540ae_400a_89bd_9f59_c90678c2a044 924b164e_b8f4_c752_6dc9_c259afef92be["acombine_docs()"] 1d761688_0112_f998_eecc_6712fc22bf37 -->|method| 924b164e_b8f4_c752_6dc9_c259afef92be 5ec5e85d_0e34_733e_07b9_d13ee52ef47f["_collapse()"] 1d761688_0112_f998_eecc_6712fc22bf37 -->|method| 5ec5e85d_0e34_733e_07b9_d13ee52ef47f 4961cdaa_eb66_8549_74f5_80aa6ef431de["_acollapse()"] 1d761688_0112_f998_eecc_6712fc22bf37 -->|method| 4961cdaa_eb66_8549_74f5_80aa6ef431de 7224f07f_aa09_7702_25d6_feae4d7180ae["_chain_type()"] 1d761688_0112_f998_eecc_6712fc22bf37 -->|method| 7224f07f_aa09_7702_25d6_feae4d7180ae
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/combine_documents/reduce.py lines 140–389
class ReduceDocumentsChain(BaseCombineDocumentsChain):
"""Combine documents by recursively reducing them.
This involves
- `combine_documents_chain`
- `collapse_documents_chain`
`combine_documents_chain` is ALWAYS provided. This is final chain that is called.
We pass all previous results to this chain, and the output of this chain is
returned as a final result.
`collapse_documents_chain` is used if the documents passed in are too many to all
be passed to `combine_documents_chain` in one go. In this case,
`collapse_documents_chain` is called recursively on as big of groups of documents
as are allowed.
Example:
```python
from langchain_classic.chains import (
StuffDocumentsChain,
LLMChain,
ReduceDocumentsChain,
)
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
# This controls how each document will be formatted. Specifically,
# it will be passed to `format_document` - see that function for more
# details.
document_prompt = PromptTemplate(
input_variables=["page_content"], template="{page_content}"
)
document_variable_name = "context"
model = OpenAI()
# The prompt here should take as an input variable the
# `document_variable_name`
prompt = PromptTemplate.from_template("Summarize this content: {context}")
llm_chain = LLMChain(llm=model, prompt=prompt)
combine_documents_chain = StuffDocumentsChain(
llm_chain=llm_chain,
document_prompt=document_prompt,
document_variable_name=document_variable_name,
)
chain = ReduceDocumentsChain(
combine_documents_chain=combine_documents_chain,
)
# If we wanted to, we could also pass in collapse_documents_chain
# which is specifically aimed at collapsing documents BEFORE
# the final call.
prompt = PromptTemplate.from_template("Collapse this content: {context}")
llm_chain = LLMChain(llm=model, prompt=prompt)
collapse_documents_chain = StuffDocumentsChain(
llm_chain=llm_chain,
document_prompt=document_prompt,
document_variable_name=document_variable_name,
)
chain = ReduceDocumentsChain(
combine_documents_chain=combine_documents_chain,
collapse_documents_chain=collapse_documents_chain,
)
```
"""
combine_documents_chain: BaseCombineDocumentsChain
"""Final chain to call to combine documents.
This is typically a `StuffDocumentsChain`.
"""
collapse_documents_chain: BaseCombineDocumentsChain | None = None
"""Chain to use to collapse documents if needed until they can all fit.
If `None`, will use the `combine_documents_chain`.
This is typically a `StuffDocumentsChain`.
"""
token_max: int = 3000
"""The maximum number of tokens to group documents into.
For example, if set to 3000 then documents will be grouped into chunks of no greater
than 3000 tokens before trying to combine them into a smaller chunk.
Extends
Source
Frequently Asked Questions
What is the ReduceDocumentsChain class?
ReduceDocumentsChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/combine_documents/reduce.py.
Where is ReduceDocumentsChain defined?
ReduceDocumentsChain is defined in libs/langchain/langchain_classic/chains/combine_documents/reduce.py at line 140.
What does ReduceDocumentsChain extend?
ReduceDocumentsChain extends BaseCombineDocumentsChain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free