LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • MCP Adapters
    Standard Tests
    Text Splitters
    • Overview
    • Agents
    • Callbacks
    • Chains
    • Chat models
    • Embeddings
    • Evaluation
    • Globals
    • Hub
    • Memory
    • Output parsers
    • Retrievers
    • Runnables
    • LangSmith
    • Storage
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    MCP Adapters
    Standard Tests
    Text Splitters
    OverviewAgentsCallbacksChainsChat modelsEmbeddingsEvaluationGlobalsHubMemoryOutput parsersRetrieversRunnablesLangSmithStorage
    Language
    Theme
    Pythonlangchain-classicchainsloading
    Module●Since v1.0

    loading

    Functionality for loading chains.

    Attributes

    Functions

    Classes

    View source on GitHub
    attribute
    URL_BASE: str
    attribute
    type_to_loader_dict: dict
    function
    load_llm
    function
    load_llm_from_config
    deprecatedfunction
    load_chain_from_config
    deprecatedfunction
    load_chain
    class
    APIChain
    class
    Chain
    class
    HypotheticalDocumentEmbedder
    class
    RetrievalQAWithSourcesChain
    class
    VectorDBQAWithSourcesChain
    deprecatedclass
    MapReduceDocumentsChain
    deprecatedclass
    MapRerankDocumentsChain
    deprecatedclass
    RefineDocumentsChain
    deprecatedclass
    StuffDocumentsChain
    deprecatedclass
    LLMChain
    deprecatedclass
    LLMCheckerChain
    deprecatedclass
    LLMMathChain
    deprecatedclass
    QAWithSourcesChain
    deprecatedclass
    RetrievalQA
    deprecatedclass
    VectorDBQA

    Import error for load_llm.

    Import error for load_llm_from_config.

    Load chain from Config Dict.

    Unified method for loading a chain from LangChainHub or local fs.

    Raise an ImportError if APIChain is used without langchain_community.

    Abstract base class for creating structured sequences of calls to components.

    Chains should be used to encode a sequence of calls to components like models, document retrievers, other chains, etc., and provide a simple interface to this sequence.

    Generate hypothetical document for query, and then embed that.

    Based on https://arxiv.org/abs/2212.10496

    Question-answering with sources over an index.

    Question-answering with sources over a vector database.

    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.

    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.

    Combine documents by doing a first pass and then refining on more documents.

    This algorithm first calls initial_llm_chain on the first document, passing that first document in with the variable name document_variable_name, and produces a new variable with the variable name initial_response_name.

    Then, it loops over every remaining document. This is called the "refine" step. It calls refine_llm_chain, passing in that document with the variable name document_variable_name as well as the previous response with the variable name initial_response_name.

    Chain that combines documents by stuffing into context.

    This chain takes a list of documents and first combines them into a single string. It does this by formatting each document into a string with the document_prompt and then joining them together with document_separator. It then adds that new string to the inputs with the variable name set by document_variable_name. Those inputs are then passed to the llm_chain.

    Chain for question-answering with self-verification.

    Question answering with sources over documents.

    Chain for question-answering against a vector database.

    Chain to run queries against LLMs.

    This class is deprecated. See below for an example implementation using LangChain runnables:

    from langchain_core.output_parsers import StrOutputParser
    from langchain_core.prompts import PromptTemplate
    from langchain_openai import OpenAI
    
    prompt_template = "Tell me a {adjective} joke"
    prompt = PromptTemplate(input_variables=["adjective"], template=prompt_template)
    model = OpenAI()
    chain = prompt | model | StrOutputParser()
    
    chain.invoke("your adjective here")

    Chain that interprets a prompt and executes python code to do math.

    Note

    This class is deprecated. See below for a replacement implementation using LangGraph. The benefits of this implementation are:

    • Uses LLM tool calling features;
    • Support for both token-by-token and step-by-step streaming;
    • Support for checkpointing and memory of chat history;
    • Easier to modify or extend (e.g., with additional tools, structured responses, etc.)

    Install LangGraph with:

    pip install -U langgraph
    import math
    from typing import Annotated, Sequence
    
    from langchain_core.messages import BaseMessage
    from langchain_core.runnables import RunnableConfig
    from langchain_core.tools import tool
    from langchain_openai import ChatOpenAI
    from langgraph.graph import END, StateGraph
    from langgraph.graph.message import add_messages
    from langgraph.prebuilt.tool_node import ToolNode
    import numexpr
    from typing_extensions import TypedDict
    
    @tool
    def calculator(expression: str) -> str:
        """Calculate expression using Python's numexpr library.
    
        Expression should be a single line mathematical expression
        that solves the problem.

    Chain for question-answering against an index.

    This class is deprecated. See below for an example implementation using create_retrieval_chain:

    from langchain_classic.chains import create_retrieval_chain
    from langchain_classic.chains.combine_documents import (
        create_stuff_documents_chain,
    )
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    
    retriever = ...  # Your retriever
    model = ChatOpenAI()
    
    system_prompt = (
        "Use the given context to answer the question. "
        "If you don't know the answer, say you don't know. "
        "Use three sentence maximum and keep the answer concise. "
        "Context: {context}"
    )
    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", system_prompt),
            ("human", "{input}"),
        ]
    )
    question_answer_chain = create_stuff_documents_chain(model, prompt)
    chain = create_retrieval_chain(retriever, question_answer_chain)
    
    chain.invoke({"input": query})