map_rerank.py — langchain Source File
Architecture documentation for map_rerank.py, a python file in the langchain codebase. 12 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR d10633b6_45a8_4aea_a6ba_993083523db3["map_rerank.py"] 2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"] d10633b6_45a8_4aea_a6ba_993083523db3 --> 2bf6d401_816d_d011_3b05_a6114f55ff58 feec1ec4_6917_867b_d228_b134d0ff8099["typing"] d10633b6_45a8_4aea_a6ba_993083523db3 --> feec1ec4_6917_867b_d228_b134d0ff8099 2485b66a_3839_d0b6_ad9c_a4ff40457dc6["langchain_core._api"] d10633b6_45a8_4aea_a6ba_993083523db3 --> 2485b66a_3839_d0b6_ad9c_a4ff40457dc6 17a62cb3_fefd_6320_b757_b53bb4a1c661["langchain_core.callbacks"] d10633b6_45a8_4aea_a6ba_993083523db3 --> 17a62cb3_fefd_6320_b757_b53bb4a1c661 6a98b0a5_5607_0043_2e22_a46a464c2d62["langchain_core.documents"] d10633b6_45a8_4aea_a6ba_993083523db3 --> 6a98b0a5_5607_0043_2e22_a46a464c2d62 a8ec7563_2814_99b3_c6da_61c599efc542["langchain_core.runnables.config"] d10633b6_45a8_4aea_a6ba_993083523db3 --> a8ec7563_2814_99b3_c6da_61c599efc542 314b1cc1_bd2e_bf43_4c2f_8c292c35f3e7["langchain_core.utils.pydantic"] d10633b6_45a8_4aea_a6ba_993083523db3 --> 314b1cc1_bd2e_bf43_4c2f_8c292c35f3e7 dd5e7909_a646_84f1_497b_cae69735550e["pydantic"] d10633b6_45a8_4aea_a6ba_993083523db3 --> dd5e7909_a646_84f1_497b_cae69735550e f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"] d10633b6_45a8_4aea_a6ba_993083523db3 --> f85fae70_1011_eaec_151c_4083140ae9e5 f40facfc_f3e4_c00f_e491_0760270cea61["langchain_classic.chains.combine_documents.base"] d10633b6_45a8_4aea_a6ba_993083523db3 --> f40facfc_f3e4_c00f_e491_0760270cea61 4044d59c_c0a5_a371_f49b_bea3da4e20ac["langchain_classic.chains.llm"] d10633b6_45a8_4aea_a6ba_993083523db3 --> 4044d59c_c0a5_a371_f49b_bea3da4e20ac c1b1b626_aa3d_1efe_18fa_d17344e01056["langchain_classic.output_parsers.regex"] d10633b6_45a8_4aea_a6ba_993083523db3 --> c1b1b626_aa3d_1efe_18fa_d17344e01056 style d10633b6_45a8_4aea_a6ba_993083523db3 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Combining documents by mapping a chain over them first, then reranking results."""
from __future__ import annotations
from collections.abc import Sequence
from typing import Any, cast
from langchain_core._api import deprecated
from langchain_core.callbacks import Callbacks
from langchain_core.documents import Document
from langchain_core.runnables.config import RunnableConfig
from langchain_core.utils.pydantic import create_model
from pydantic import BaseModel, ConfigDict, model_validator
from typing_extensions import Self, override
from langchain_classic.chains.combine_documents.base import BaseCombineDocumentsChain
from langchain_classic.chains.llm import LLMChain
from langchain_classic.output_parsers.regex import RegexParser
@deprecated(
since="0.3.1",
removal="1.0",
message=(
"This class is deprecated. Please see the migration guide here for "
"a recommended replacement: "
"https://python.langchain.com/docs/versions/migrating_chains/map_rerank_docs_chain/"
),
)
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,
// ... (186 more lines)
Domain
Subdomains
Classes
Dependencies
- collections.abc
- langchain_classic.chains.combine_documents.base
- langchain_classic.chains.llm
- langchain_classic.output_parsers.regex
- langchain_core._api
- langchain_core.callbacks
- langchain_core.documents
- langchain_core.runnables.config
- langchain_core.utils.pydantic
- pydantic
- typing
- typing_extensions
Source
Frequently Asked Questions
What does map_rerank.py do?
map_rerank.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ClassicChains subdomain.
What does map_rerank.py depend on?
map_rerank.py imports 12 module(s): collections.abc, langchain_classic.chains.combine_documents.base, langchain_classic.chains.llm, langchain_classic.output_parsers.regex, langchain_core._api, langchain_core.callbacks, langchain_core.documents, langchain_core.runnables.config, and 4 more.
Where is map_rerank.py in the architecture?
map_rerank.py is located at libs/langchain/langchain_classic/chains/combine_documents/map_rerank.py (domain: AgentOrchestration, subdomain: ClassicChains, directory: libs/langchain/langchain_classic/chains/combine_documents).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free