o
    JjgN                     @   sL   d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 G dd de	Z
dS )    N)AnyCallableOptionalUnion)parse_json_markdown)StringEvaluatorc                       s   e Zd ZdZ		ddeeeegef  deeegef  deddf fddZ	e
defd	d
Ze
defddZe
defddZdedeeedeeeef fddZ		ddedee dee dedef
ddZ  ZS )JsonEditDistanceEvaluatora  
    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.

    Args:
        string_distance (Optional[Callable[[str, str], float]]): A callable that computes the distance between two strings.
            If not provided, a Damerau-Levenshtein distance from the `rapidfuzz` package will be used.
        canonicalize (Optional[Callable[[Any], Any]]): 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.
        **kwargs (Any): Additional keyword arguments.

    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.

    Nstring_distancecanonicalizekwargsreturnc                    sl   t    |d ur|| _nzddlm} W n ty    tdw |jj| _|d ur/|| _d S dd | _d S )Nr   )distancezThe default string_distance operator for the  JsonEditDistanceEvaluator requires installation of the rapidfuzz package. Please install it with `pip install rapidfuzz`.c                 S   s   t j| dddS )N),:T)
separators	sort_keys)jsondumps)x r   b/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/evaluation/parsing/json_distance.py<lambda>=   s
    z4JsonEditDistanceEvaluator.__init__.<locals>.<lambda>)	super__init___string_distance	rapidfuzzr   ImportErrorDamerauLevenshteinnormalized_distance_canonicalize)selfr	   r
   r   rfd	__class__r   r   r   &   s   


z"JsonEditDistanceEvaluator.__init__c                 C      dS )NFr   r    r   r   r   requires_inputC      z(JsonEditDistanceEvaluator.requires_inputc                 C   r$   )NTr   r%   r   r   r   requires_referenceG   r'   z,JsonEditDistanceEvaluator.requires_referencec                 C   r$   )Njson_edit_distancer   r%   r   r   r   evaluation_nameK   r'   z)JsonEditDistanceEvaluator.evaluation_namenodec                 C   s   t |tr	t|S |S )N)
isinstancestrr   )r    r+   r   r   r   _parse_jsonO   s   
z%JsonEditDistanceEvaluator._parse_json
predictioninput	referencec                 K   s4   |  | |}|  | |}| ||}d|iS )Nscore)r   r.   r   )r    r/   r0   r1   r   parsedlabelr   r   r   r   _evaluate_stringsT   s   z+JsonEditDistanceEvaluator._evaluate_strings)NN)__name__
__module____qualname____doc__r   r   r-   floatr   r   propertyboolr&   r(   r*   r   dictlistintr.   r5   __classcell__r   r   r"   r   r   	   sB    $r   )r   typingr   r   r   r   langchain_core.utils.jsonr   langchain.evaluation.schemar   r   r   r   r   r   <module>   s
    