Base interface for chains combining documents.
Abstract base class for creating structured sequences of calls to components.
Chains should be used to encode a sequence of calls to components like models, document retrievers, other chains, etc., and provide a simple interface to this sequence.
Base interface for chains combining documents.
Subclasses of this chain deal with combining documents in a variety of
ways. This base class exists to add some uniformity in the interface these types
of chains should expose. Namely, they expect an input key related to the documents
to use (default input_documents), and then also expose a method to calculate
the length of a prompt from documents (useful for outside callers to use to
determine whether it's safe to pass a list of documents into this chain or whether
that will be longer than the context length).
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")
)