Chain that splits documents, then analyzes it in pieces.
This chain is parameterized by a TextSplitter and a CombineDocumentsChain. This chain takes a single document as input, and then splits it up into chunks and then passes those chucks to the CombineDocumentsChain.
This class is deprecated. See below for alternative implementations which supports async and streaming modes of operation.
If the underlying combine documents chain takes one input_documents argument
(e.g., chains generated by load_summarize_chain):
split_text = lambda x: text_splitter.create_documents([x])
summarize_document_chain = split_text | chain
If the underlying chain takes additional arguments (e.g., load_qa_chain, which
takes an additional question argument), we can use the following:
from operator import itemgetter
from langchain_core.runnables import RunnableLambda, RunnableParallel
split_text = RunnableLambda(lambda x: text_splitter.create_documents([x]))
summarize_document_chain = RunnableParallel(
question=itemgetter("question"),
input_documents=itemgetter("input_document") | split_text,
) | chain.pick("output_text")
To additionally return the input parameters, as AnalyzeDocumentChain does,
we can wrap this construction with RunnablePassthrough:
from operator import itemgetter
from langchain_core.runnables import (
RunnableLambda,
RunnableParallel,
RunnablePassthrough,
)
split_text = RunnableLambda(lambda x: text_splitter.create_documents([x]))
summarize_document_chain = RunnablePassthrough.assign(
output_text=RunnableParallel(
question=itemgetter("question"),
input_documents=itemgetter("input_document") | split_text,
)
| chain.pick("output_text")
)Asynchronously execute the chain.
Expect input key.
Return output key.