
    Ngj                     J    d dl mZmZ d dlmZ d dlmZ  G d de          ZdS )    )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}dS # 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      d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/evaluation/parsing/json_schema.pyr   zJsonSchemaEvaluator.__init__#   se     		 	 	 	D  	s	   ) Ac                     dS )z-Returns whether the evaluator requires input.F r   s    r   requires_inputz"JsonSchemaEvaluator.requires_input5   s	     u    c                     dS )z1Returns whether the evaluator requires reference.Tr   r   s    r   requires_referencez&JsonSchemaEvaluator.requires_reference:   s	     tr   c                     dS )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          r5t	          t          |d                    r t          |d                      S |S )Nschema)
isinstancestrr   hasattrcallablegetattr)r   r   s     r   _parse_jsonzJsonSchemaEvaluator._parse_jsonD   sj    dC   	-&t,,,T8$$ 	-'$2I2I)J)J 	-*74**,,,r   
predictionr   c                 ~    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   s    88888888	:Hj8888   	: 	: 	:"a99999999	:s    <7<<input	referencec                     |                      |          }|                      |          }|                     ||          S )N)r%   r/   )r   r&   r0   r1   r	   parsed_predictionr   s          r   _evaluate_stringsz%JsonSchemaEvaluator._evaluate_stringsW   s@     !,,Z88!!),,~~/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       $     X D    X ( ( ( ( X( dD$tSRU.U(V    	:C 	: 	: 	: 	: 	: 	: "&%)		9 	9#s(O	9 S#X	9 c?		9
 	9 
	9 	9 	9 	9 	9 	9 	9 	9r   r   N)typingr   r   langchain_core.utils.jsonr   langchain.evaluation.schemar   r   r   r   r   <module>rC      s            9 9 9 9 9 9 7 7 7 7 7 7X9 X9 X9 X9 X9/ X9 X9 X9 X9 X9r   