
    9|hj                     B    d dl mZmZ d dlmZ d dlmZ  G d de      Zy)    )AnyUnion)parse_json_markdown)StringEvaluatorc                        e Zd ZdZdeddf fdZedefd       Zedefd       Z	ede
fd       Zd	edeeedeeee
f   fd
ZdededefdZ	 	 ddee
ef   dee
ef   dee
ef   dedef
dZ x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                 \    t         |           	 ddl}y# t        $ r t        d      w xY 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   	__class__s      g/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain/evaluation/parsing/json_schema.pyr   zJsonSchemaEvaluator.__init__#   s:     		 	D 	s    +c                      y)z-Returns whether the evaluator requires input.F r   s    r   requires_inputz"JsonSchemaEvaluator.requires_input5   s         c                      y)z1Returns whether the evaluator requires reference.Tr   r   s    r   requires_referencez&JsonSchemaEvaluator.requires_reference:   s     r   c                      y)z#Returns the name of the evaluation.json_schema_validationr   r   s    r   evaluation_namez#JsonSchemaEvaluator.evaluation_name?   s     (r   nodec                     t        |t              rt        |      S t        |d      r&t	        t        |d            r t        |d             S |S )Nschema)
isinstancestrr   hasattrcallablegetattr)r   r   s     r   _parse_jsonzJsonSchemaEvaluator._parse_jsonD   sF    dC &t,,T8$'$2I)J*74*,,r   
predictionr   c                 p    ddl m}m} 	  |||       ddiS # |$ r}dt        |      dcY d }~S d }~ww xY w)Nr   )ValidationErrorvalidate)instancer   scoreTF)r+   	reasoning)r   r(   r)   repr)r   r&   r   r(   r)   es         r   	_validatezJsonSchemaEvaluator._validateL   sD    8	:j8   	:"a99	:s    5055input	referencec                 j    | j                  |      }| j                  |      }| j                  ||      S )N)r%   r/   )r   r&   r0   r1   r	   parsed_predictionr   s          r   _evaluate_stringsz%JsonSchemaEvaluator._evaluate_stringsW   s7     !,,Z8!!),~~/88r   )NN)__name__
__module____qualname____doc__r   r   propertyboolr   r   r!   r   r   dictlistfloatintr%   r/   r4   __classcell__)r   s   @r   r   r      s   4  $    D   ( ( ( dD$tSRU.U(V 	:C 	: 	: 	: "&%)		9#s(O	9 S#X	9 c?		9
 	9 
	9r   r   N)typingr   r   langchain_core.utils.jsonr   langchain.evaluation.schemar   r   r   r   r   <module>rC      s     9 7X9/ X9r   