
    h)                     N    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e      Z	y)    )AnyUnion)parse_json_markdown)override)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e	 	 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                 j    t         |           	 ddl}y# t        $ r}d}t        |      |d}~w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   emsg	__class__s        f/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain/evaluation/parsing/json_schema.pyr   zJsonSchemaEvaluator.__init__$   sB     		* 	*D  c")	*s    	2-2c                      y)z-Returns whether the evaluator requires input.F r   s    r   requires_inputz"JsonSchemaEvaluator.requires_input7   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_nameA   s     (r   nodec                     t        |t              rt        |      S t        |d      r%t	        |j
                        r|j                         S t        |d      r%t	        |j                        r|j                         S |S )Nmodel_json_schemaschema)
isinstancestrr   hasattrcallabler"   r#   )r   r    s     r   _parse_jsonzJsonSchemaEvaluator._parse_jsonF   sd    dC &t,,4,-(4;Q;Q2R))++4"x'<;;= 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#   F)score	reasoningr.   T)r   r+   r,   repr)r   r)   r#   r+   r,   r   s         r   	_validatezJsonSchemaEvaluator._validateQ   sA    8	:j8   	:"a99	:s   
 5055input	referencec                 j    | j                  |      }| j                  |      }| j                  ||      S )N)r(   r1   )r   r)   r2   r3   r
   parsed_predictionr#   s          r   _evaluate_stringsz%JsonSchemaEvaluator._evaluate_stringsZ   s7     !,,Z8!!),~~/88r   )NN)__name__
__module____qualname____doc__r   r   propertyboolr   r   r%   r   r   dictlistfloatintr(   r1   r   r6   __classcell__)r   s   @r   r	   r	   	   s   4* * *&    D   ( ( (	 	dD$tSRU.U(V 	C     "&%)		9#s(O	9 S#X	9 c?		9
 	9 
	9 	9r   r	   N)
typingr   r   langchain_core.utils.jsonr   typing_extensionsr   langchain.evaluation.schemar   r	   r   r   r   <module>rF      s     9 & 7[9/ [9r   