Home / File/ base.py — langchain Source File

base.py — langchain Source File

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

File python AgentOrchestration ActionLogic 17 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  90c8922f_faca_da82_5890_e178007ba134["base.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  90c8922f_faca_da82_5890_e178007ba134 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  90c8922f_faca_da82_5890_e178007ba134 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  90c8922f_faca_da82_5890_e178007ba134 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  b19a8b7e_fbee_95b1_65b8_509a1ed3cad7["langchain_core._api"]
  90c8922f_faca_da82_5890_e178007ba134 --> b19a8b7e_fbee_95b1_65b8_509a1ed3cad7
  80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"]
  90c8922f_faca_da82_5890_e178007ba134 --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  90c8922f_faca_da82_5890_e178007ba134 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  90c8922f_faca_da82_5890_e178007ba134 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  90c8922f_faca_da82_5890_e178007ba134 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  90c8922f_faca_da82_5890_e178007ba134 --> d758344f_537f_649e_f467_b9d7442e86df
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  90c8922f_faca_da82_5890_e178007ba134 --> e6b4f61e_7b98_6666_3641_26b069517d4a
  e45722a2_0136_a972_1f58_7b5987500404["langchain_core.prompts.chat"]
  90c8922f_faca_da82_5890_e178007ba134 --> e45722a2_0136_a972_1f58_7b5987500404
  63bafb65_e3c1_8fca_d02a_332411ce701e["langchain_core.prompts.message"]
  90c8922f_faca_da82_5890_e178007ba134 --> 63bafb65_e3c1_8fca_d02a_332411ce701e
  43d88577_548b_2248_b01b_7987bae85dcc["langchain_core.tools"]
  90c8922f_faca_da82_5890_e178007ba134 --> 43d88577_548b_2248_b01b_7987bae85dcc
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  90c8922f_faca_da82_5890_e178007ba134 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  style 90c8922f_faca_da82_5890_e178007ba134 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Module implements an agent that uses OpenAI's APIs function enabled API."""

import json
from collections.abc import Sequence
from json import JSONDecodeError
from typing import Any

from langchain_core._api import deprecated
from langchain_core.agents import AgentAction, AgentActionMessageLog, AgentFinish
from langchain_core.callbacks import BaseCallbackManager, Callbacks
from langchain_core.exceptions import OutputParserException
from langchain_core.language_models import BaseLanguageModel
from langchain_core.messages import (
    AIMessage,
    BaseMessage,
    SystemMessage,
)
from langchain_core.prompts import BasePromptTemplate
from langchain_core.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    MessagesPlaceholder,
)
from langchain_core.prompts.message import BaseMessagePromptTemplate
from langchain_core.tools import BaseTool
from pydantic import model_validator
from typing_extensions import Self

from langchain_classic.agents import BaseMultiActionAgent
from langchain_classic.agents.format_scratchpad.openai_functions import (
    format_to_openai_function_messages,
)

# For backwards compatibility
_FunctionsAgentAction = AgentActionMessageLog


def _parse_ai_message(message: BaseMessage) -> list[AgentAction] | AgentFinish:
    """Parse an AI message."""
    if not isinstance(message, AIMessage):
        msg = f"Expected an AI message got {type(message)}"
        raise TypeError(msg)

    function_call = message.additional_kwargs.get("function_call", {})

    if function_call:
        try:
            arguments = json.loads(function_call["arguments"], strict=False)
        except JSONDecodeError as e:
            msg = (
                f"Could not parse tool input: {function_call} because "
                f"the `arguments` is not valid JSON."
            )
            raise OutputParserException(msg) from e

        try:
            tools = arguments["actions"]
        except (TypeError, KeyError) as e:
            msg = (
                f"Could not parse tool input: {function_call} because "
// ... (278 more lines)

Subdomains

Dependencies

  • collections.abc
  • json
  • langchain_classic.agents
  • langchain_classic.agents.format_scratchpad.openai_functions
  • langchain_core._api
  • langchain_core.agents
  • langchain_core.callbacks
  • langchain_core.exceptions
  • langchain_core.language_models
  • langchain_core.messages
  • langchain_core.prompts
  • langchain_core.prompts.chat
  • langchain_core.prompts.message
  • langchain_core.tools
  • 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, ActionLogic subdomain.
What functions are defined in base.py?
base.py defines 1 function(s): _parse_ai_message.
What does base.py depend on?
base.py imports 17 module(s): collections.abc, json, langchain_classic.agents, langchain_classic.agents.format_scratchpad.openai_functions, langchain_core._api, langchain_core.agents, langchain_core.callbacks, langchain_core.exceptions, and 9 more.
Where is base.py in the architecture?
base.py is located at libs/langchain/langchain_classic/agents/openai_functions_multi_agent/base.py (domain: AgentOrchestration, subdomain: ActionLogic, directory: libs/langchain/langchain_classic/agents/openai_functions_multi_agent).

Analyze Your Own Codebase

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

Try Supermodel Free