Home / Function/ _get_metric() — langchain Function Reference

_get_metric() — langchain Function Reference

Architecture documentation for the _get_metric() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  47ea03f1_ae34_6c9b_8db7_6adf9d7fb807["_get_metric()"]
  5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb["_RapidFuzzChainMixin"]
  47ea03f1_ae34_6c9b_8db7_6adf9d7fb807 -->|defined in| 5c42d4f5_4dcc_bdad_cf78_f643b26ab2eb
  49290d01_62f4_4e49_fdf7_9c4174a4ba2e["metric()"]
  49290d01_62f4_4e49_fdf7_9c4174a4ba2e -->|calls| 47ea03f1_ae34_6c9b_8db7_6adf9d7fb807
  style 47ea03f1_ae34_6c9b_8db7_6adf9d7fb807 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/evaluation/string_distance/base.py lines 106–138

    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

Domain

Subdomains

Called By

Frequently Asked Questions

What does _get_metric() do?
_get_metric() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/evaluation/string_distance/base.py.
Where is _get_metric() defined?
_get_metric() is defined in libs/langchain/langchain_classic/evaluation/string_distance/base.py at line 106.
What calls _get_metric()?
_get_metric() is called by 1 function(s): metric.

Analyze Your Own Codebase

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

Try Supermodel Free