Home / File/ transform.py — langchain Source File

transform.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  c629d49a_b244_753d_acc3_91ded440fff9["transform.py"]
  c990f2d7_9509_7cea_ca95_51ad57dbe5c6["functools"]
  c629d49a_b244_753d_acc3_91ded440fff9 --> c990f2d7_9509_7cea_ca95_51ad57dbe5c6
  2a7f66a7_8738_3d47_375b_70fcaa6ac169["logging"]
  c629d49a_b244_753d_acc3_91ded440fff9 --> 2a7f66a7_8738_3d47_375b_70fcaa6ac169
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  c629d49a_b244_753d_acc3_91ded440fff9 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  c629d49a_b244_753d_acc3_91ded440fff9 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  c629d49a_b244_753d_acc3_91ded440fff9 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  c629d49a_b244_753d_acc3_91ded440fff9 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  c629d49a_b244_753d_acc3_91ded440fff9 --> 91721f45_4909_e489_8c1f_084f8bd87145
  01158a5b_b299_f45d_92e9_2a7433a1a91a["langchain_classic.chains.base"]
  c629d49a_b244_753d_acc3_91ded440fff9 --> 01158a5b_b299_f45d_92e9_2a7433a1a91a
  style c629d49a_b244_753d_acc3_91ded440fff9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Chain that runs an arbitrary python function."""

import functools
import logging
from collections.abc import Awaitable, Callable
from typing import Any

from langchain_core.callbacks import (
    AsyncCallbackManagerForChainRun,
    CallbackManagerForChainRun,
)
from pydantic import Field
from typing_extensions import override

from langchain_classic.chains.base import Chain

logger = logging.getLogger(__name__)


class TransformChain(Chain):
    """Chain that transforms the chain output.

    Example:
        ```python
        from langchain_classic.chains import TransformChain
        transform_chain = TransformChain(input_variables=["text"],
         output_variables["entities"], transform=func())

        ```
    """

    input_variables: list[str]
    """The keys expected by the transform's input dictionary."""
    output_variables: list[str]
    """The keys returned by the transform's output dictionary."""
    transform_cb: Callable[[dict[str, str]], dict[str, str]] = Field(alias="transform")
    """The transform function."""
    atransform_cb: Callable[[dict[str, Any]], Awaitable[dict[str, Any]]] | None = Field(
        None, alias="atransform"
    )
    """The async coroutine transform function."""

    @staticmethod
    @functools.lru_cache
    def _log_once(msg: str) -> None:
        """Log a message once."""
        logger.warning(msg)

    @property
    def input_keys(self) -> list[str]:
        """Expect input keys."""
        return self.input_variables

    @property
    def output_keys(self) -> list[str]:
        """Return output keys."""
        return self.output_variables

    @override
    def _call(
        self,
        inputs: dict[str, str],
        run_manager: CallbackManagerForChainRun | None = None,
    ) -> dict[str, str]:
        return self.transform_cb(inputs)

    @override
    async def _acall(
        self,
        inputs: dict[str, Any],
        run_manager: AsyncCallbackManagerForChainRun | None = None,
    ) -> dict[str, Any]:
        if self.atransform_cb is not None:
            return await self.atransform_cb(inputs)
        self._log_once(
            "TransformChain's atransform is not provided, falling"
            " back to synchronous transform",
        )
        return self.transform_cb(inputs)

Subdomains

Classes

Dependencies

  • collections.abc
  • functools
  • langchain_classic.chains.base
  • langchain_core.callbacks
  • logging
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does transform.py do?
transform.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What does transform.py depend on?
transform.py imports 8 module(s): collections.abc, functools, langchain_classic.chains.base, langchain_core.callbacks, logging, pydantic, typing, typing_extensions.
Where is transform.py in the architecture?
transform.py is located at libs/langchain/langchain_classic/chains/transform.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/chains).

Analyze Your Own Codebase

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

Try Supermodel Free