langchain.chains.combine_documents.reduce.ReduceDocumentsChain¶
class langchain.chains.combine_documents.reduce.ReduceDocumentsChain[source]¶
Bases: 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
from langchain.chains import (
    StuffDocumentsChain, LLMChain, ReduceDocumentsChain
)
from langchain.prompts import PromptTemplate
from langchain.llms 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"
llm = 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=llm, 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