
    jP                     V    d dl Z d dlmZmZ ddlmZmZ  G d dej                        Zy)    N)AnyOptional   )base_metricscore_resultc            	       j     e Zd ZdZ	 	 	 ddededee   ddf fdZded	ede	j                  fd
Z xZS )IsJsona  
    A metric that checks if a given output string is valid JSON.

    This metric returns a score of 1.0 if the output string can be parsed as JSON,
    and 0.0 otherwise.

    Args:
        name: The name of the metric. Defaults to "is_json_metric".
        track: Whether to track the metric. Defaults to True.
        project_name: Optional project name to track the metric in for the cases when there are no parent span/trace to inherit project name from.

    Example:
        >>> from opik.evaluation.metrics import IsJson
        >>> is_json_metric = IsJson()
        >>> result = is_json_metric.score('{"key": "value"}')
        >>> print(result.value)
        1.0
        >>> result = is_json_metric.score('Not a JSON string')
        >>> print(result.value)
        0.0
    Nnametrackproject_namereturnc                 *    t         |   |||       y )N)r
   r   r   )super__init__)selfr
   r   r   	__class__s       /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/metrics/heuristics/is_json.pyr   zIsJson.__init__   s     	d%lK    outputignored_kwargsc                     	 t        j                  |       t        j                  d| j                        S # t
        $ r$ t        j                  d| j                        cY S w xY w)a  
        Calculate the score based on whether the output string is valid JSON.

        Args:
            output: The output string to check for JSON validity.
            **ignored_kwargs: Additional keyword arguments that are ignored.

        Returns:
            score_result.ScoreResult: A ScoreResult object with a value of 1.0 if the output
                is valid JSON, 0.0 otherwise.
        g      ?)valuer
   g        )jsonloadsr   ScoreResultr
   	Exception)r   r   r   s      r   scorezIsJson.score&   sS    	GJJv++#DIIFF 	G++#DIIFF	Gs   58 *A%$A%)is_json_metricTN)__name__
__module____qualname____doc__strboolr   r   r   r   r   r   __classcell__)r   s   @r   r	   r	      sk    0 %&*	LL L sm	L
 
LGC G3 G<;S;S Gr   r	   )	r   typingr   r    r   r   
BaseMetricr	    r   r   <module>r*      s"       (/G[## /Gr   