2121 from ragas_experimental .dataset import Dataset
2222
2323
24+ @dataclass
25+ class BaseMetric (ABC ):
26+ name : str
27+
28+ @abstractmethod
29+ def score (self , ** kwargs ) -> MetricResult :
30+ pass
31+
32+ @abstractmethod
33+ async def ascore (self , ** kwargs ) -> MetricResult :
34+ pass
35+
36+ def batch_score (
37+ self ,
38+ inputs : t .List [t .Dict [str , t .Any ]],
39+ ) -> t .List [MetricResult ]:
40+ return [self .score (** input_dict ) for input_dict in inputs ]
41+
42+ async def abatch_score (
43+ self ,
44+ inputs : t .List [t .Dict [str , t .Any ]],
45+ ) -> t .List [MetricResult ]:
46+ async_tasks = []
47+ for input_dict in inputs :
48+ # Process input asynchronously
49+ async_tasks .append (self .ascore (** input_dict ))
50+
51+ # Run all tasks concurrently and return results
52+ return await asyncio .gather (* async_tasks )
53+
54+
2455@dataclass
2556class Metric (ABC ):
2657 """Base class for all metrics in the LLM evaluation library."""
@@ -48,7 +79,12 @@ def get_variables(self) -> t.List[str]:
4879 def score (self , llm : RagasLLM , ** kwargs ) -> MetricResult :
4980 traces = {}
5081 traces ["input" ] = kwargs
82+
83+ # get prompt
84+ if not self .prompt :
85+ raise Exception ("prompt not passed" )
5186 prompt_input = self .prompt .format (** kwargs )
87+
5288 response = llm .generate (prompt_input , response_model = self ._response_model )
5389 traces ["output" ] = response .model_dump ()
5490 result = MetricResult (** response .model_dump ())
@@ -58,7 +94,11 @@ def score(self, llm: RagasLLM, **kwargs) -> MetricResult:
5894 async def ascore (self , llm : RagasLLM , ** kwargs ) -> MetricResult :
5995 traces = {}
6096
97+ # get prompt
98+ if not self .prompt :
99+ raise Exception ("prompt not passed" )
61100 prompt_input = self .prompt .format (** kwargs )
101+
62102 traces ["input" ] = prompt_input
63103 response = await llm .agenerate (
64104 prompt_input ,
@@ -137,11 +177,13 @@ def align(
137177 Align the metric with the specified experiments by different optimization methods.
138178 """
139179
140- assert isinstance (self .prompt , Prompt )
180+ # get prompt
181+ if not self .prompt :
182+ raise Exception ("prompt not passed" )
141183 self .prompt = DynamicFewShotPrompt .from_prompt (
142184 self .prompt , embedding_model , ** kwargs
143185 )
144- dataset .load ()
186+ dataset .reload ()
145187 total_items = len (dataset )
146188 input_vars = self .get_variables ()
147189 output_vars = [self .name , f"{ self .name } _reason" ]
@@ -188,7 +230,7 @@ def validate_alignment(
188230 for v in self .get_variables ()
189231 }
190232 score = self .score (llm = llm , ** values )
191- pred_scores .append (score .result )
233+ pred_scores .append (score .value )
192234
193235 df = test_dataset .to_pandas ()
194236 df [f"{ self .name } _pred" ] = pred_scores
0 commit comments