Home / File/ test_openai_tools.py — langchain Source File

test_openai_tools.py — langchain Source File

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

File python LangChainCore MessageInterface 3 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  e1f67a20_c53c_0c56_caf3_8aba6704bc4c["test_openai_tools.py"]
  9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"]
  e1f67a20_c53c_0c56_caf3_8aba6704bc4c --> 9444498b_8066_55c7_b3a2_1d90c4162a32
  1b584883_70bf_28d9_739f_aaff0ff692d7["langchain_classic.agents.format_scratchpad.openai_tools"]
  e1f67a20_c53c_0c56_caf3_8aba6704bc4c --> 1b584883_70bf_28d9_739f_aaff0ff692d7
  cbe72457_20d2_4fbf_73af_b810c87ce51d["langchain_classic.agents.output_parsers.openai_tools"]
  e1f67a20_c53c_0c56_caf3_8aba6704bc4c --> cbe72457_20d2_4fbf_73af_b810c87ce51d
  style e1f67a20_c53c_0c56_caf3_8aba6704bc4c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from langchain_core.messages import AIMessage, ToolCall, ToolMessage

from langchain_classic.agents.format_scratchpad.openai_tools import (
    format_to_openai_tool_messages,
)
from langchain_classic.agents.output_parsers.openai_tools import (
    parse_ai_message_to_openai_tool_action,
)


def test_calls_convert_agent_action_to_messages() -> None:
    additional_kwargs1 = {
        "tool_calls": [
            {
                "id": "call_abcd12345",
                "function": {"arguments": '{"a": 3, "b": 5}', "name": "add"},
                "type": "function",
            },
        ],
    }
    message1 = AIMessage(content="", additional_kwargs=additional_kwargs1)

    actions1 = parse_ai_message_to_openai_tool_action(message1)
    additional_kwargs2 = {
        "tool_calls": [
            {
                "id": "call_abcd54321",
                "function": {"arguments": '{"a": 3, "b": 5}', "name": "subtract"},
                "type": "function",
            },
        ],
    }
    message2 = AIMessage(content="", additional_kwargs=additional_kwargs2)
    actions2 = parse_ai_message_to_openai_tool_action(message2)

    additional_kwargs3 = {
        "tool_calls": [
            {
                "id": "call_abcd67890",
                "function": {"arguments": '{"a": 3, "b": 5}', "name": "multiply"},
                "type": "function",
            },
            {
                "id": "call_abcd09876",
                "function": {"arguments": '{"a": 3, "b": 5}', "name": "divide"},
                "type": "function",
            },
        ],
    }
    message3 = AIMessage(content="", additional_kwargs=additional_kwargs3)
    actions3 = parse_ai_message_to_openai_tool_action(message3)

    message4 = AIMessage(
        content="",
        tool_calls=[
            ToolCall(
                name="exponentiate",
                args={"a": 3, "b": 5},
                id="call_abc02468",
                type="tool_call",
            ),
        ],
    )
    actions4 = parse_ai_message_to_openai_tool_action(message4)

    # for mypy
    assert isinstance(actions1, list)
    assert isinstance(actions2, list)
    assert isinstance(actions3, list)
    assert isinstance(actions4, list)

    intermediate_steps = [
        (actions1[0], "observation1"),
        (actions2[0], "observation2"),
        (actions3[0], "observation3"),
        (actions3[1], "observation4"),
        (actions4[0], "observation4"),
    ]
    expected_messages = [
        message1,
        ToolMessage(
            tool_call_id="call_abcd12345",
            content="observation1",
            additional_kwargs={"name": "add"},
        ),
        message2,
        ToolMessage(
            tool_call_id="call_abcd54321",
            content="observation2",
            additional_kwargs={"name": "subtract"},
        ),
        message3,
        ToolMessage(
            tool_call_id="call_abcd67890",
            content="observation3",
            additional_kwargs={"name": "multiply"},
        ),
        ToolMessage(
            tool_call_id="call_abcd09876",
            content="observation4",
            additional_kwargs={"name": "divide"},
        ),
        message4,
        ToolMessage(
            tool_call_id="call_abc02468",
            content="observation4",
            additional_kwargs={"name": "exponentiate"},
        ),
    ]
    output = format_to_openai_tool_messages(intermediate_steps)
    assert output == expected_messages


def test_handles_empty_input_list() -> None:
    output = format_to_openai_tool_messages([])
    assert output == []

Domain

Subdomains

Dependencies

  • langchain_classic.agents.format_scratchpad.openai_tools
  • langchain_classic.agents.output_parsers.openai_tools
  • langchain_core.messages

Frequently Asked Questions

What does test_openai_tools.py do?
test_openai_tools.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, MessageInterface subdomain.
What functions are defined in test_openai_tools.py?
test_openai_tools.py defines 2 function(s): test_calls_convert_agent_action_to_messages, test_handles_empty_input_list.
What does test_openai_tools.py depend on?
test_openai_tools.py imports 3 module(s): langchain_classic.agents.format_scratchpad.openai_tools, langchain_classic.agents.output_parsers.openai_tools, langchain_core.messages.
Where is test_openai_tools.py in the architecture?
test_openai_tools.py is located at libs/langchain/tests/unit_tests/agents/format_scratchpad/test_openai_tools.py (domain: LangChainCore, subdomain: MessageInterface, directory: libs/langchain/tests/unit_tests/agents/format_scratchpad).

Analyze Your Own Codebase

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

Try Supermodel Free