From ae50d459904759e08ad926a7b598d0d15efaf647 Mon Sep 17 00:00:00 2001 From: Jithin James Date: Wed, 11 Sep 2024 18:28:47 +0530 Subject: [PATCH] fix: make score nested if loop_is_running (#1276) --- src/ragas/metrics/base.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/ragas/metrics/base.py b/src/ragas/metrics/base.py index e14d129b9..c94c8cf66 100644 --- a/src/ragas/metrics/base.py +++ b/src/ragas/metrics/base.py @@ -17,6 +17,7 @@ from ragas.callbacks import new_group from ragas.dataset_schema import MultiTurnSample, SingleTurnSample +from ragas.executor import is_event_loop_running from ragas.run_config import RunConfig from ragas.utils import deprecated @@ -59,8 +60,7 @@ class Metric(ABC): @property @abstractmethod - def name(self) -> str: - ... + def name(self) -> str: ... @property def required_columns(self) -> t.Dict[str, t.Set[str]]: @@ -103,6 +103,15 @@ def score(self: t.Self, row: t.Dict, callbacks: Callbacks = None) -> float: callbacks = callbacks or [] rm, group_cm = new_group(self.name, inputs=row, callbacks=callbacks) try: + if is_event_loop_running(): + try: + import nest_asyncio + + nest_asyncio.apply() + except ImportError: + raise ImportError( + "It seems like your running this in a jupyter-like environment. Please install nest_asyncio with `pip install nest_asyncio` to make it work." + ) loop = asyncio.get_event_loop() score = loop.run_until_complete(self._ascore(row=row, callbacks=group_cm)) except Exception as e: @@ -138,8 +147,7 @@ async def ascore( return score @abstractmethod - async def _ascore(self, row: t.Dict, callbacks: Callbacks) -> float: - ... + async def _ascore(self, row: t.Dict, callbacks: Callbacks) -> float: ... @dataclass @@ -246,8 +254,7 @@ async def _single_turn_ascore( self, sample: SingleTurnSample, callbacks: Callbacks, - ) -> float: - ... + ) -> float: ... class MultiTurnMetric(Metric): @@ -299,8 +306,7 @@ async def _multi_turn_ascore( self, sample: MultiTurnSample, callbacks: Callbacks, - ) -> float: - ... + ) -> float: ... class Ensember: