Home / File/ test_openai_functions_multi.py — langchain Source File

test_openai_functions_multi.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  40e0e915_c4ea_f522_8b88_2c93fbe7a809["test_openai_functions_multi.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  40e0e915_c4ea_f522_8b88_2c93fbe7a809 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  40e0e915_c4ea_f522_8b88_2c93fbe7a809 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"]
  40e0e915_c4ea_f522_8b88_2c93fbe7a809 --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  40e0e915_c4ea_f522_8b88_2c93fbe7a809 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  40e0e915_c4ea_f522_8b88_2c93fbe7a809 --> d758344f_537f_649e_f467_b9d7442e86df
  377b0b7b_c641_f5a5_d861_f90194927e87["langchain_classic.agents.openai_functions_multi_agent.base"]
  40e0e915_c4ea_f522_8b88_2c93fbe7a809 --> 377b0b7b_c641_f5a5_d861_f90194927e87
  style 40e0e915_c4ea_f522_8b88_2c93fbe7a809 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import json

import pytest
from langchain_core.agents import AgentFinish
from langchain_core.exceptions import OutputParserException
from langchain_core.messages import AIMessage, SystemMessage

from langchain_classic.agents.openai_functions_multi_agent.base import (
    _FunctionsAgentAction,
    _parse_ai_message,
)


# Test: _parse_ai_message() function.
class TestParseAIMessage:
    # Test: Pass Non-AIMessage.
    def test_not_an_ai(self) -> None:
        err = f"Expected an AI message got {SystemMessage!s}"
        with pytest.raises(TypeError, match=err):
            _parse_ai_message(SystemMessage(content="x"))

    # Test: Model response (not a function call).
    def test_model_response(self) -> None:
        msg = AIMessage(content="Model response.")
        result = _parse_ai_message(msg)

        assert isinstance(result, AgentFinish)
        assert result.return_values == {"output": "Model response."}
        assert result.log == "Model response."

    # Test: Model response with a function call.
    def test_func_call(self) -> None:
        act = json.dumps([{"action_name": "foo", "action": {"param": 42}}])

        msg = AIMessage(
            content="LLM thoughts.",
            additional_kwargs={
                "function_call": {"name": "foo", "arguments": f'{{"actions": {act}}}'},
            },
        )
        result = _parse_ai_message(msg)

        assert isinstance(result, list)
        assert len(result) == 1

        action = result[0]
        assert isinstance(action, _FunctionsAgentAction)
        assert action.tool == "foo"
        assert action.tool_input == {"param": 42}
        assert action.log == (
            "\nInvoking: `foo` with `{'param': 42}`\nresponded: LLM thoughts.\n\n"
        )
        assert action.message_log == [msg]

    # Test: Model response with a function call (old style tools).
    def test_func_call_oldstyle(self) -> None:
        act = json.dumps([{"action_name": "foo", "action": {"__arg1": "42"}}])

        msg = AIMessage(
            content="LLM thoughts.",
            additional_kwargs={
                "function_call": {"name": "foo", "arguments": f'{{"actions": {act}}}'},
            },
        )
        result = _parse_ai_message(msg)

        assert isinstance(result, list)
        assert len(result) == 1

        action = result[0]
        assert isinstance(action, _FunctionsAgentAction)
        assert action.tool == "foo"
        assert action.tool_input == "42"
        assert action.log == (
            "\nInvoking: `foo` with `42`\nresponded: LLM thoughts.\n\n"
        )
        assert action.message_log == [msg]

    # Test: Invalid function call args.
    def test_func_call_invalid(self) -> None:
        msg = AIMessage(
            content="LLM thoughts.",
            additional_kwargs={"function_call": {"name": "foo", "arguments": "{42]"}},
        )

        err = (
            "Could not parse tool input: {'name': 'foo', 'arguments': '{42]'} "
            "because the `arguments` is not valid JSON."
        )
        with pytest.raises(OutputParserException, match=err):
            _parse_ai_message(msg)

Subdomains

Dependencies

  • json
  • langchain_classic.agents.openai_functions_multi_agent.base
  • langchain_core.agents
  • langchain_core.exceptions
  • langchain_core.messages
  • pytest

Frequently Asked Questions

What does test_openai_functions_multi.py do?
test_openai_functions_multi.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What does test_openai_functions_multi.py depend on?
test_openai_functions_multi.py imports 6 module(s): json, langchain_classic.agents.openai_functions_multi_agent.base, langchain_core.agents, langchain_core.exceptions, langchain_core.messages, pytest.
Where is test_openai_functions_multi.py in the architecture?
test_openai_functions_multi.py is located at libs/langchain/tests/unit_tests/agents/test_openai_functions_multi.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/langchain/tests/unit_tests/agents).

Analyze Your Own Codebase

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

Try Supermodel Free