ReduceDocumentsChain()The keys to use for the 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.