Home / File/ base.py — langchain Source File

base.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  6e19ef03_b3fe_9631_cb2c_38dd63c7086d["base.py"]
  b7996424_637b_0b54_6edf_2e18e9c1a8bf["re"]
  6e19ef03_b3fe_9631_cb2c_38dd63c7086d --> b7996424_637b_0b54_6edf_2e18e9c1a8bf
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  6e19ef03_b3fe_9631_cb2c_38dd63c7086d --> feec1ec4_6917_867b_d228_b134d0ff8099
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  6e19ef03_b3fe_9631_cb2c_38dd63c7086d --> f85fae70_1011_eaec_151c_4083140ae9e5
  37291248_07a6_a05c_821a_71cc0592429f["langchain_classic.evaluation.schema"]
  6e19ef03_b3fe_9631_cb2c_38dd63c7086d --> 37291248_07a6_a05c_821a_71cc0592429f
  style 6e19ef03_b3fe_9631_cb2c_38dd63c7086d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import re
from typing import Any

from typing_extensions import override

from langchain_classic.evaluation.schema import StringEvaluator


class RegexMatchStringEvaluator(StringEvaluator):
    """Compute a regex match between the prediction and the reference.

    Examples:
    ----------
    >>> evaluator = RegexMatchStringEvaluator(flags=re.IGNORECASE)
    >>> evaluator.evaluate_strings(
            prediction="Mindy is the CTO",
            reference="^mindy.*cto$",
        )  # This will return {'score': 1.0} due to the IGNORECASE flag

    >>> evaluator = RegexMatchStringEvaluator()
    >>> evaluator.evaluate_strings(
            prediction="Mindy is the CTO",
            reference="^Mike.*CEO$",
        )  # This will return {'score': 0.0}

    >>> evaluator.evaluate_strings(
            prediction="Mindy is the CTO",
            reference="^Mike.*CEO$|^Mindy.*CTO$",
        )  # This will return {'score': 1.0} as the prediction matches the second pattern in the union
    """  # noqa: E501

    def __init__(self, *, flags: int = 0, **_: Any):  # Default is no flags
        """Initialize the RegexMatchStringEvaluator.

        Args:
            flags: Flags to use for the regex match. Defaults to no flags.
        """
        super().__init__()
        self.flags = flags

    @property
    def requires_input(self) -> bool:
        """This evaluator does not require input."""
        return False

    @property
    def requires_reference(self) -> bool:
        """This evaluator requires a reference."""
        return True

    @property
    def input_keys(self) -> list[str]:
        """Get the input keys.

        Returns:
            The input keys.
        """
        return ["reference", "prediction"]

    @property
    def evaluation_name(self) -> str:
        """Get the evaluation name.

        Returns:
            The evaluation name.
        """
        return "regex_match"

    @override
    def _evaluate_strings(  # type: ignore[override]
        self,
        *,
        prediction: str,
        reference: str,
        **kwargs: Any,
    ) -> dict:
        """Evaluate the regex match between the prediction and the reference.

        Args:
            prediction: The prediction string.
            reference: The reference regex pattern.
            **kwargs: Additional keyword arguments (not used).

        Returns:
            The evaluation results containing the score.
        """
        match = re.match(reference, prediction, flags=self.flags)
        return {"score": int(bool(match))}

Domain

Subdomains

Dependencies

  • langchain_classic.evaluation.schema
  • re
  • typing
  • typing_extensions

Frequently Asked Questions

What does base.py do?
base.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, LanguageModelBase subdomain.
What does base.py depend on?
base.py imports 4 module(s): langchain_classic.evaluation.schema, re, typing, typing_extensions.
Where is base.py in the architecture?
base.py is located at libs/langchain/langchain_classic/evaluation/regex_match/base.py (domain: LangChainCore, subdomain: LanguageModelBase, directory: libs/langchain/langchain_classic/evaluation/regex_match).

Analyze Your Own Codebase

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

Try Supermodel Free