Home / Class/ OpenAIMultiFunctionsAgent Class — langchain Architecture

OpenAIMultiFunctionsAgent Class — langchain Architecture

Architecture documentation for the OpenAIMultiFunctionsAgent class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f["OpenAIMultiFunctionsAgent"]
  abf3bf27_12e4_7de1_8ae0_a05a97c163d3["BaseMultiActionAgent"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|extends| abf3bf27_12e4_7de1_8ae0_a05a97c163d3
  033fac42_6b10_d356_1953_4b3c355a3da0["base.py"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|defined in| 033fac42_6b10_d356_1953_4b3c355a3da0
  bc8159d9_bc74_fa6b_b5e5_b3409685d8a9["get_allowed_tools()"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|method| bc8159d9_bc74_fa6b_b5e5_b3409685d8a9
  d000def0_46e2_6f87_6b0d_194f128f4c54["_validate_prompt()"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|method| d000def0_46e2_6f87_6b0d_194f128f4c54
  abdb66df_275f_a49d_bd60_8bfb3f00eae8["input_keys()"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|method| abdb66df_275f_a49d_bd60_8bfb3f00eae8
  7413276a_45ac_0ff0_06d5_601120d1d3ad["functions()"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|method| 7413276a_45ac_0ff0_06d5_601120d1d3ad
  2d3c80b2_2ddf_7154_9d64_84b6fee1992d["plan()"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|method| 2d3c80b2_2ddf_7154_9d64_84b6fee1992d
  9792acac_fda4_7f57_ed96_109c8cb24f18["aplan()"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|method| 9792acac_fda4_7f57_ed96_109c8cb24f18
  15e14e95_cbcf_22f8_6cc5_e15ae3dbcff0["create_prompt()"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|method| 15e14e95_cbcf_22f8_6cc5_e15ae3dbcff0
  a48ce0cf_92fc_c84b_b28d_0ea9f75501f7["from_llm_and_tools()"]
  37cb2b60_9ae1_7f39_46a1_1f7d0156c75f -->|method| a48ce0cf_92fc_c84b_b28d_0ea9f75501f7

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/openai_functions_multi_agent/base.py lines 107–337

class OpenAIMultiFunctionsAgent(BaseMultiActionAgent):
    """Agent driven by OpenAIs function powered API.

    Args:
        llm: This should be an instance of ChatOpenAI, specifically a model
            that supports using `functions`.
        tools: The tools this agent has access to.
        prompt: The prompt for this agent, should support agent_scratchpad as one
            of the variables. For an easy way to construct this prompt, use
            `OpenAIMultiFunctionsAgent.create_prompt(...)`
    """

    llm: BaseLanguageModel
    tools: Sequence[BaseTool]
    prompt: BasePromptTemplate

    def get_allowed_tools(self) -> list[str]:
        """Get allowed tools."""
        return [t.name for t in self.tools]

    @model_validator(mode="after")
    def _validate_prompt(self) -> Self:
        prompt: BasePromptTemplate = self.prompt
        if "agent_scratchpad" not in prompt.input_variables:
            msg = (
                "`agent_scratchpad` should be one of the variables in the prompt, "
                f"got {prompt.input_variables}"
            )
            raise ValueError(msg)
        return self

    @property
    def input_keys(self) -> list[str]:
        """Get input keys. Input refers to user input here."""
        return ["input"]

    @property
    def functions(self) -> list[dict]:
        """Get the functions for the agent."""
        enum_vals = [t.name for t in self.tools]
        tool_selection = {
            # OpenAI functions returns a single tool invocation
            # Here we force the single tool invocation it returns to
            # itself be a list of tool invocations. We do this by constructing
            # a new tool that has one argument which is a list of tools
            # to use.
            "name": "tool_selection",
            "description": "A list of actions to take.",
            "parameters": {
                "title": "tool_selection",
                "description": "A list of actions to take.",
                "type": "object",
                "properties": {
                    "actions": {
                        "title": "actions",
                        "type": "array",
                        "items": {
                            # This is a custom item which bundles the action_name
                            # and the action. We do this because some actions
                            # could have the same schema, and without this there
                            # is no way to differentiate them.
                            "title": "tool_call",
                            "type": "object",
                            "properties": {
                                # This is the name of the action to take
                                "action_name": {
                                    "title": "action_name",
                                    "enum": enum_vals,
                                    "type": "string",
                                    "description": (
                                        "Name of the action to take. The name "
                                        "provided here should match up with the "
                                        "parameters for the action below."
                                    ),
                                },
                                # This is the action to take.
                                "action": {
                                    "title": "Action",
                                    "anyOf": [
                                        {
                                            "title": t.name,

Frequently Asked Questions

What is the OpenAIMultiFunctionsAgent class?
OpenAIMultiFunctionsAgent is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/openai_functions_multi_agent/base.py.
Where is OpenAIMultiFunctionsAgent defined?
OpenAIMultiFunctionsAgent is defined in libs/langchain/langchain_classic/agents/openai_functions_multi_agent/base.py at line 107.
What does OpenAIMultiFunctionsAgent extend?
OpenAIMultiFunctionsAgent extends BaseMultiActionAgent.

Analyze Your Own Codebase

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

Try Supermodel Free