Home / Class/ MapReduceDocumentsChain Class — langchain Architecture

MapReduceDocumentsChain Class — langchain Architecture

Architecture documentation for the MapReduceDocumentsChain class in map_reduce.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  9fbee41f_c541_81ee_4b4c_ffdc2b275427["MapReduceDocumentsChain"]
  50b5f653_aed7_2d63_4a4b_6cf222b6a2f1["BaseCombineDocumentsChain"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|extends| 50b5f653_aed7_2d63_4a4b_6cf222b6a2f1
  674a7092_c07e_4023_0ea0_bda77a32a1a4["ReduceDocumentsChain"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|extends| 674a7092_c07e_4023_0ea0_bda77a32a1a4
  6f4a5e67_8497_3595_c67a_7c1d7cfdd969["map_reduce.py"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|defined in| 6f4a5e67_8497_3595_c67a_7c1d7cfdd969
  6cf16e41_3577_5f16_bde3_8db72bcb5c3b["get_output_schema()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| 6cf16e41_3577_5f16_bde3_8db72bcb5c3b
  02995300_e288_a807_de53_5732c5d02ad7["output_keys()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| 02995300_e288_a807_de53_5732c5d02ad7
  1dcf7314_a4f9_e467_39d1_4d839527a46d["get_reduce_chain()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| 1dcf7314_a4f9_e467_39d1_4d839527a46d
  2d8b9fbe_e661_8b07_4c35_4ab39eaf153a["get_return_intermediate_steps()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| 2d8b9fbe_e661_8b07_4c35_4ab39eaf153a
  e8186ec5_df07_78c9_34c3_f58883ed91b4["get_default_document_variable_name()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| e8186ec5_df07_78c9_34c3_f58883ed91b4
  288850ab_eb26_470c_44bd_3945194e44e5["collapse_document_chain()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| 288850ab_eb26_470c_44bd_3945194e44e5
  a5fbdc73_fff8_49fc_34e9_be8a1123ca76["combine_document_chain()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| a5fbdc73_fff8_49fc_34e9_be8a1123ca76
  dcc4b6f9_a127_6e78_dbe0_744ea153baa9["combine_docs()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| dcc4b6f9_a127_6e78_dbe0_744ea153baa9
  00f992fb_01d7_7dd8_3a6c_7edd7b9f25dc["acombine_docs()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| 00f992fb_01d7_7dd8_3a6c_7edd7b9f25dc
  87934057_db8b_cb4e_9ba9_0f4458067b52["_chain_type()"]
  9fbee41f_c541_81ee_4b4c_ffdc2b275427 -->|method| 87934057_db8b_cb4e_9ba9_0f4458067b52

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/combine_documents/map_reduce.py lines 29–294

class MapReduceDocumentsChain(BaseCombineDocumentsChain):
    """Combining documents by mapping a chain over them, then combining results.

    We first call `llm_chain` on each document individually, passing in the
    `page_content` and any other kwargs. This is the `map` step.

    We then process the results of that `map` step in a `reduce` step. This should
    likely be a ReduceDocumentsChain.

    Example:
        ```python
        from langchain_classic.chains import (
            StuffDocumentsChain,
            LLMChain,
            ReduceDocumentsChain,
            MapReduceDocumentsChain,
        )
        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)
        # We now define how to combine these summaries
        reduce_prompt = PromptTemplate.from_template(
            "Combine these summaries: {context}"
        )
        reduce_llm_chain = LLMChain(llm=model, prompt=reduce_prompt)
        combine_documents_chain = StuffDocumentsChain(
            llm_chain=reduce_llm_chain,
            document_prompt=document_prompt,
            document_variable_name=document_variable_name,
        )
        reduce_documents_chain = ReduceDocumentsChain(
            combine_documents_chain=combine_documents_chain,
        )
        chain = MapReduceDocumentsChain(
            llm_chain=llm_chain,
            reduce_documents_chain=reduce_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,
        )
        reduce_documents_chain = ReduceDocumentsChain(
            combine_documents_chain=combine_documents_chain,
            collapse_documents_chain=collapse_documents_chain,
        )
        chain = MapReduceDocumentsChain(
            llm_chain=llm_chain,
            reduce_documents_chain=reduce_documents_chain,
        )
        ```
    """

    llm_chain: LLMChain
    """Chain to apply to each document individually."""
    reduce_documents_chain: BaseCombineDocumentsChain
    """Chain to use to reduce the results of applying `llm_chain` to each doc.
    This typically either a ReduceDocumentChain or StuffDocumentChain."""
    document_variable_name: str
    """The variable name in the llm_chain to put the documents in.
    If only one variable in the llm_chain, this need not be provided."""
    return_intermediate_steps: bool = False
    """Return the results of the map steps in the output."""

Frequently Asked Questions

What is the MapReduceDocumentsChain class?
MapReduceDocumentsChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/combine_documents/map_reduce.py.
Where is MapReduceDocumentsChain defined?
MapReduceDocumentsChain is defined in libs/langchain/langchain_classic/chains/combine_documents/map_reduce.py at line 29.
What does MapReduceDocumentsChain extend?
MapReduceDocumentsChain extends BaseCombineDocumentsChain, ReduceDocumentsChain.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free