[docs]@autorag_metric(fields_to_check=["retrieval_gt","retrieved_ids"])defretrieval_f1(metric_input:MetricInput):""" Compute f1 score for retrieval. :param metric_input: The MetricInput schema for AutoRAG metric. :return: The f1 score. """recall_score=retrieval_recall.__wrapped__(metric_input)precision_score=retrieval_precision.__wrapped__(metric_input)ifrecall_score+precision_score==0:return0else:return2*(recall_score*precision_score)/(recall_score+precision_score)
[docs]@autorag_metric(fields_to_check=["retrieval_gt","retrieved_ids"])defretrieval_mrr(metric_input:MetricInput)->float:""" Reciprocal Rank (RR) is the reciprocal of the rank of the first relevant item. Mean of RR in whole queries is MRR. """gt,pred=metric_input.retrieval_gt,metric_input.retrieved_ids# Flatten the ground truth list of lists into a single set of relevant documentsgt_sets=[frozenset(g)forgingt]rr_list=[]forgt_setingt_sets:fori,pred_idinenumerate(pred):ifpred_idingt_set:rr_list.append(1.0/(i+1))breakreturnsum(rr_list)/len(gt_sets)ifrr_listelse0.0
[docs]@autorag_metric(fields_to_check=["retrieval_gt","retrieved_ids"])defretrieval_map(metric_input:MetricInput)->float:""" Mean Average Precision (MAP) is the mean of Average Precision (AP) for all queries. """gt,pred=metric_input.retrieval_gt,metric_input.retrieved_idsgt_sets=[frozenset(g)forgingt]ap_list=[]forgt_setingt_sets:pred_hits=[1ifpred_idingt_setelse0forpred_idinpred]precision_list=[sum(pred_hits[:i+1])/(i+1)fori,hitinenumerate(pred_hits)ifhit==1]ap_list.append(sum(precision_list)/len(precision_list)ifprecision_listelse0.0)returnsum(ap_list)/len(gt_sets)ifap_listelse0.0