o
    Jjgz                     @   sp   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 G dd deZG d	d
 d
eZdS )zEvaluators for parsing strings.    N)eq)AnyCallableOptionalUnioncast)parse_json_markdown)StringEvaluatorc                       s   e Zd ZdZ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de
dee
 dee
 dedef
ddZ  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                    s   t    d S N)super__init__)selfr   	__class__ Y/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/evaluation/parsing/base.pyr   %   s   zJsonValidityEvaluator.__init__c                 C      dS NFr   r   r   r   r   requires_input(      z$JsonValidityEvaluator.requires_inputc                 C   r   r   r   r   r   r   r   requires_reference,   r   z(JsonValidityEvaluator.requires_referencec                 C   r   )Njson_validityr   r   r   r   r   evaluation_name0   r   z%JsonValidityEvaluator.evaluation_name
predictioninput	referencec              
   K   sL   zt |tjd ddiW S  ty% } zdt|dW  Y d}~S d}~w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.

        )parserscore   r   )r!   	reasoningN)r   jsonloads	Exceptionstr)r   r   r   r   r   er   r   r   _evaluate_strings4   s   
z'JsonValidityEvaluator._evaluate_stringsNN)__name__
__module____qualname____doc__r   r   propertyboolr   r   r'   r   r   dictr)   __classcell__r   r   r   r   r
      s,    r
   c                       s   e Zd ZdZddee 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 )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                    s   t    |pt| _d S r   )r   r   r   r4   )r   r4   r   r   r   r   r   o   s   
zJsonEqualityEvaluator.__init__c                 C   r   r   r   r   r   r   r   r   s   r   z$JsonEqualityEvaluator.requires_inputc                 C   r   )NTr   r   r   r   r   r   w   r   z(JsonEqualityEvaluator.requires_referencec                 C   r   )Njson_equalityr   r   r   r   r   r   {   r   z%JsonEqualityEvaluator.evaluation_namestringc                 C   s   t |tr	t|S |S r   )
isinstancer'   r   )r   r6   r   r   r   _parse_json   s   
z!JsonEqualityEvaluator._parse_jsonr   r   r   c                 K   sf   |  |}|  tt|}t|tr+t|tsddiS t|dd d}t|dd d}d| ||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                 S      t | S r   r'   xr   r   r   <lambda>       z9JsonEqualityEvaluator._evaluate_strings.<locals>.<lambda>)keyc                 S   r9   r   r:   r;   r   r   r   r=      r>   )r8   r   r'   r7   listsortedr4   )r   r   r   r   r   parsedlabelr   r   r   r)      s   


z'JsonEqualityEvaluator._evaluate_stringsr   r*   )r+   r,   r-   r.   r   r   r   r   r/   r0   r   r   r'   r   r   r1   r@   floatintr8   r)   r2   r   r   r   r   r3   P   s6     
r3   )r.   r$   r4   r   typingr   r   r   r   r   langchain_core.utils.jsonr   langchain.evaluation.schemar	   r
   r3   r   r   r   r   <module>   s    D