Home / File/ base.py — langchain Source File

base.py — langchain Source File

Architecture documentation for base.py, a python file in the langchain codebase. 12 imports, 0 dependents.

File python AgentOrchestration ClassicChains 12 imports 1 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  ab0b7396_014e_9570_8f95_c492a9ffc94b["base.py"]
  50e20440_a135_6be3_a5a5_67791be5a2a6["abc"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> 50e20440_a135_6be3_a5a5_67791be5a2a6
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> feec1ec4_6917_867b_d228_b134d0ff8099
  2485b66a_3839_d0b6_ad9c_a4ff40457dc6["langchain_core._api"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> 2485b66a_3839_d0b6_ad9c_a4ff40457dc6
  17a62cb3_fefd_6320_b757_b53bb4a1c661["langchain_core.callbacks"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> 17a62cb3_fefd_6320_b757_b53bb4a1c661
  6a98b0a5_5607_0043_2e22_a46a464c2d62["langchain_core.documents"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> 6a98b0a5_5607_0043_2e22_a46a464c2d62
  435e49bf_bb2e_2016_ead7_0afb9d57ad71["langchain_core.prompts"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> 435e49bf_bb2e_2016_ead7_0afb9d57ad71
  a8ec7563_2814_99b3_c6da_61c599efc542["langchain_core.runnables.config"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> a8ec7563_2814_99b3_c6da_61c599efc542
  314b1cc1_bd2e_bf43_4c2f_8c292c35f3e7["langchain_core.utils.pydantic"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> 314b1cc1_bd2e_bf43_4c2f_8c292c35f3e7
  7c6676be_7003_53c4_f08f_05a4d67a1cee["langchain_text_splitters"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> 7c6676be_7003_53c4_f08f_05a4d67a1cee
  dd5e7909_a646_84f1_497b_cae69735550e["pydantic"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> dd5e7909_a646_84f1_497b_cae69735550e
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> f85fae70_1011_eaec_151c_4083140ae9e5
  9a0fc770_8c3f_14bc_3c7d_37852927778e["langchain_classic.chains.base"]
  ab0b7396_014e_9570_8f95_c492a9ffc94b --> 9a0fc770_8c3f_14bc_3c7d_37852927778e
  style ab0b7396_014e_9570_8f95_c492a9ffc94b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Base interface for chains combining documents."""

from abc import ABC, abstractmethod
from typing import Any

from langchain_core._api import deprecated
from langchain_core.callbacks import (
    AsyncCallbackManagerForChainRun,
    CallbackManagerForChainRun,
)
from langchain_core.documents import Document
from langchain_core.prompts import BasePromptTemplate, PromptTemplate
from langchain_core.runnables.config import RunnableConfig
from langchain_core.utils.pydantic import create_model
from langchain_text_splitters import RecursiveCharacterTextSplitter, TextSplitter
from pydantic import BaseModel, Field
from typing_extensions import override

from langchain_classic.chains.base import Chain

DEFAULT_DOCUMENT_SEPARATOR = "\n\n"
DOCUMENTS_KEY = "context"
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template("{page_content}")


def _validate_prompt(prompt: BasePromptTemplate, document_variable_name: str) -> None:
    if document_variable_name not in prompt.input_variables:
        msg = (
            f"Prompt must accept {document_variable_name} as an input variable. "
            f"Received prompt with input variables: {prompt.input_variables}"
        )
        raise ValueError(msg)


class BaseCombineDocumentsChain(Chain, ABC):
    """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).
    """

    input_key: str = "input_documents"
    output_key: str = "output_text"

    @override
    def get_input_schema(
        self,
        config: RunnableConfig | None = None,
    ) -> type[BaseModel]:
        return create_model(
            "CombineDocumentsInput",
            **{self.input_key: (list[Document], None)},
        )

    @override
// ... (219 more lines)

Subdomains

Functions

Dependencies

  • abc
  • langchain_classic.chains.base
  • langchain_core._api
  • langchain_core.callbacks
  • langchain_core.documents
  • langchain_core.prompts
  • langchain_core.runnables.config
  • langchain_core.utils.pydantic
  • langchain_text_splitters
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does base.py do?
base.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ClassicChains subdomain.
What functions are defined in base.py?
base.py defines 1 function(s): _validate_prompt.
What does base.py depend on?
base.py imports 12 module(s): abc, langchain_classic.chains.base, langchain_core._api, langchain_core.callbacks, langchain_core.documents, langchain_core.prompts, langchain_core.runnables.config, langchain_core.utils.pydantic, and 4 more.
Where is base.py in the architecture?
base.py is located at libs/langchain/langchain_classic/chains/combine_documents/base.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