a
    •˜ëjã  ã                   @   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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e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)Ú_Úreturnc              
      sN   t ƒ  ¡  zddl}W n2 tyH } zd}t|ƒ|‚W Y d}~n
d}~0 0 dS )zƒInitializes the JsonSchemaEvaluator.

        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__© úg/mnt/bigstor/www/usecases/.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_input4   s    z"JsonSchemaEvaluator.requires_inputc                 C   s   dS )z1Returns whether the evaluator requires reference.Tr   r   r   r   r   Úrequires_reference9   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   sN   t |tƒrt|ƒS t|dƒr.t|jƒr.| ¡ S t|dƒrJt|jƒrJ| ¡ S |S )NÚmodel_json_schemaÚschema)Ú
isinstanceÚstrr   ÚhasattrÚcallabler   r   )r   r   r   r   r   Ú_parse_jsonC   s    
zJsonSchemaEvaluator._parse_json)Ú
predictionr   r	   c              
   C   s\   ddl m}m} z|||d� W n2 |yR } zdt|ƒdœW  Y d }~S d }~0 0 ddiS )Nr   )ÚValidationErrorÚvalidate)Úinstancer   F)ÚscoreZ	reasoningr%   T)r   r"   r#   Úrepr)r   r!   r   r"   r#   r   r   r   r   Ú	_validateN   s    $zJsonSchemaEvaluator._validate)r!   ÚinputÚ	referenceÚkwargsr	   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   r+   Ú__classcell__r   r   r   r   r   	   s(   "	  ü


úr   N)
Útypingr   r   Zlangchain_core.utils.jsonr   Ztyping_extensionsr   Zlangchain.evaluation.schemar   r   r   r   r   r   Ú<module>   s   