Home / Class/ _RapidFuzzChainMixin Class — langchain Architecture

_RapidFuzzChainMixin Class — langchain Architecture

Architecture documentation for the _RapidFuzzChainMixin class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb["_RapidFuzzChainMixin"]
  f3cef70e_11b0_61c9_7ec0_7308f4b45056["Chain"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|extends| f3cef70e_11b0_61c9_7ec0_7308f4b45056
  2ed5965a_26da_da8e_a7d5_637c8db1ed79["StringDistance"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|extends| 2ed5965a_26da_da8e_a7d5_637c8db1ed79
  e8a12ac8_a98d_9b97_548e_a57588a0d3dc["base.py"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|defined in| e8a12ac8_a98d_9b97_548e_a57588a0d3dc
  acfebc1d_25d5_7138_1671_16ea528e5c1d["validate_dependencies()"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|method| acfebc1d_25d5_7138_1671_16ea528e5c1d
  24fcfd3a_2f11_a3bc_6421_32dcbc008fe4["output_keys()"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|method| 24fcfd3a_2f11_a3bc_6421_32dcbc008fe4
  b169d8ca_baf2_528c_d013_53f7c6ab28a6["_prepare_output()"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|method| b169d8ca_baf2_528c_d013_53f7c6ab28a6
  47ea03f1_ae34_6c9b_8db7_6adf9d7fb807["_get_metric()"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|method| 47ea03f1_ae34_6c9b_8db7_6adf9d7fb807
  49290d01_62f4_4e49_fdf7_9c4174a4ba2e["metric()"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|method| 49290d01_62f4_4e49_fdf7_9c4174a4ba2e
  5ca4db2b_adc2_0733_78d9_bd519d1ec89b["compute_metric()"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb -->|method| 5ca4db2b_adc2_0733_78d9_bd519d1ec89b

Relationship Graph

Source Code

libs/langchain/langchain_classic/evaluation/string_distance/base.py lines 61–162

class _RapidFuzzChainMixin(Chain):
    """Shared methods for the rapidfuzz string distance evaluators."""

    distance: StringDistance = Field(default=StringDistance.JARO_WINKLER)
    normalize_score: bool = Field(default=True)
    """Whether to normalize the score to a value between `0` and `1`.
    Applies only to the Levenshtein and Damerau-Levenshtein distances."""

    @pre_init
    def validate_dependencies(cls, values: dict[str, Any]) -> dict[str, Any]:
        """Validate that the rapidfuzz library is installed.

        Args:
            values: The input values.

        Returns:
            The validated values.
        """
        _load_rapidfuzz()
        return values

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

        Returns:
            The output keys.
        """
        return ["score"]

    def _prepare_output(self, result: dict[str, Any]) -> dict[str, Any]:
        """Prepare the output dictionary.

        Args:
            result: The evaluation results.

        Returns:
            The prepared output dictionary.
        """
        result = {"score": result["score"]}
        if RUN_KEY in result:
            result[RUN_KEY] = result[RUN_KEY].dict()
        return result

    @staticmethod
    def _get_metric(distance: str, *, normalize_score: bool = False) -> Callable:
        """Get the distance metric function based on the distance type.

        Args:
            distance: The distance type.
            normalize_score: Whether to normalize the score.

        Returns:
            The distance metric function.

        Raises:
            ValueError: If the distance metric is invalid.
        """
        from rapidfuzz import distance as rf_distance

        module_map: dict[str, Any] = {
            StringDistance.DAMERAU_LEVENSHTEIN: rf_distance.DamerauLevenshtein,
            StringDistance.LEVENSHTEIN: rf_distance.Levenshtein,
            StringDistance.JARO: rf_distance.Jaro,
            StringDistance.JARO_WINKLER: rf_distance.JaroWinkler,
            StringDistance.HAMMING: rf_distance.Hamming,
            StringDistance.INDEL: rf_distance.Indel,
        }
        if distance not in module_map:
            msg = (
                f"Invalid distance metric: {distance}"
                f"\nMust be one of: {list(StringDistance)}"
            )
            raise ValueError(msg)
        module = module_map[distance]
        if normalize_score:
            return module.normalized_distance
        return module.distance

    @property
    def metric(self) -> Callable:

Frequently Asked Questions

What is the _RapidFuzzChainMixin class?
_RapidFuzzChainMixin is a class in the langchain codebase, defined in libs/langchain/langchain_classic/evaluation/string_distance/base.py.
Where is _RapidFuzzChainMixin defined?
_RapidFuzzChainMixin is defined in libs/langchain/langchain_classic/evaluation/string_distance/base.py at line 61.
What does _RapidFuzzChainMixin extend?
_RapidFuzzChainMixin extends Chain, StringDistance.

Analyze Your Own Codebase

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

Try Supermodel Free