Home / File/ moderation.py — langchain Source File

moderation.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  94a349af_c2c6_0226_b6d0_1ccacc5e4f09["moderation.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  94a349af_c2c6_0226_b6d0_1ccacc5e4f09 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  94a349af_c2c6_0226_b6d0_1ccacc5e4f09 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  f4d905c6_a2b2_eb8f_be9b_7808b72f6a16["langchain_core.utils"]
  94a349af_c2c6_0226_b6d0_1ccacc5e4f09 --> f4d905c6_a2b2_eb8f_be9b_7808b72f6a16
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  94a349af_c2c6_0226_b6d0_1ccacc5e4f09 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  94a349af_c2c6_0226_b6d0_1ccacc5e4f09 --> 91721f45_4909_e489_8c1f_084f8bd87145
  01158a5b_b299_f45d_92e9_2a7433a1a91a["langchain_classic.chains.base"]
  94a349af_c2c6_0226_b6d0_1ccacc5e4f09 --> 01158a5b_b299_f45d_92e9_2a7433a1a91a
  45fc8fd3_e815_b442_a6d1_0dedc9327b62["openai"]
  94a349af_c2c6_0226_b6d0_1ccacc5e4f09 --> 45fc8fd3_e815_b442_a6d1_0dedc9327b62
  style 94a349af_c2c6_0226_b6d0_1ccacc5e4f09 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Pass input through a moderation endpoint."""

from typing import Any

from langchain_core.callbacks import (
    AsyncCallbackManagerForChainRun,
    CallbackManagerForChainRun,
)
from langchain_core.utils import check_package_version, get_from_dict_or_env
from pydantic import Field, model_validator
from typing_extensions import override

from langchain_classic.chains.base import Chain


class OpenAIModerationChain(Chain):
    """Pass input through a moderation endpoint.

    To use, you should have the `openai` python package installed, and the
    environment variable `OPENAI_API_KEY` set with your API key.

    Any parameters that are valid to be passed to the openai.create call can be passed
    in, even if not explicitly saved on this class.

    Example:
        ```python
        from langchain_classic.chains import OpenAIModerationChain

        moderation = OpenAIModerationChain()
        ```
    """

    client: Any = None
    async_client: Any = None
    model_name: str | None = None
    """Moderation model name to use."""
    error: bool = False
    """Whether or not to error if bad content was found."""
    input_key: str = "input"
    output_key: str = "output"
    openai_api_key: str | None = None
    openai_organization: str | None = None
    openai_pre_1_0: bool = Field(default=False)

    @model_validator(mode="before")
    @classmethod
    def validate_environment(cls, values: dict) -> Any:
        """Validate that api key and python package exists in environment."""
        openai_api_key = get_from_dict_or_env(
            values,
            "openai_api_key",
            "OPENAI_API_KEY",
        )
        openai_organization = get_from_dict_or_env(
            values,
            "openai_organization",
            "OPENAI_ORGANIZATION",
            default="",
        )
        try:
// ... (70 more lines)

Subdomains

Dependencies

  • langchain_classic.chains.base
  • langchain_core.callbacks
  • langchain_core.utils
  • openai
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does moderation.py do?
moderation.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What does moderation.py depend on?
moderation.py imports 7 module(s): langchain_classic.chains.base, langchain_core.callbacks, langchain_core.utils, openai, pydantic, typing, typing_extensions.
Where is moderation.py in the architecture?
moderation.py is located at libs/langchain/langchain_classic/chains/moderation.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