11"""Ragas metrics evaluation using LLM Manager with Ragas 0.4+ API."""
22
33import errno
4+ import logging
45import math
56import threading
67from typing import Any , Optional
2829from lightspeed_evaluation .core .models import EvaluationScope , TurnData
2930from lightspeed_evaluation .core .system .exceptions import EvaluationError
3031
32+ logger = logging .getLogger (__name__ )
33+
34+ _DEPRECATED_RAGAS_METRICS : dict [str , str ] = {
35+ "context_precision_with_reference" : "context_precision" ,
36+ "context_precision_without_reference" : "context_utilization" ,
37+ }
38+
3139
3240def _clamp_score (score : float ) -> float :
3341 """Clamp score to [0, 1] range to handle floating-point precision issues.
@@ -88,10 +96,11 @@ def __init__(self, llm_manager: LLMManager, embedding_manager: EmbeddingManager)
8896 # Context/Retrieval evaluation metrics
8997 "context_recall" : self ._evaluate_context_recall ,
9098 "context_relevance" : self ._evaluate_context_relevance ,
91- "context_precision_with_reference" : self ._evaluate_context_precision_with_reference ,
92- "context_precision_without_reference" : (
93- self ._evaluate_context_precision_without_reference
94- ),
99+ "context_precision" : self ._evaluate_context_precision ,
100+ "context_utilization" : self ._evaluate_context_utilization ,
101+ # Deprecated aliases (backward compatibility)
102+ "context_precision_with_reference" : self ._evaluate_context_precision ,
103+ "context_precision_without_reference" : self ._evaluate_context_utilization ,
95104 }
96105
97106 @property
@@ -133,6 +142,14 @@ def evaluate(
133142 scope : EvaluationScope ,
134143 ) -> tuple [Optional [float ], str ]:
135144 """Evaluate a Ragas metric."""
145+ if metric_name in _DEPRECATED_RAGAS_METRICS :
146+ new_name = _DEPRECATED_RAGAS_METRICS [metric_name ]
147+ logger .warning (
148+ "Metric 'ragas:%s' is deprecated, use 'ragas:%s' instead." ,
149+ metric_name ,
150+ new_name ,
151+ )
152+
136153 if metric_name not in self .supported_metrics :
137154 return None , f"Unsupported Ragas metric: { metric_name } "
138155
@@ -220,16 +237,16 @@ def _evaluate_faithfulness(
220237 score = _clamp_score (float (result .value ))
221238 return score , f"Ragas faithfulness: { score :.2f} "
222239
223- def _evaluate_context_precision_without_reference (
240+ def _evaluate_context_utilization (
224241 self ,
225242 _conv_data : Any ,
226243 _turn_idx : Optional [int ],
227244 turn_data : Optional [TurnData ],
228245 is_conversation : bool ,
229246 ) -> tuple [Optional [float ], str ]:
230- """Evaluate context precision without reference using Ragas 0.4+ score() ."""
247+ """Evaluate context utilization using Ragas 0.4+ ContextUtilization ."""
231248 if is_conversation :
232- return None , "Context precision without reference is a turn-level metric"
249+ return None , "Context utilization is a turn-level metric"
233250
234251 query , response , contexts = self ._extract_turn_data (turn_data )
235252
@@ -242,21 +259,21 @@ def _evaluate_context_precision_without_reference(
242259 )
243260
244261 score = _clamp_score (float (result .value ))
245- return score , f"Ragas context precision without reference : { score :.2f} "
262+ return score , f"Ragas context utilization : { score :.2f} "
246263
247- def _evaluate_context_precision_with_reference (
264+ def _evaluate_context_precision (
248265 self ,
249266 _conv_data : Any ,
250267 _turn_idx : Optional [int ],
251268 turn_data : Optional [TurnData ],
252269 is_conversation : bool ,
253270 ) -> tuple [Optional [float ], str ]:
254- """Evaluate context precision with reference using Ragas 0.4+ score() ."""
271+ """Evaluate context precision using Ragas 0.4+ ContextPrecision ."""
255272 if is_conversation :
256- return None , "Context precision with reference is a turn-level metric"
273+ return None , "Context precision is a turn-level metric"
257274
258275 if turn_data is None :
259- return None , "TurnData is required for context precision with reference "
276+ return None , "TurnData is required for context precision"
260277
261278 query , _ , contexts = self ._extract_turn_data (turn_data )
262279
@@ -269,7 +286,7 @@ def _evaluate_context_precision_with_reference(
269286 )
270287
271288 score = _clamp_score (float (result .value ))
272- return score , f"Ragas context precision with reference : { score :.2f} "
289+ return score , f"Ragas context precision: { score :.2f} "
273290
274291 def _evaluate_context_recall (
275292 self ,
0 commit comments