MapRerankDocumentsChain Class — langchain Architecture
Architecture documentation for the MapRerankDocumentsChain class in map_rerank.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD ee80b7d6_d087_8cc2_13e6_51659dee89b4["MapRerankDocumentsChain"] 2f364d76_a69d_403d_0a63_04792fe626bb["BaseCombineDocumentsChain"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|extends| 2f364d76_a69d_403d_0a63_04792fe626bb 5243b873_36fb_0c0c_e351_1b8d11b83852["RegexParser"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|extends| 5243b873_36fb_0c0c_e351_1b8d11b83852 d10633b6_45a8_4aea_a6ba_993083523db3["map_rerank.py"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|defined in| d10633b6_45a8_4aea_a6ba_993083523db3 75727667_b3f3_4021_f4ed_db40757fde23["get_output_schema()"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|method| 75727667_b3f3_4021_f4ed_db40757fde23 af780dbd_e12e_4b41_07ba_27cd9414e5e7["output_keys()"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|method| af780dbd_e12e_4b41_07ba_27cd9414e5e7 66907a22_1945_395e_6a94_37a8d00a56e2["validate_llm_output()"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|method| 66907a22_1945_395e_6a94_37a8d00a56e2 967c9508_b393_1557_3383_0fafe4e3d3d7["get_default_document_variable_name()"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|method| 967c9508_b393_1557_3383_0fafe4e3d3d7 3ee542d4_6c5f_5591_2901_27a70f133c10["combine_docs()"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|method| 3ee542d4_6c5f_5591_2901_27a70f133c10 5744a8f1_eae7_a731_34d0_2d43bba6a9cc["acombine_docs()"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|method| 5744a8f1_eae7_a731_34d0_2d43bba6a9cc 04e51b41_5072_aa90_26eb_d0a9395e5846["_process_results()"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|method| 04e51b41_5072_aa90_26eb_d0a9395e5846 52658435_620f_0f64_1f39_905ec1efaae4["_chain_type()"] ee80b7d6_d087_8cc2_13e6_51659dee89b4 -->|method| 52658435_620f_0f64_1f39_905ec1efaae4
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/combine_documents/map_rerank.py lines 30–245
class MapRerankDocumentsChain(BaseCombineDocumentsChain):
r"""Combining documents by mapping a chain over them, then reranking results.
This algorithm calls an LLMChain on each input document. The LLMChain is expected
to have an OutputParser that parses the result into both an answer (`answer_key`)
and a score (`rank_key`). The answer with the highest score is then returned.
Example:
```python
from langchain_classic.chains import MapRerankDocumentsChain, LLMChain
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from langchain_classic.output_parsers.regex import RegexParser
document_variable_name = "context"
model = OpenAI()
# The prompt here should take as an input variable the
# `document_variable_name`
# The actual prompt will need to be a lot more complex, this is just
# an example.
prompt_template = (
"Use the following context to tell me the chemical formula "
"for water. Output both your answer and a score of how confident "
"you are. Context: {context}"
)
output_parser = RegexParser(
regex=r"(.*?)\nScore: (.*)",
output_keys=["answer", "score"],
)
prompt = PromptTemplate(
template=prompt_template,
input_variables=["context"],
output_parser=output_parser,
)
llm_chain = LLMChain(llm=model, prompt=prompt)
chain = MapRerankDocumentsChain(
llm_chain=llm_chain,
document_variable_name=document_variable_name,
rank_key="score",
answer_key="answer",
)
```
"""
llm_chain: LLMChain
"""Chain to apply to each document individually."""
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."""
rank_key: str
"""Key in output of llm_chain to rank on."""
answer_key: str
"""Key in output of llm_chain to return as answer."""
metadata_keys: list[str] | None = None
"""Additional metadata from the chosen document to return."""
return_intermediate_steps: bool = False
"""Return intermediate steps.
Intermediate steps include the results of calling llm_chain on each document."""
model_config = ConfigDict(
arbitrary_types_allowed=True,
extra="forbid",
)
@override
def get_output_schema(
self,
config: RunnableConfig | None = None,
) -> type[BaseModel]:
schema: dict[str, Any] = {
self.output_key: (str, None),
}
if self.return_intermediate_steps:
schema["intermediate_steps"] = (list[str], None)
if self.metadata_keys:
schema.update(dict.fromkeys(self.metadata_keys, (Any, None)))
return create_model("MapRerankOutput", **schema)
@property
def output_keys(self) -> list[str]:
Source
Frequently Asked Questions
What is the MapRerankDocumentsChain class?
MapRerankDocumentsChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/combine_documents/map_rerank.py.
Where is MapRerankDocumentsChain defined?
MapRerankDocumentsChain is defined in libs/langchain/langchain_classic/chains/combine_documents/map_rerank.py at line 30.
What does MapRerankDocumentsChain extend?
MapRerankDocumentsChain extends BaseCombineDocumentsChain, RegexParser.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free