Home / File/ agent.py — langchain Source File

agent.py — langchain Source File

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

File python AgentOrchestration ActionLogic 9 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  2802721f_971c_bf69_b405_2e0a4f956ecb["agent.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> ba43b74d_3099_7e1c_aac3_cf594720469e
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> e6b4f61e_7b98_6666_3641_26b069517d4a
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  43d88577_548b_2248_b01b_7987bae85dcc["langchain_core.tools"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> 43d88577_548b_2248_b01b_7987bae85dcc
  ca5ba58c_877f_7975_369b_ca5e40124948["langchain_core.tools.render"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> ca5ba58c_877f_7975_369b_ca5e40124948
  37814a67_ff56_3f91_8aa4_794e5ef7a906["langchain_classic.agents"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> 37814a67_ff56_3f91_8aa4_794e5ef7a906
  86bb3cc5_a04d_0230_26de_553f2b190764["langchain_classic.agents.format_scratchpad"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> 86bb3cc5_a04d_0230_26de_553f2b190764
  9c4f5659_9e69_a51c_6f55_4eed4ef335f6["langchain_classic.agents.output_parsers"]
  2802721f_971c_bf69_b405_2e0a4f956ecb --> 9c4f5659_9e69_a51c_6f55_4eed4ef335f6
  style 2802721f_971c_bf69_b405_2e0a4f956ecb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from collections.abc import Sequence

from langchain_core.language_models import BaseLanguageModel
from langchain_core.prompts import BasePromptTemplate
from langchain_core.runnables import Runnable, RunnablePassthrough
from langchain_core.tools import BaseTool
from langchain_core.tools.render import ToolsRenderer, render_text_description

from langchain_classic.agents import AgentOutputParser
from langchain_classic.agents.format_scratchpad import format_log_to_str
from langchain_classic.agents.output_parsers import ReActSingleInputOutputParser


def create_react_agent(
    llm: BaseLanguageModel,
    tools: Sequence[BaseTool],
    prompt: BasePromptTemplate,
    output_parser: AgentOutputParser | None = None,
    tools_renderer: ToolsRenderer = render_text_description,
    *,
    stop_sequence: bool | list[str] = True,
) -> Runnable:
    r"""Create an agent that uses ReAct prompting.

    Based on paper "ReAct: Synergizing Reasoning and Acting in Language Models"
    (https://arxiv.org/abs/2210.03629)

    !!! warning

        This implementation is based on the foundational ReAct paper but is older and
        not well-suited for production applications.

        For a more robust and feature-rich implementation, we recommend using the
        `create_agent` function from the `langchain` library.

        See the
        [reference doc](https://reference.langchain.com/python/langchain/agents/)
        for more information.

    Args:
        llm: LLM to use as the agent.
        tools: Tools this agent has access to.
        prompt: The prompt to use. See Prompt section below for more.
        output_parser: AgentOutputParser for parse the LLM output.
        tools_renderer: This controls how the tools are converted into a string and
            then passed into the LLM.
        stop_sequence: bool or list of str.
            If `True`, adds a stop token of "Observation:" to avoid hallucinates.
            If `False`, does not add a stop token.
            If a list of str, uses the provided list as the stop tokens.

            You may to set this to False if the LLM you are using
            does not support stop sequences.

    Returns:
        A Runnable sequence representing an agent. It takes as input all the same input
        variables as the prompt passed in does. It returns as output either an
        AgentAction or AgentFinish.
// ... (91 more lines)

Subdomains

Dependencies

  • collections.abc
  • langchain_classic.agents
  • langchain_classic.agents.format_scratchpad
  • langchain_classic.agents.output_parsers
  • langchain_core.language_models
  • langchain_core.prompts
  • langchain_core.runnables
  • langchain_core.tools
  • langchain_core.tools.render

Frequently Asked Questions

What does agent.py do?
agent.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ActionLogic subdomain.
What functions are defined in agent.py?
agent.py defines 1 function(s): create_react_agent.
What does agent.py depend on?
agent.py imports 9 module(s): collections.abc, langchain_classic.agents, langchain_classic.agents.format_scratchpad, langchain_classic.agents.output_parsers, langchain_core.language_models, langchain_core.prompts, langchain_core.runnables, langchain_core.tools, and 1 more.
Where is agent.py in the architecture?
agent.py is located at libs/langchain/langchain_classic/agents/react/agent.py (domain: AgentOrchestration, subdomain: ActionLogic, directory: libs/langchain/langchain_classic/agents/react).

Analyze Your Own Codebase

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

Try Supermodel Free