Home / File/ test_file.py — langchain Source File

test_file.py — langchain Source File

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

File python CoreAbstractions MessageSchema 6 imports 2 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  c0189376_53db_a7c9_4455_ed2f0371fc86["test_file.py"]
  b6ee5de5_719a_eeb5_1e11_e9c63bc22ef8["pathlib"]
  c0189376_53db_a7c9_4455_ed2f0371fc86 --> b6ee5de5_719a_eeb5_1e11_e9c63bc22ef8
  67ec3255_645e_8b6e_1eff_1eb3c648ed95["re"]
  c0189376_53db_a7c9_4455_ed2f0371fc86 --> 67ec3255_645e_8b6e_1eff_1eb3c648ed95
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  c0189376_53db_a7c9_4455_ed2f0371fc86 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  c0189376_53db_a7c9_4455_ed2f0371fc86 --> 91721f45_4909_e489_8c1f_084f8bd87145
  ceb2805d_366a_e6bf_6780_d87bd45cd413["langchain_classic.callbacks"]
  c0189376_53db_a7c9_4455_ed2f0371fc86 --> ceb2805d_366a_e6bf_6780_d87bd45cd413
  01158a5b_b299_f45d_92e9_2a7433a1a91a["langchain_classic.chains.base"]
  c0189376_53db_a7c9_4455_ed2f0371fc86 --> 01158a5b_b299_f45d_92e9_2a7433a1a91a
  style c0189376_53db_a7c9_4455_ed2f0371fc86 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import pathlib
import re

from langchain_core.callbacks import CallbackManagerForChainRun
from typing_extensions import override

from langchain_classic.callbacks import FileCallbackHandler
from langchain_classic.chains.base import Chain


class FakeChain(Chain):
    """Fake chain class for testing purposes."""

    be_correct: bool = True
    the_input_keys: list[str] = ["foo"]
    the_output_keys: list[str] = ["bar"]

    @property
    def input_keys(self) -> list[str]:
        """Input keys."""
        return self.the_input_keys

    @property
    def output_keys(self) -> list[str]:
        """Output key of bar."""
        return self.the_output_keys

    @override
    def _call(
        self,
        inputs: dict[str, str],
        run_manager: CallbackManagerForChainRun | None = None,
    ) -> dict[str, str]:
        return {"bar": "bar"}


def strip_ansi(text: str) -> str:
    """Removes ANSI escape sequences from a string.

    Args:
        text: The string potentially containing ANSI codes.
    """
    ansi_escape = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
    return ansi_escape.sub("", text)


def test_filecallback(tmp_path: pathlib.Path) -> None:
    """Test the file callback handler."""
    log1 = tmp_path / "output.log"
    handler = FileCallbackHandler(str(log1))
    chain_test = FakeChain(callbacks=[handler])
    chain_test.invoke({"foo": "bar"})
    handler.close()
    # Assert the output is as expected
    assert "Entering new FakeChain chain" in strip_ansi(log1.read_text())

    # Test using a callback manager
    log2 = tmp_path / "output2.log"

    with FileCallbackHandler(str(log2)) as handler_cm:
        chain_test = FakeChain(callbacks=[handler_cm])
        chain_test.invoke({"foo": "bar"})

    assert "Entering new FakeChain chain" in strip_ansi(log2.read_text())

    # Test passing via invoke callbacks
    log3 = tmp_path / "output3.log"

    with FileCallbackHandler(str(log3)) as handler_cm:
        chain_test.invoke({"foo": "bar"}, {"callbacks": [handler_cm]})
    assert "Entering new FakeChain chain" in strip_ansi(log3.read_text())

Subdomains

Classes

Dependencies

  • langchain_classic.callbacks
  • langchain_classic.chains.base
  • langchain_core.callbacks
  • pathlib
  • re
  • typing_extensions

Frequently Asked Questions

What does test_file.py do?
test_file.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in test_file.py?
test_file.py defines 2 function(s): strip_ansi, test_filecallback.
What does test_file.py depend on?
test_file.py imports 6 module(s): langchain_classic.callbacks, langchain_classic.chains.base, langchain_core.callbacks, pathlib, re, typing_extensions.
Where is test_file.py in the architecture?
test_file.py is located at libs/langchain/tests/unit_tests/callbacks/test_file.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/langchain/tests/unit_tests/callbacks).

Analyze Your Own Codebase

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

Try Supermodel Free