ReduceDocumentsChain()Return output key.
Optional memory object.
Optional list of callback handlers (or callback manager).
Whether or not run in verbose mode. In verbose mode, some intermediate logs
Optional list of tags associated with the chain.
Optional metadata associated with the chain.
[DEPRECATED] Use callbacks instead.
Keys expected to be in the chain input.
Keys expected to be in the chain output.
Combine documents by recursively reducing them.
This involves
combine_documents_chaincollapse_documents_chaincombine_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_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,
)Set the chain verbosity.