a
    •˜ëj[  ã                   @   sX   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	 d dl
mZ G dd„ deƒZdS )é    N)ÚAnyÚCallableÚOptionalÚUnion)Úparse_json_markdown)Úoverride)ÚStringEvaluatorc                	       sÎ   e Zd ZdZdeeeegef  eeegef  eddœ‡ fdd„Z	e
eedœdd„ƒƒZe
eedœd	d
„ƒƒZe
eedœdd„ƒƒZeeeedeeeef dœdd„Zedeee eedœ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.

    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Ú_Úreturnc              
      s†   t ƒ  ¡  |dur|| _nNzddlm} W n2 ty\ } zd}t|ƒ|‚W Y d}~n
d}~0 0 |jj| _|durx|| _n
dd„ | _dS )a¦  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.
        Nr   )Údistancez§The 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   úi/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/evaluation/parsing/json_distance.pyÚ<lambda>H   s   ýz4JsonEditDistanceEvaluator.__init__.<locals>.<lambda>)	ÚsuperÚ__init__Ú_string_distanceZ	rapidfuzzr   ÚImportErrorZDamerauLevenshteinZnormalized_distanceÚ_canonicalize)Úselfr
   r   r   ZrfdÚeÚmsg©Ú	__class__r   r   r   !   s    
ÿ 
z"JsonEditDistanceEvaluator.__init__)r   c                 C   s   dS )NFr   ©r   r   r   r   Úrequires_inputN   s    z(JsonEditDistanceEvaluator.requires_inputc                 C   s   dS )NTr   r#   r   r   r   Úrequires_referenceS   s    z,JsonEditDistanceEvaluator.requires_referencec                 C   s   dS )NZjson_edit_distancer   r#   r   r   r   Úevaluation_nameX   s    z)JsonEditDistanceEvaluator.evaluation_name)Únoder   c                 C   s   t |tƒrt|ƒS |S )N)Ú
isinstanceÚstrr   )r   r'   r   r   r   Ú_parse_json]   s    
z%JsonEditDistanceEvaluator._parse_json)Ú
predictionÚ	referenceÚkwargsr   c                 K   s4   |   |  |¡¡}|   |  |¡¡}|  ||¡}d|iS )NZscore)r   r*   r   )r   r+   r,   r-   ÚparsedÚlabelr   r   r   r   Ú_evaluate_stringsb   s    z+JsonEditDistanceEvaluator._evaluate_strings)NN)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r)   Úfloatr   r   Úpropertyr   Úboolr$   r%   r&   r   ÚdictÚlistÚintr*   r0   Ú__classcell__r   r   r!   r   r	   
   s6     ýû-" ýûr	   )r   Útypingr   r   r   r   Zlangchain_core.utils.jsonr   Ztyping_extensionsr   Zlangchain.evaluation.schemar   r	   r   r   r   r   Ú<module>   s
   