a
    agj                     @   s<   d dl mZmZ d dlmZ d dlmZ G dd deZdS )    )AnyUnion)parse_json_markdown)StringEvaluatorc                	       s   e Zd ZdZedd fddZeedddZeedd	d
Z	ee
dddZeeeedeeee
f dddZeeedddZdee
ef ee
ef ee
ef eedddZ  ZS )JsonSchemaEvaluatora  An evaluator that validates a JSON prediction against a JSON schema reference.

    This evaluator checks if a given JSON prediction conforms to the provided JSON schema.
    If the prediction is valid, the score is True (no errors). Otherwise, the score is False (error occurred).

    Attributes:
        requires_input (bool): Whether the evaluator requires input.
        requires_reference (bool): Whether the evaluator requires reference.
        evaluation_name (str): The name of the evaluation.

    Examples:
        evaluator = JsonSchemaEvaluator()
        result = evaluator.evaluate_strings(
            prediction='{"name": "John", "age": 30}',
            reference={
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"}
                }
            }
        )
        assert result["score"] is not None

    N)kwargsreturnc                    s6   t    zddl}W n ty0   tdY n0 dS )zInitializes the JsonSchemaEvaluator.

        Args:
            kwargs: Additional keyword arguments.

        Raises:
            ImportError: If the jsonschema package is not installed.
        r   NziThe JsonSchemaEvaluator requires the jsonschema package. Please install it with `pip install jsonschema`.)super__init__
jsonschemaImportError)selfr   r   	__class__ v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain/evaluation/parsing/json_schema.pyr
   #   s    	
zJsonSchemaEvaluator.__init__)r   c                 C   s   dS )z-Returns whether the evaluator requires input.Fr   r   r   r   r   requires_input5   s    z"JsonSchemaEvaluator.requires_inputc                 C   s   dS )z1Returns whether the evaluator requires reference.Tr   r   r   r   r   requires_reference:   s    z&JsonSchemaEvaluator.requires_referencec                 C   s   dS )z#Returns the name of the evaluation.Zjson_schema_validationr   r   r   r   r   evaluation_name?   s    z#JsonSchemaEvaluator.evaluation_name)noder   c                 C   s:   t |trt|S t|dr6tt|dr6t|d S |S )Nschema)
isinstancestrr   hasattrcallablegetattr)r   r   r   r   r   _parse_jsonD   s
    
zJsonSchemaEvaluator._parse_json)
predictionr   r   c              
   C   s^   ddl m}m} z|||d ddiW S  |yX } zdt|dW  Y d }~S d }~0 0 d S )Nr   )ValidationErrorvalidate)instancer   scoreTF)r"   Z	reasoning)r   r   r    repr)r   r   r   r   r    er   r   r   	_validateL   s    zJsonSchemaEvaluator._validate)r   input	referencer   r   c                 K   s    |  |}|  |}| ||S )N)r   r%   )r   r   r&   r'   r   Zparsed_predictionr   r   r   r   _evaluate_stringsW   s    

z%JsonSchemaEvaluator._evaluate_strings)NN)__name__
__module____qualname____doc__r   r
   propertyboolr   r   r   r   r   dictlistfloatintr   r%   r(   __classcell__r   r   r   r   r      s&   "  


r   N)typingr   r   Zlangchain_core.utils.jsonr   Zlangchain.evaluation.schemar   r   r   r   r   r   <module>   s   