Skip to content

Commit

Permalink
fix: make score nested if loop_is_running (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjmachan authored Sep 11, 2024
1 parent a4a3e56 commit ae50d45
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/ragas/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]]:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -246,8 +254,7 @@ async def _single_turn_ascore(
self,
sample: SingleTurnSample,
callbacks: Callbacks,
) -> float:
...
) -> float: ...


class MultiTurnMetric(Metric):
Expand Down Expand Up @@ -299,8 +306,7 @@ async def _multi_turn_ascore(
self,
sample: MultiTurnSample,
callbacks: Callbacks,
) -> float:
...
) -> float: ...


class Ensember:
Expand Down

0 comments on commit ae50d45

Please sign in to comment.