Home / Class/ OpenAIFunctionsAgent Class — langchain Architecture

OpenAIFunctionsAgent Class — langchain Architecture

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

Entity Profile

Dependency Diagram

graph TD
  93ed7b23_f0e4_5732_b3ed_dccef0049b97["OpenAIFunctionsAgent"]
  3b9abff9_c907_d2e1_41bb_cbceeb6d27f4["BaseSingleActionAgent"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|extends| 3b9abff9_c907_d2e1_41bb_cbceeb6d27f4
  21559aaf_24e1_3178_8193_1719e6ef11ba["AgentFinish"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|extends| 21559aaf_24e1_3178_8193_1719e6ef11ba
  8878ac65_f307_870e_1a83_8610e50355b5["base.py"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|defined in| 8878ac65_f307_870e_1a83_8610e50355b5
  bc453dc4_230c_f96f_9450_3e1c171b4f1e["get_allowed_tools()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| bc453dc4_230c_f96f_9450_3e1c171b4f1e
  0558a8d1_8354_9c26_3820_59b579b5eb8d["validate_prompt()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| 0558a8d1_8354_9c26_3820_59b579b5eb8d
  f24cb777_0fbf_1aab_7634_786e861f6b0c["input_keys()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| f24cb777_0fbf_1aab_7634_786e861f6b0c
  acf238fd_af45_ea9f_4159_62787bcf6663["functions()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| acf238fd_af45_ea9f_4159_62787bcf6663
  1beb0b73_e6d9_844c_11db_60fdd5087d3d["plan()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| 1beb0b73_e6d9_844c_11db_60fdd5087d3d
  e9a6d1b8_cce5_f163_c1a8_6567cac2f0d3["aplan()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| e9a6d1b8_cce5_f163_c1a8_6567cac2f0d3
  083ac900_3458_2f92_cdad_5b192c460d85["return_stopped_response()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| 083ac900_3458_2f92_cdad_5b192c460d85
  428fd0bf_f575_d9ce_a33a_03b23d9eb0f0["create_prompt()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| 428fd0bf_f575_d9ce_a33a_03b23d9eb0f0
  4e43f07b_4dcb_07be_cdea_d96f79d80f79["from_llm_and_tools()"]
  93ed7b23_f0e4_5732_b3ed_dccef0049b97 -->|method| 4e43f07b_4dcb_07be_cdea_d96f79d80f79

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/openai_functions_agent/base.py lines 39–284

class OpenAIFunctionsAgent(BaseSingleActionAgent):
    """An 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
            `OpenAIFunctionsAgent.create_prompt(...)`
        output_parser: The output parser for this agent. Should be an instance of
            `OpenAIFunctionsAgentOutputParser`.
    """

    llm: BaseLanguageModel
    tools: Sequence[BaseTool]
    prompt: BasePromptTemplate
    output_parser: type[OpenAIFunctionsAgentOutputParser] = (
        OpenAIFunctionsAgentOutputParser
    )

    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:
        """Validate prompt.

        Args:
            values: Values to validate.

        Returns:
            Validated values.

        Raises:
            ValueError: If `agent_scratchpad` is not in the prompt.
        """
        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 functions."""
        return [dict(convert_to_openai_function(t)) for t in self.tools]

    def plan(
        self,
        intermediate_steps: list[tuple[AgentAction, str]],
        callbacks: Callbacks = None,
        with_functions: bool = True,  # noqa: FBT001,FBT002
        **kwargs: Any,
    ) -> AgentAction | AgentFinish:
        """Given input, decided what to do.

        Args:
            intermediate_steps: Steps the LLM has taken to date,
                along with observations.
            callbacks: Callbacks to use.
            with_functions: Whether to use functions.
            **kwargs: User inputs.

        Returns:
            Action specifying what tool to use.
            If the agent is finished, returns an `AgentFinish`.
            If the agent is not finished, returns an `AgentAction`.
        """
        agent_scratchpad = format_to_openai_function_messages(intermediate_steps)
        selected_inputs = {
            k: kwargs[k] for k in self.prompt.input_variables if k != "agent_scratchpad"

Frequently Asked Questions

What is the OpenAIFunctionsAgent class?
OpenAIFunctionsAgent is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/openai_functions_agent/base.py.
Where is OpenAIFunctionsAgent defined?
OpenAIFunctionsAgent is defined in libs/langchain/langchain_classic/agents/openai_functions_agent/base.py at line 39.
What does OpenAIFunctionsAgent extend?
OpenAIFunctionsAgent extends BaseSingleActionAgent, AgentFinish.

Analyze Your Own Codebase

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

Try Supermodel Free