Home / File/ json_distance.py — langchain Source File

json_distance.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  48064ee3_5685_911f_0066_eea8215a76c0["json_distance.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  48064ee3_5685_911f_0066_eea8215a76c0 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  48064ee3_5685_911f_0066_eea8215a76c0 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  48064ee3_5685_911f_0066_eea8215a76c0 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  78c5ca66_f675_3ca1_fdb7_d5a994dcf4bf["langchain_core.utils.json"]
  48064ee3_5685_911f_0066_eea8215a76c0 --> 78c5ca66_f675_3ca1_fdb7_d5a994dcf4bf
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  48064ee3_5685_911f_0066_eea8215a76c0 --> 91721f45_4909_e489_8c1f_084f8bd87145
  538b302b_528d_b6e6_cf56_04147780d18b["langchain_classic.evaluation.schema"]
  48064ee3_5685_911f_0066_eea8215a76c0 --> 538b302b_528d_b6e6_cf56_04147780d18b
  927b7bf8_8755_9458_b82b_b966efa51e6a["rapidfuzz"]
  48064ee3_5685_911f_0066_eea8215a76c0 --> 927b7bf8_8755_9458_b82b_b966efa51e6a
  style 48064ee3_5685_911f_0066_eea8215a76c0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import json
from collections.abc import Callable
from typing import Any

from langchain_core.utils.json import parse_json_markdown
from typing_extensions import override

from langchain_classic.evaluation.schema import StringEvaluator


class JsonEditDistanceEvaluator(StringEvaluator):
    """An evaluator that calculates the edit distance between JSON strings.

    This evaluator computes a normalized Damerau-Levenshtein distance between two JSON strings
    after parsing them and converting them to a canonical format (i.e., whitespace and key order are normalized).
    It can be customized with alternative distance and canonicalization functions.

    Attributes:
        _string_distance (Callable[[str, str], float]): The internal distance computation function.
        _canonicalize (Callable[[Any], Any]): The internal canonicalization function.

    Examples:
        >>> evaluator = JsonEditDistanceEvaluator()
        >>> result = evaluator.evaluate_strings(
        ...     prediction='{"a": 1, "b": 2}', reference='{"a": 1, "b": 3}'
        ... )
        >>> assert result["score"] is not None

    Raises:
        ImportError: If `rapidfuzz` is not installed and no alternative `string_distance` function is provided.

    """  # noqa: E501

    def __init__(
        self,
        string_distance: Callable[[str, str], float] | None = None,
        canonicalize: Callable[[Any], Any] | None = None,
        **_: Any,
    ) -> None:
        """Initialize the JsonEditDistanceEvaluator.

        Args:
            string_distance: A callable that computes the distance between two strings.
                If not provided, a Damerau-Levenshtein distance from the `rapidfuzz`
                package will be used.
            canonicalize: A callable that converts a parsed JSON object into its
                canonical string form.
                If not provided, the default behavior is to serialize the JSON with
                sorted keys and no extra whitespace.

        Raises:
            ImportError: If the `rapidfuzz` package is not installed and no
                `string_distance` function is provided.
        """
        super().__init__()
        if string_distance is not None:
            self._string_distance = string_distance
        else:
            try:
                from rapidfuzz import distance as rfd
            except ImportError as e:
                msg = (
                    "The default string_distance operator for the "
                    " JsonEditDistanceEvaluator requires installation of "
                    "the rapidfuzz package. "
                    "Please install it with `pip install rapidfuzz`."
                )
                raise ImportError(msg) from e
            self._string_distance = rfd.DamerauLevenshtein.normalized_distance
        if canonicalize is not None:
            self._canonicalize = canonicalize
        else:
            self._canonicalize = lambda x: json.dumps(
                x,
                separators=(",", ":"),
                sort_keys=True,  # eliminate whitespace
            )

    @property
    @override
    def requires_input(self) -> bool:
        return False

    @property
    @override
    def requires_reference(self) -> bool:
        return True

    @property
    @override
    def evaluation_name(self) -> str:
        return "json_edit_distance"

    def _parse_json(self, node: Any) -> dict | list | None | float | bool | int | str:
        if isinstance(node, str):
            return parse_json_markdown(node)
        return node

    @override
    def _evaluate_strings(
        self,
        prediction: str,
        reference: str | None = None,
        **kwargs: Any,
    ) -> dict:
        parsed = self._canonicalize(self._parse_json(prediction))
        label = self._canonicalize(self._parse_json(reference))
        distance = self._string_distance(parsed, label)
        return {"score": distance}

Subdomains

Dependencies

  • collections.abc
  • json
  • langchain_classic.evaluation.schema
  • langchain_core.utils.json
  • rapidfuzz
  • typing
  • typing_extensions

Frequently Asked Questions

What does json_distance.py do?
json_distance.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What does json_distance.py depend on?
json_distance.py imports 7 module(s): collections.abc, json, langchain_classic.evaluation.schema, langchain_core.utils.json, rapidfuzz, typing, typing_extensions.
Where is json_distance.py in the architecture?
json_distance.py is located at libs/langchain/langchain_classic/evaluation/parsing/json_distance.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/evaluation/parsing).

Analyze Your Own Codebase

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

Try Supermodel Free