
    KKi                         S r SSKrSSKrSSKJr  SSKJr  SSKJrJ	r	  SSK
Jr  SSKJr  SSKJr  \R                   " \5      r " S	 S
\5      r " S S\5      rg)zEvaluators for parsing strings.    N)Callable)eq)Anycast)parse_json_markdown)override)StringEvaluatorc                      ^  \ rS rSrSrS\SS4U 4S jjr\\S\	4S j5       5       r
\\S\	4S j5       5       r\\S\4S	 j5       5       r\S
\S\S\4S j5       rSrU =r$ )JsonValidityEvaluator   a2  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: Whether this evaluator requires an input
        string. Always False.
    requires_reference: Whether this evaluator requires a
        reference string. Always False.
    evaluation_name: 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'}
_returnNc                 "   > [         TU ]  5         g)z%Initialize the JsonValidityEvaluator.N)super__init__)selfr   	__class__s     k/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langchain_classic/evaluation/parsing/base.pyr   JsonValidityEvaluator.__init__*   s        c                     gNF r   s    r   requires_input$JsonValidityEvaluator.requires_input.        r   c                     gr   r   r   s    r   requires_reference(JsonValidityEvaluator.requires_reference3   r   r   c                     g)Njson_validityr   r   s    r   evaluation_name%JsonValidityEvaluator.evaluation_name8        r   
predictionkwargsc                     [        U[        R                  S9  SS0$ ! [        R                   a  nS[	        U5      S.s SnA$ SnAf[
         a-  n[        R                  S5        S[	        U5      S.s SnA$ SnAff = f)a  Evaluate the prediction string.

Args:
    prediction: The prediction string to evaluate.
    **kwargs: Additional keyword arguments (not used).

Returns:
    `dict` 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	reasoningNz*Passing JSON failed with unexpected error.r*      )r   jsonloadsJSONDecodeErrorstr	Exception_logger	exception)r   r&   r'   es       r   _evaluate_strings'JsonValidityEvaluator._evaluate_strings=   so    (	5
4::> | ## 	5SV44 	5JKSV44	5s+    A?AA?A?"A:4A?:A?r   )__name__
__module____qualname____firstlineno____doc__r   r   propertyr   boolr   r   r0   r#   dictr5   __static_attributes____classcell__r   s   @r   r   r      s    0C D      D          
	 r   r   c            
         ^  \ rS rSrSrSS\S-  S\SS4U 4S jjjr\\	S\
4S j5       5       r\\	S\
4S	 j5       5       r\\	S\4S
 j5       5       rS\S\\-  S-  \-  \
-  \-  \-  4S jr\	 SS\S\S-  S\S\4S jj5       rSrU =r$ )JsonEqualityEvaluator[   a'  Json Equality Evaluator.

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: Whether this evaluator requires an
        input string. Always False.
    requires_reference: Whether this evaluator requires
        a reference string. Always True.
    evaluation_name: 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                 J   > [         TU ]  5         U=(       d    [        U l        g)zInitialize the JsonEqualityEvaluator.

Args:
    operator: A custom operator to compare the parsed JSON objects.
        Defaults to equality (`eq`).
N)r   r   r   rE   )r   rE   r   r   s      r   r   JsonEqualityEvaluator.__init__|   s     	 Br   c                     gr   r   r   s    r   r   $JsonEqualityEvaluator.requires_input   r   r   c                     g)NTr   r   s    r   r   (JsonEqualityEvaluator.requires_reference   s     r   c                     g)Njson_equalityr   r   s    r   r#   %JsonEqualityEvaluator.evaluation_name   r%   r   stringc                 F    [        U[        5      (       a  [        U5      $ U$ N)
isinstancer0   r   )r   rO   s     r   _parse_json!JsonEqualityEvaluator._parse_json   s!     fc""&v..r   r&   	referencer'   c                    U R                  U5      nU R                  [        SU5      5      n[        U[        5      (       a/  [        U[        5      (       d  SS0$ [	        US S9n[	        US S9nSU R                  XE5      0$ )zEvaluate the prediction string.

Args:
    prediction: The prediction string to evaluate.
    reference: The reference string to compare against.
    **kwargs: Additional keyword arguments (not used).

Returns:
    `dict` containing the evaluation score.
r0   r*   r   c                     [        U 5      $ rQ   r0   xs    r   <lambda>9JsonEqualityEvaluator._evaluate_strings.<locals>.<lambda>   s    #a&r   )keyc                     [        U 5      $ rQ   rX   rY   s    r   r[   r\      s    Ar   )rS   r   rR   listsortedrE   )r   r&   rU   r'   parsedlabels         r   r5   'JsonEqualityEvaluator._evaluate_strings   s}    " !!*-  eY!78eT""fd++|#F(89F5&67Ev566r   )rE   rQ   )r7   r8   r9   r:   r;   r   r   r   r<   r   r=   r   r   r0   r#   r>   r_   floatintrS   r5   r?   r@   rA   s   @r   rC   rC   [   s   @'D 'c 'd ' '     D        
t	e	#d	*S	03	6  !%77 :7 	7
 
7 7r   rC   )r;   r-   loggingcollections.abcr   rE   r   typingr   r   langchain_core.utils.jsonr   typing_extensionsr   #langchain_classic.evaluation.schemar	   	getLoggerr7   r2   r   rC   r   r   r   <module>rm      sM    %   $   9 & ?


H
%GO GTZ7O Z7r   