Home / File/ openai_tools.py — langchain Source File

openai_tools.py — langchain Source File

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

File python AgentOrchestration ClassicChains 6 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  01a8f147_c3fb_2633_e69b_be569e7ac472["openai_tools.py"]
  59e0d3b0_0f8e_4b79_d442_e9b4821561c7["langchain_core.agents"]
  01a8f147_c3fb_2633_e69b_be569e7ac472 --> 59e0d3b0_0f8e_4b79_d442_e9b4821561c7
  9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"]
  01a8f147_c3fb_2633_e69b_be569e7ac472 --> 9444498b_8066_55c7_b3a2_1d90c4162a32
  4382dc25_6fba_324a_49e2_e9742d579385["langchain_core.outputs"]
  01a8f147_c3fb_2633_e69b_be569e7ac472 --> 4382dc25_6fba_324a_49e2_e9742d579385
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  01a8f147_c3fb_2633_e69b_be569e7ac472 --> f85fae70_1011_eaec_151c_4083140ae9e5
  496466eb_d5c8_fece_1b1f_31541c641cdd["langchain_classic.agents.agent"]
  01a8f147_c3fb_2633_e69b_be569e7ac472 --> 496466eb_d5c8_fece_1b1f_31541c641cdd
  530bd36e_f6a3_152e_39f8_85482aae96f1["langchain_classic.agents.output_parsers.tools"]
  01a8f147_c3fb_2633_e69b_be569e7ac472 --> 530bd36e_f6a3_152e_39f8_85482aae96f1
  style 01a8f147_c3fb_2633_e69b_be569e7ac472 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.messages import BaseMessage
from langchain_core.outputs import ChatGeneration, Generation
from typing_extensions import override

from langchain_classic.agents.agent import MultiActionAgentOutputParser
from langchain_classic.agents.output_parsers.tools import (
    ToolAgentAction,
    parse_ai_message_to_tool_action,
)

OpenAIToolAgentAction = ToolAgentAction


def parse_ai_message_to_openai_tool_action(
    message: BaseMessage,
) -> list[AgentAction] | AgentFinish:
    """Parse an AI message potentially containing tool_calls."""
    tool_actions = parse_ai_message_to_tool_action(message)
    if isinstance(tool_actions, AgentFinish):
        return tool_actions
    final_actions: list[AgentAction] = []
    for action in tool_actions:
        if isinstance(action, ToolAgentAction):
            final_actions.append(
                OpenAIToolAgentAction(
                    tool=action.tool,
                    tool_input=action.tool_input,
                    log=action.log,
                    message_log=action.message_log,
                    tool_call_id=action.tool_call_id,
                ),
            )
        else:
            final_actions.append(action)
    return final_actions


class OpenAIToolsAgentOutputParser(MultiActionAgentOutputParser):
    """Parses a message into agent actions/finish.

    Is meant to be used with OpenAI models, as it relies on the specific
    tool_calls parameter from OpenAI to convey what tools to use.

    If a tool_calls parameter is passed, then that is used to get
    the tool names and tool inputs.

    If one is not passed, then the AIMessage is assumed to be the final output.
    """

    @property
    def _type(self) -> str:
        return "openai-tools-agent-output-parser"

    @override
    def parse_result(
        self,
        result: list[Generation],
        *,
        partial: bool = False,
    ) -> list[AgentAction] | AgentFinish:
        if not isinstance(result[0], ChatGeneration):
            msg = "This output parser only works on ChatGeneration output"
            raise ValueError(msg)  # noqa: TRY004
        message = result[0].message
        return parse_ai_message_to_openai_tool_action(message)

    @override
    def parse(self, text: str) -> list[AgentAction] | AgentFinish:
        msg = "Can only parse messages"
        raise ValueError(msg)

Subdomains

Dependencies

  • langchain_classic.agents.agent
  • langchain_classic.agents.output_parsers.tools
  • langchain_core.agents
  • langchain_core.messages
  • langchain_core.outputs
  • typing_extensions

Frequently Asked Questions

What does openai_tools.py do?
openai_tools.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ClassicChains subdomain.
What functions are defined in openai_tools.py?
openai_tools.py defines 1 function(s): parse_ai_message_to_openai_tool_action.
What does openai_tools.py depend on?
openai_tools.py imports 6 module(s): langchain_classic.agents.agent, langchain_classic.agents.output_parsers.tools, langchain_core.agents, langchain_core.messages, langchain_core.outputs, typing_extensions.
Where is openai_tools.py in the architecture?
openai_tools.py is located at libs/langchain/langchain_classic/agents/output_parsers/openai_tools.py (domain: AgentOrchestration, subdomain: ClassicChains, directory: libs/langchain/langchain_classic/agents/output_parsers).

Analyze Your Own Codebase

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

Try Supermodel Free