o
    Jjgj                     @   s<   d dl mZ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)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		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                    s0   t    zddl}W dS  ty   td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__ `/var/www/html/zoom/venv/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_input5      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.json_schema_validationr   r   r   r   r   evaluation_name?   r   z#JsonSchemaEvaluator.evaluation_namenodec                 C   s:   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   r   r   r   _parse_jsonD   s
   
zJsonSchemaEvaluator._parse_json
predictionr   c              
   C   sZ   ddl m}m} z|||d ddiW S  |y, } zdt|dW  Y d }~S d }~ww )Nr   )ValidationErrorvalidate)instancer   scoreTF)r%   	reasoning)r   r"   r#   repr)r   r!   r   r"   r#   er   r   r   	_validateL   s   zJsonSchemaEvaluator._validateinput	referencec                 K   s    |  |}|  |}| ||S )N)r    r)   )r   r!   r*   r+   r   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-   __classcell__r   r   r   r   r      s0    $


r   N)typingr   r   langchain_core.utils.jsonr   langchain.evaluation.schemar   r   r   r   r   r   <module>   s    