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
  0b2ef852_a8bd_0357_97fb_8efda447e910["base.py"]
  06ab3965_70ce_6e2c_feb9_564d849aa5f4["string"]
  0b2ef852_a8bd_0357_97fb_8efda447e910 --> 06ab3965_70ce_6e2c_feb9_564d849aa5f4
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  0b2ef852_a8bd_0357_97fb_8efda447e910 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  0b2ef852_a8bd_0357_97fb_8efda447e910 --> 91721f45_4909_e489_8c1f_084f8bd87145
  538b302b_528d_b6e6_cf56_04147780d18b["langchain_classic.evaluation.schema"]
  0b2ef852_a8bd_0357_97fb_8efda447e910 --> 538b302b_528d_b6e6_cf56_04147780d18b
  style 0b2ef852_a8bd_0357_97fb_8efda447e910 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import string
from typing import Any

from typing_extensions import override

from langchain_classic.evaluation.schema import StringEvaluator


class ExactMatchStringEvaluator(StringEvaluator):
    """Compute an exact match between the prediction and the reference.

    Examples:
    ----------
    >>> evaluator = ExactMatchChain()
    >>> evaluator.evaluate_strings(
            prediction="Mindy is the CTO",
            reference="Mindy is the CTO",
        )  # This will return {'score': 1.0}

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

    def __init__(
        self,
        *,
        ignore_case: bool = False,
        ignore_punctuation: bool = False,
        ignore_numbers: bool = False,
        **_: Any,
    ):
        """Initialize the `ExactMatchStringEvaluator`.

        Args:
            ignore_case: Whether to ignore case when comparing strings.
            ignore_punctuation: Whether to ignore punctuation when comparing strings.
            ignore_numbers: Whether to ignore numbers when comparing strings.
        """
        super().__init__()
        self.ignore_case = ignore_case
        self.ignore_punctuation = ignore_punctuation
        self.ignore_numbers = ignore_numbers

    @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 "exact_match"

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

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

        Returns:
            The evaluation results containing the score.
        """
        if self.ignore_case:
            prediction = prediction.lower()
            reference = reference.lower()
        if self.ignore_punctuation:
            prediction = prediction.translate(str.maketrans("", "", string.punctuation))
            reference = reference.translate(str.maketrans("", "", string.punctuation))
        if self.ignore_numbers:
            prediction = prediction.translate(str.maketrans("", "", string.digits))
            reference = reference.translate(str.maketrans("", "", string.digits))
        return {"score": int(prediction == reference)}

Subdomains

Dependencies

  • langchain_classic.evaluation.schema
  • string
  • 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 CoreAbstractions domain, Serialization subdomain.
What does base.py depend on?
base.py imports 4 module(s): langchain_classic.evaluation.schema, string, typing, typing_extensions.
Where is base.py in the architecture?
base.py is located at libs/langchain/langchain_classic/evaluation/exact_match/base.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/langchain/langchain_classic/evaluation/exact_match).

Analyze Your Own Codebase

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

Try Supermodel Free