Home / File/ test_openai_functions.py — langchain Source File

test_openai_functions.py — langchain Source File

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

File python CoreAbstractions Serialization 9 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc["test_openai_functions.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  e8ec017e_6c91_4b34_675f_2a96c5aa9be6["langchain_core.callbacks.manager"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> e8ec017e_6c91_4b34_675f_2a96c5aa9be6
  2312f229_c199_ac88_c29f_62e2a2958404["langchain_core.language_models.chat_models"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> 2312f229_c199_ac88_c29f_62e2a2958404
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> d758344f_537f_649e_f467_b9d7442e86df
  ac2a9b92_4484_491e_1b48_ec85e71e1d58["langchain_core.outputs"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> ac2a9b92_4484_491e_1b48_ec85e71e1d58
  2e073793_7bdb_2272_3b6f_82c878fdc38c["pytest_mock"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> 2e073793_7bdb_2272_3b6f_82c878fdc38c
  66d6194f_d8c1_55b6_f522_311fdad57877["syrupy.assertion"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> 66d6194f_d8c1_55b6_f522_311fdad57877
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> 91721f45_4909_e489_8c1f_084f8bd87145
  bb152c8d_2204_0bb4_fdce_5a7ec9082e9b["langchain_classic.runnables.openai_functions"]
  b216c4d2_77b4_52a4_04ca_4f6c3bd489dc --> bb152c8d_2204_0bb4_fdce_5a7ec9082e9b
  style b216c4d2_77b4_52a4_04ca_4f6c3bd489dc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Any

from langchain_core.callbacks.manager import CallbackManagerForLLMRun
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import AIMessage, BaseMessage
from langchain_core.outputs import ChatGeneration, ChatResult
from pytest_mock import MockerFixture
from syrupy.assertion import SnapshotAssertion
from typing_extensions import override

from langchain_classic.runnables.openai_functions import OpenAIFunctionsRouter


class FakeChatOpenAI(BaseChatModel):
    @property
    def _llm_type(self) -> str:
        return "fake-openai-chat-model"

    @override
    def _generate(
        self,
        messages: list[BaseMessage],
        stop: list[str] | None = None,
        run_manager: CallbackManagerForLLMRun | None = None,
        **kwargs: Any,
    ) -> ChatResult:
        return ChatResult(
            generations=[
                ChatGeneration(
                    message=AIMessage(
                        content="",
                        additional_kwargs={
                            "function_call": {
                                "name": "accept",
                                "arguments": '{\n  "draft": "turtles"\n}',
                            },
                        },
                    ),
                ),
            ],
        )


def test_openai_functions_router(
    snapshot: SnapshotAssertion,
    mocker: MockerFixture,
) -> None:
    revise = mocker.Mock(
        side_effect=lambda kw: f"Revised draft: no more {kw['notes']}!",
    )
    accept = mocker.Mock(side_effect=lambda kw: f"Accepted draft: {kw['draft']}!")

    router = OpenAIFunctionsRouter(
        {
            "revise": revise,
            "accept": accept,
        },
        functions=[
            {
                "name": "revise",
                "description": "Sends the draft for revision.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "notes": {
                            "type": "string",
                            "description": "The editor's notes to guide the revision.",
                        },
                    },
                },
            },
            {
                "name": "accept",
                "description": "Accepts the draft.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "draft": {
                            "type": "string",
                            "description": "The draft to accept.",
                        },
                    },
                },
            },
        ],
    )

    model = FakeChatOpenAI()

    chain = model.bind(functions=router.functions) | router

    assert router.functions == snapshot

    assert chain.invoke("Something about turtles?") == "Accepted draft: turtles!"

    revise.assert_not_called()
    accept.assert_called_once_with({"draft": "turtles"})

Subdomains

Classes

Dependencies

  • langchain_classic.runnables.openai_functions
  • langchain_core.callbacks.manager
  • langchain_core.language_models.chat_models
  • langchain_core.messages
  • langchain_core.outputs
  • pytest_mock
  • syrupy.assertion
  • typing
  • typing_extensions

Frequently Asked Questions

What does test_openai_functions.py do?
test_openai_functions.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in test_openai_functions.py?
test_openai_functions.py defines 1 function(s): test_openai_functions_router.
What does test_openai_functions.py depend on?
test_openai_functions.py imports 9 module(s): langchain_classic.runnables.openai_functions, langchain_core.callbacks.manager, langchain_core.language_models.chat_models, langchain_core.messages, langchain_core.outputs, pytest_mock, syrupy.assertion, typing, and 1 more.
Where is test_openai_functions.py in the architecture?
test_openai_functions.py is located at libs/langchain/tests/unit_tests/runnables/test_openai_functions.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/langchain/tests/unit_tests/runnables).

Analyze Your Own Codebase

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

Try Supermodel Free