Ë
    ´Œj¹  ã                   óˆ   — d Z ddlZddlmZ ddlmZ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 G d
„ de«      Zy)zEvaluators for parsing strings.é    N)Úeq)ÚAnyÚCallableÚOptionalÚUnionÚcast)Úparse_json_markdown)Úoverride)ÚStringEvaluatorc                   ó¦   ‡ — e Zd ZdZ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e	 	 dd	e
d
ee
   dee
   dedef
d„«       Zˆ xZS )ÚJsonValidityEvaluatoraŽ  Evaluate whether the prediction is valid JSON.

    This evaluator checks if the prediction is a valid JSON string. It does not
        require any input or reference.

    Attributes:
        requires_input (bool): Whether this evaluator requires an input
            string. Always False.
        requires_reference (bool): Whether this evaluator requires a
            reference string. Always False.
        evaluation_name (str): The name of the evaluation metric.
            Always "json".

    Examples:
        >>> evaluator = JsonValidityEvaluator()
        >>> prediction = '{"name": "John", "age": 30, "city": "New York"}'
        >>> evaluator.evaluate(prediction)
        {'score': 1}

        >>> prediction = '{"name": "John", "age": 30, "city": "New York",}'
        >>> evaluator.evaluate(prediction)
        {'score': 0, 'reasoning': 'Expecting property name enclosed in double quotes'}
    ÚkwargsÚreturnNc                 ó"   •— t         ‰| �  «        y ©N)ÚsuperÚ__init__)Úselfr   Ú	__class__s     €úk/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain/evaluation/parsing/base.pyr   zJsonValidityEvaluator.__init__&   s   ø€ Ü‰ÑÕó    c                  ó   — y©NF© ©r   s    r   Úrequires_inputz$JsonValidityEvaluator.requires_input)   ó   € àr   c                  ó   — yr   r   r   s    r   Úrequires_referencez(JsonValidityEvaluator.requires_reference-   r   r   c                  ó   — y)NÚjson_validityr   r   s    r   Úevaluation_namez%JsonValidityEvaluator.evaluation_name1   ó   € àr   Ú
predictionÚinputÚ	referencec                 óŠ   — 	 t        |t        j                  ¬«       ddiS # t        $ r}dt	        |«      dœcY d}~S d}~ww xY w)aL  Evaluate the prediction string.

        Args:
            prediction (str): The prediction string to evaluate.
            input (str, optional): Not used in this evaluator. Defaults to None.
            reference (str, optional): Not used in this evaluator. Defaults to None.

        Returns:
            dict: A dictionary containing the evaluation score. The score is 1 if
            the prediction is valid JSON, and 0 otherwise.
                If the prediction is not valid JSON, the dictionary also contains
                a "reasoning" field with the error message.

        )Úparserr   )ÚscoreÚ	reasoningNr)   é   )r	   ÚjsonÚloadsÚ	ExceptionÚstr)r   r$   r%   r&   r   Úes         r   Ú_evaluate_stringsz'JsonValidityEvaluator._evaluate_strings5   sB   € ð,	5Ü 
´4·:±:Õ>ð ˜ˆ|Ðøô ò 	5Ø¬S°«VÑ4Õ4ûð	5ús   ‚! ¡	Aª=·A½A©NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚpropertyÚboolr   r   r/   r"   r
   r   Údictr1   Ú__classcell__©r   s   @r   r   r      sÀ   ø„ ñð0 ð ¨õ ð ð ò ó ðð ð Dò ó ðð ð ò ó ðð ð  $Ø#'ñ	àðð ˜‰}ðð ˜C‘=ð	ð
 ðð 
òó ôr   r   c                   óÖ   ‡ — e Zd ZdZddee   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 )ÚJsonEqualityEvaluatora�  Evaluate whether the prediction is equal to the reference after
        parsing both as JSON.

    This evaluator checks if the prediction, after parsing as JSON, is equal
        to the reference,
    which is also parsed as JSON. It does not require an input string.

    Attributes:
        requires_input (bool): Whether this evaluator requires an
            input string. Always False.
        requires_reference (bool): Whether this evaluator requires
            a reference string. Always True.
        evaluation_name (str): The name of the evaluation metric.
            Always "parsed_equality".

    Examples:
        >>> evaluator = JsonEqualityEvaluator()
        >>> evaluator.evaluate_strings('{"a": 1}', reference='{"a": 1}')
        {'score': True}
        >>> evaluator.evaluate_strings('{"a": 1}', reference='{"a": 2}')
        {'score': False}

        >>> evaluator = JsonEqualityEvaluator(operator=lambda x, y: x['a'] == y['a'])
        >>> evaluator.evaluate_strings('{"a": 1}', reference='{"a": 1}')
        {'score': True}
        >>> evaluator.evaluate_strings('{"a": 1}', reference='{"a": 2}')
        {'score': False}

    NÚoperatorr   r   c                 ó@   •— t         ‰| �  «        |xs t        | _        y r   )r   r   r   r>   )r   r>   r   r   s      €r   r   zJsonEqualityEvaluator.__init__q   s   ø€ Ü‰ÑÔØ š¤Bˆ�r   c                  ó   — yr   r   r   s    r   r   z$JsonEqualityEvaluator.requires_inputu   r   r   c                  ó   — y)NTr   r   s    r   r   z(JsonEqualityEvaluator.requires_referencey   s   € àr   c                  ó   — y)NÚjson_equalityr   r   s    r   r"   z%JsonEqualityEvaluator.evaluation_name}   r#   r   Ústringc                 ó<   — t        |t        «      rt        |«      S |S r   )Ú
isinstancer/   r	   )r   rD   s     r   Ú_parse_jsonz!JsonEqualityEvaluator._parse_json�   s   € ô �fœcÔ"Ü& vÓ.Ð.Øˆr   r$   r%   r&   c                 ó
  — | j                  |«      }| j                  t        t        |«      «      }t        |t        «      r0t        |t        «      sddiS t        |d„ ¬«      }t        |d„ ¬«      }d| j                  ||«      iS )aO  Evaluate the prediction string.

        Args:
            prediction (str): The prediction string to evaluate.
            input (str, optional): Not used in this evaluator.
            reference (str): The reference string to compare against.

        Returns:
            dict: A dictionary containing the evaluation score.
        r)   r   c                 ó   — t        | «      S r   ©r/   ©Úxs    r   Ú<lambda>z9JsonEqualityEvaluator._evaluate_strings.<locals>.<lambda>    s   € ´#°a´&r   )Úkeyc                 ó   — t        | «      S r   rJ   rK   s    r   rM   z9JsonEqualityEvaluator._evaluate_strings.<locals>.<lambda>¡   s   € ´°A´r   )rG   r   r/   rF   ÚlistÚsortedr>   )r   r$   r%   r&   r   ÚparsedÚlabels          r   r1   z'JsonEqualityEvaluator._evaluate_strings‰   sy   € ð$ ×!Ñ! *Ó-ˆØ× Ñ ¤¤c¨9Ó!5Ó6ˆÜ�eœTÔ"Ü˜f¤dÔ+Ø �|Ð#Ü˜FÑ(8Ô9ˆFÜ˜5Ñ&6Ô7ˆEØ˜Ÿ™ v¨uÓ5Ð6Ð6r   r   r2   )r3   r4   r5   r6   r   r   r   r   r7   r8   r   r   r/   r"   r   r9   rP   ÚfloatÚintrG   r
   r1   r:   r;   s   @r   r=   r=   R   s÷   ø„ ññ<' ¨(Ñ!3ð 'Àcð 'Èdõ 'ð ð ò ó ðð ð Dò ó ðð ð ò ó ððàðð 
ˆt�T˜4 ¨¨c°3Ð6Ñ	7óð ð  $Ø#'ñ	7àð7ð ˜‰}ð7ð ˜C‘=ð	7ð
 ð7ð 
ò7ó ô7r   r=   )r6   r,   r>   r   Útypingr   r   r   r   r   Úlangchain_core.utils.jsonr	   Útyping_extensionsr
   Úlangchain.evaluation.schemar   r   r=   r   r   r   Ú<module>rZ      s:   ðÙ %ã Ý ß 7Õ 7å 9Ý &å 7ôB˜Oô BôJP7˜Oõ P7r   