Home / File/ initialize.py — langchain Source File

initialize.py — langchain Source File

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

File python AgentOrchestration ToolInterface 12 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  dc73acf6_5759_fd39_4791_4836f8ca0d6b["initialize.py"]
  69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56["contextlib"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> 69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  b19a8b7e_fbee_95b1_65b8_509a1ed3cad7["langchain_core._api"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> b19a8b7e_fbee_95b1_65b8_509a1ed3cad7
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> f3bc7443_c889_119d_0744_aacc3620d8d2
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> ba43b74d_3099_7e1c_aac3_cf594720469e
  43d88577_548b_2248_b01b_7987bae85dcc["langchain_core.tools"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> 43d88577_548b_2248_b01b_7987bae85dcc
  0569002b_723c_e521_6645_d02d74dd6913["langchain_classic._api.deprecation"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> 0569002b_723c_e521_6645_d02d74dd6913
  e160f068_75de_4342_6673_9969b919de85["langchain_classic.agents.agent"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> e160f068_75de_4342_6673_9969b919de85
  8bc2eee7_b040_0edd_0172_48295cb3fb89["langchain_classic.agents.agent_types"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> 8bc2eee7_b040_0edd_0172_48295cb3fb89
  a5261bfd_0055_d616_d3b8_2bcf536d766b["langchain_classic.agents.loading"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> a5261bfd_0055_d616_d3b8_2bcf536d766b
  e2852ebd_b14d_e81d_bcb1_80da79e9601e["langchain_classic.agents.types"]
  dc73acf6_5759_fd39_4791_4836f8ca0d6b --> e2852ebd_b14d_e81d_bcb1_80da79e9601e
  style dc73acf6_5759_fd39_4791_4836f8ca0d6b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Load agent."""

import contextlib
from collections.abc import Sequence
from typing import Any

from langchain_core._api import deprecated
from langchain_core.callbacks import BaseCallbackManager
from langchain_core.language_models import BaseLanguageModel
from langchain_core.tools import BaseTool

from langchain_classic._api.deprecation import AGENT_DEPRECATION_WARNING
from langchain_classic.agents.agent import AgentExecutor
from langchain_classic.agents.agent_types import AgentType
from langchain_classic.agents.loading import load_agent
from langchain_classic.agents.types import AGENT_TO_CLASS


@deprecated(
    "0.1.0",
    message=AGENT_DEPRECATION_WARNING,
    removal="1.0",
)
def initialize_agent(
    tools: Sequence[BaseTool],
    llm: BaseLanguageModel,
    agent: AgentType | None = None,
    callback_manager: BaseCallbackManager | None = None,
    agent_path: str | None = None,
    agent_kwargs: dict | None = None,
    *,
    tags: Sequence[str] | None = None,
    **kwargs: Any,
) -> AgentExecutor:
    """Load an agent executor given tools and LLM.

    !!! warning

        This function is no deprecated in favor of
        [`create_agent`][langchain.agents.create_agent] from the `langchain`
        package, which provides a more flexible agent factory with middleware
        support, structured output, and integration with LangGraph.

        For migration guidance, see
        [Migrating to langchain v1](https://docs.langchain.com/oss/python/migrate/langchain-v1)
        and
        [Migrating from AgentExecutor](https://python.langchain.com/docs/how_to/migrate_agent/).

    Args:
        tools: List of tools this agent has access to.
        llm: Language model to use as the agent.
        agent: Agent type to use. If `None` and agent_path is also None, will default
            to AgentType.ZERO_SHOT_REACT_DESCRIPTION.
        callback_manager: CallbackManager to use. Global callback manager is used if
            not provided.
        agent_path: Path to serialized agent to use. If `None` and agent is also None,
            will default to AgentType.ZERO_SHOT_REACT_DESCRIPTION.
        agent_kwargs: Additional keyword arguments to pass to the underlying agent.
        tags: Tags to apply to the traced runs.
        kwargs: Additional keyword arguments passed to the agent executor.

    Returns:
        An agent executor.

    Raises:
        ValueError: If both `agent` and `agent_path` are specified.
        ValueError: If `agent` is not a valid agent type.
        ValueError: If both `agent` and `agent_path` are None.
    """
    tags_ = list(tags) if tags else []
    if agent is None and agent_path is None:
        agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION
    if agent is not None and agent_path is not None:
        msg = (
            "Both `agent` and `agent_path` are specified, "
            "but at most only one should be."
        )
        raise ValueError(msg)
    if agent is not None:
        if agent not in AGENT_TO_CLASS:
            msg = (
                f"Got unknown agent type: {agent}. "
                f"Valid types are: {AGENT_TO_CLASS.keys()}."
            )
            raise ValueError(msg)
        tags_.append(agent.value if isinstance(agent, AgentType) else agent)
        agent_cls = AGENT_TO_CLASS[agent]
        agent_kwargs = agent_kwargs or {}
        agent_obj = agent_cls.from_llm_and_tools(
            llm,
            tools,
            callback_manager=callback_manager,
            **agent_kwargs,
        )
    elif agent_path is not None:
        agent_obj = load_agent(
            agent_path,
            llm=llm,
            tools=tools,
            callback_manager=callback_manager,
        )
        with contextlib.suppress(NotImplementedError):
            # TODO: Add tags from the serialized object directly.
            tags_.append(agent_obj._agent_type)  # noqa: SLF001
    else:
        msg = (
            "Somehow both `agent` and `agent_path` are None, this should never happen."
        )
        raise ValueError(msg)
    return AgentExecutor.from_agent_and_tools(
        agent=agent_obj,
        tools=tools,
        callback_manager=callback_manager,
        tags=tags_,
        **kwargs,
    )

Subdomains

Functions

Dependencies

  • collections.abc
  • contextlib
  • langchain_classic._api.deprecation
  • langchain_classic.agents.agent
  • langchain_classic.agents.agent_types
  • langchain_classic.agents.loading
  • langchain_classic.agents.types
  • langchain_core._api
  • langchain_core.callbacks
  • langchain_core.language_models
  • langchain_core.tools
  • typing

Frequently Asked Questions

What does initialize.py do?
initialize.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ToolInterface subdomain.
What functions are defined in initialize.py?
initialize.py defines 1 function(s): initialize_agent.
What does initialize.py depend on?
initialize.py imports 12 module(s): collections.abc, contextlib, langchain_classic._api.deprecation, langchain_classic.agents.agent, langchain_classic.agents.agent_types, langchain_classic.agents.loading, langchain_classic.agents.types, langchain_core._api, and 4 more.
Where is initialize.py in the architecture?
initialize.py is located at libs/langchain/langchain_classic/agents/initialize.py (domain: AgentOrchestration, subdomain: ToolInterface, directory: libs/langchain/langchain_classic/agents).

Analyze Your Own Codebase

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

Try Supermodel Free