o
    `+ i)                     @   sH   d dl mZmZ d dl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)override)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edeeedeeee
f fddZdededefddZe		ddee
ef dee
ef dee
ef dedef
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

    kwargsreturnNc              
      s@   t    zddl}W dS  ty } zd}t||d}~ww )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   emsg	__class__ t/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/langchain/evaluation/parsing/json_schema.pyr   $   s   
	
zJsonSchemaEvaluator.__init__c                 C      dS )z-Returns whether the evaluator requires input.Fr   r   r   r   r   requires_input7      z"JsonSchemaEvaluator.requires_inputc                 C   r   )z1Returns whether the evaluator requires reference.Tr   r   r   r   r   requires_reference<   r   z&JsonSchemaEvaluator.requires_referencec                 C   r   )z#Returns the name of the evaluation.Zjson_schema_validationr   r   r   r   r   evaluation_nameA   r   z#JsonSchemaEvaluator.evaluation_namenodec                 C   sN   t |tr	t|S t|drt|jr| S t|dr%t|jr%| S |S )Nmodel_json_schemaschema)
isinstancestrr   hasattrcallabler   r   )r   r   r   r   r   _parse_jsonF   s   
zJsonSchemaEvaluator._parse_json
predictionr   c              
   C   sZ   ddl m}m} z|||d W ddiS  |y, } zdt|dW  Y d }~S d }~ww )Nr   )ValidationErrorvalidate)instancer   F)scoreZ	reasoningr'   T)r   r$   r%   repr)r   r#   r   r$   r%   r   r   r   r   	_validateQ   s   zJsonSchemaEvaluator._validateinput	referencec                 K   s    |  |}|  |}| ||S )N)r"   r)   )r   r#   r*   r+   r   Zparsed_predictionr   r   r   r   _evaluate_stringsZ   s   

z%JsonSchemaEvaluator._evaluate_strings)NN)__name__
__module____qualname____doc__r   r   propertyboolr   r   r   r   r   dictlistfloatintr"   r)   r   r,   __classcell__r   r   r   r   r   	   s2    $	


r   N)
typingr   r   Zlangchain_core.utils.jsonr   Ztyping_extensionsr   Zlangchain.evaluation.schemar   r   r   r   r   r   <module>   s
    