MapReduceChain Class — langchain Architecture
Architecture documentation for the MapReduceChain class in mapreduce.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 5201e8ad_1740_ce23_1ab6_cafdbc91f78c["MapReduceChain"] f3cef70e_11b0_61c9_7ec0_7308f4b45056["Chain"] 5201e8ad_1740_ce23_1ab6_cafdbc91f78c -->|extends| f3cef70e_11b0_61c9_7ec0_7308f4b45056 2b877f61_7216_3d99_402d_5c7078ad6783["mapreduce.py"] 5201e8ad_1740_ce23_1ab6_cafdbc91f78c -->|defined in| 2b877f61_7216_3d99_402d_5c7078ad6783 d4b15165_0193_5a67_e7ae_50a24fe2f0aa["from_params()"] 5201e8ad_1740_ce23_1ab6_cafdbc91f78c -->|method| d4b15165_0193_5a67_e7ae_50a24fe2f0aa 1f8d05e7_9a50_ba11_3f5e_938bf022930b["input_keys()"] 5201e8ad_1740_ce23_1ab6_cafdbc91f78c -->|method| 1f8d05e7_9a50_ba11_3f5e_938bf022930b dbf5f27c_025c_c644_9a87_7598ca49d74b["output_keys()"] 5201e8ad_1740_ce23_1ab6_cafdbc91f78c -->|method| dbf5f27c_025c_c644_9a87_7598ca49d74b d4377005_d592_4f81_ac05_8d26750ca867["_call()"] 5201e8ad_1740_ce23_1ab6_cafdbc91f78c -->|method| d4377005_d592_4f81_ac05_8d26750ca867
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/mapreduce.py lines 39–116
class MapReduceChain(Chain):
"""Map-reduce chain."""
combine_documents_chain: BaseCombineDocumentsChain
"""Chain to use to combine documents."""
text_splitter: TextSplitter
"""Text splitter to use."""
input_key: str = "input_text"
output_key: str = "output_text"
@classmethod
def from_params(
cls,
llm: BaseLanguageModel,
prompt: BasePromptTemplate,
text_splitter: TextSplitter,
callbacks: Callbacks = None,
combine_chain_kwargs: Mapping[str, Any] | None = None,
reduce_chain_kwargs: Mapping[str, Any] | None = None,
**kwargs: Any,
) -> MapReduceChain:
"""Construct a map-reduce chain that uses the chain for map and reduce."""
llm_chain = LLMChain(llm=llm, prompt=prompt, callbacks=callbacks)
stuff_chain = StuffDocumentsChain(
llm_chain=llm_chain,
callbacks=callbacks,
**(reduce_chain_kwargs or {}),
)
reduce_documents_chain = ReduceDocumentsChain(
combine_documents_chain=stuff_chain,
)
combine_documents_chain = MapReduceDocumentsChain(
llm_chain=llm_chain,
reduce_documents_chain=reduce_documents_chain,
callbacks=callbacks,
**(combine_chain_kwargs or {}),
)
return cls(
combine_documents_chain=combine_documents_chain,
text_splitter=text_splitter,
callbacks=callbacks,
**kwargs,
)
model_config = ConfigDict(
arbitrary_types_allowed=True,
extra="forbid",
)
@property
def input_keys(self) -> list[str]:
"""Expect input key."""
return [self.input_key]
@property
def output_keys(self) -> list[str]:
"""Return output key."""
return [self.output_key]
def _call(
self,
inputs: dict[str, str],
run_manager: CallbackManagerForChainRun | None = None,
) -> dict[str, str]:
_run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager()
# Split the larger text into smaller chunks.
doc_text = inputs.pop(self.input_key)
texts = self.text_splitter.split_text(doc_text)
docs = [Document(page_content=text) for text in texts]
_inputs: dict[str, Any] = {
**inputs,
self.combine_documents_chain.input_key: docs,
}
outputs = self.combine_documents_chain.run(
_inputs,
callbacks=_run_manager.get_child(),
)
return {self.output_key: outputs}
Extends
Source
Frequently Asked Questions
What is the MapReduceChain class?
MapReduceChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/mapreduce.py.
Where is MapReduceChain defined?
MapReduceChain is defined in libs/langchain/langchain_classic/chains/mapreduce.py at line 39.
What does MapReduceChain extend?
MapReduceChain extends Chain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free