Ë
    ´Œj«  ã                   ó^   — 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e«      Zy)é    N)ÚAnyÚCallableÚOptionalÚUnion)Úparse_json_markdown)Úoverride)ÚStringEvaluatorc                   óþ   ‡ — 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„Z	e
defd„«       Ze
defd	„«       Ze
defd
„«       Zdedeeedeeeef   fd„Ze	 	 ddedee   dee   dedef
d„«       Zˆ x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                 óÜ   •— t         ‰| �  «        |�|| _        n"	 ddlm} |j                  j                  | _        |�|| _        y d„ | _        y # t
        $ r}d}t        |«      |‚d }~ww xY w)Nr   )Údistancez§The default string_distance operator for the  JsonEditDistanceEvaluator requires installation of the rapidfuzz package. Please install it with `pip install rapidfuzz`.c                 ó2   — t        j                  | dd¬«      S )N)Ú,Ú:T)Ú
separatorsÚ	sort_keys)ÚjsonÚdumps)Úxs    út/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain/evaluation/parsing/json_distance.pyÚ<lambda>z4JsonEditDistanceEvaluator.__init__.<locals>.<lambda>?   s   € ¬4¯:©:ØØ%Øõ,ó    )	ÚsuperÚ__init__Ú_string_distanceÚ	rapidfuzzr   ÚImportErrorÚDamerauLevenshteinÚnormalized_distanceÚ_canonicalize)Úselfr   r   r   ÚrfdÚeÚmsgÚ	__class__s          €r   r   z"JsonEditDistanceEvaluator.__init__'   s€   ø€ ô 	‰ÑÔØÐ&Ø$3ˆDÕ!ð	.Ý5ð %(×$:Ñ$:×$NÑ$NˆDÔ!ØÐ#Ø!-ˆDÕñ"ˆDÕøô ò .ðFð ô " #Ó&¨AÐ-ûð.ús   ›A Á	A+ÁA&Á&A+c                  ó   — y)NF© ©r%   s    r   Úrequires_inputz(JsonEditDistanceEvaluator.requires_inputE   s   € àr   c                  ó   — y)NTr+   r,   s    r   Úrequires_referencez,JsonEditDistanceEvaluator.requires_referenceI   s   € àr   c                  ó   — y)NÚjson_edit_distancer+   r,   s    r   Úevaluation_namez)JsonEditDistanceEvaluator.evaluation_nameM   s   € à#r   Únodec                 ó<   — t        |t        «      rt        |«      S |S )N)Ú
isinstanceÚstrr   )r%   r3   s     r   Ú_parse_jsonz%JsonEditDistanceEvaluator._parse_jsonQ   s   € Ü�dœCÔ Ü& tÓ,Ð,Øˆr   Ú
predictionÚinputÚ	referencec                 ó®   — | j                  | j                  |«      «      }| j                  | j                  |«      «      }| j                  ||«      }d|iS )NÚscore)r$   r7   r   )r%   r8   r9   r:   r   ÚparsedÚlabelr   s           r   Ú_evaluate_stringsz+JsonEditDistanceEvaluator._evaluate_stringsV   sW   € ð ×#Ñ# D×$4Ñ$4°ZÓ$@ÓAˆØ×"Ñ" 4×#3Ñ#3°IÓ#>Ó?ˆØ×(Ñ(¨°Ó7ˆØ˜Ð"Ð"r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r6   Úfloatr   r   ÚpropertyÚboolr-   r/   r2   r   ÚdictÚlistÚintr7   r   r?   Ú__classcell__)r)   s   @r   r   r   
   s-  ø„ ñð< BFØ7;ñà! (¨C°¨:°uÐ+<Ñ"=Ñ>ðð ˜x¨¨¨s¨
Ñ3Ñ4ðð ð	ð
 
õð< ð ò ó ðð ð Dò ó ðð ð$ ò $ó ð$ð ð ¨¨d°D¸$ÀÀtÈSÐRUÐ.UÑ(Vó ð
 ð  $Ø#'ñ	
#àð
#ð ˜‰}ð
#ð ˜C‘=ð	
#ð
 ð
#ð 
ò
#ó ô
#r   r   )r   Útypingr   r   r   r   Úlangchain_core.utils.jsonr   Útyping_extensionsr   Úlangchain.evaluation.schemar	   r   r+   r   r   Ú<module>rO      s%   ðÛ ß 1Ó 1å 9Ý &å 7ôW# õ W#r   