
    j+                         d dl mZmZmZmZmZ ddlmZ ddlm	Z	m
Z
mZmZ  G d dej                  e
j                        Zy)	    )ListCallableAnyDictOptional   )types   )arguments_helpersarguments_validatorbase_metricscore_resultc                        e Zd ZdZ	 	 ddedeej                     deee	j                     ge	j                  f   dedee   f
 fdZd	ed
e	j                  fdZdeeef   deej$                     d
dfdZ xZS )AggregatedMetrica  
    Combine the output of multiple metrics into a single aggregated ``ScoreResult``.

    Each metric in ``metrics`` is executed with the provided scoring kwargs, then the
    ``aggregator`` callback decides how to merge the individual results. This is
    handy for building ensembles such as min/max, weighted averages, or custom
    pass/fail checks without re-implementing the metrics themselves.

    Args:
        name: Display name for the aggregated metric result.
        metrics: Ordered list of metric instances that should be executed.
        aggregator: Callable receiving the list of ``ScoreResult`` objects and
            returning the final aggregated ``ScoreResult``.
        track: Whether to automatically track the metric in Opik. Defaults to
            ``True``.
        project_name: Optional tracking project used when no parent context exists.

    Example:
        >>> from opik.evaluation.metrics import AggregatedMetric, Contains, RegexMatch
        >>> metrics = [Contains(track=False), RegexMatch(pattern=r"\d+", track=False)]
        >>> from opik.evaluation.metrics import score_result
        >>> def combine(results):
        ...     score = sum(result.value for result in results) / len(results)
        ...     return score_result.ScoreResult(
        ...         name="combined_contains_regex",
        ...         value=score,
        ...         reason="Average of contains and regex checks",
        ...     )
        >>> metric = AggregatedMetric(
        ...     name="combined_contains_regex",
        ...     metrics=metrics,
        ...     aggregator=combine,
        ... )
        >>> response = "Order number 12345 confirmed"
        >>> result = metric.score(output=response, reference="order")
        >>> float(result.value)  # doctest: +SKIP
        1.0
    Nnamemetrics
aggregatortrackproject_namec                     t         |   |||       || _        || _        | j                  t	        | j                        dk(  rt        d      |t        d      y )N)r   r   r   r   zNo metrics providedzNo aggregator provided)super__init__r   r   len
ValueError)selfr   r   r   r   r   	__class__s         /Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/metrics/aggregated_metric.pyr   zAggregatedMetric.__init__2   sd     	d%lK$<<3t||#4#9233566     kwargsreturnc                     g }| j                   D ]G  } |j                  di |}t        |t              r|j	                  |       7|j                  |       I | j                  |      S )N )r   score
isinstancelistextendappendr   )r   r   score_resultsmetricmetric_results        r   r#   zAggregatedMetric.scoreF   s_    8:llF(FLL262M-.$$]3$$]3 # }--r   score_kwargskey_mappingc                     | j                   D ]/  }t        j                  |j                  |j                  ||       1 y )N)score_function
score_namer   scoring_key_mapping)r   r   $raise_if_score_arguments_are_missingr#   r   )r   r+   r,   r)   s       r   validate_score_argumentsz)AggregatedMetric.validate_score_argumentsQ   s4    
 llFBB%||!;;#$/	 #r   )TN)__name__
__module____qualname____doc__strr   r   
BaseMetricr   r   ScoreResultboolr   r   r   r#   r   evaluation_typesScoringKeyMappingTyper2   __classcell__)r   s   @r   r   r      s    %\ &*77 k,,-7 ,**+,l.F.FF
	7 7 sm7(	.c 	.l&>&> 	.38n .DDE 
	r   r   N)typingr   r   r   r   r    r	   r;   r   r   r   r   r8   ScoreArgumentsValidatorr   r"   r   r   <module>rA      s2    6 6 ( O OT/GGTr   