Skip to content

Commit 6a88465

Browse files
authored
fix: proper error message and fixed hf embedding error (#561)
1 parent c520f98 commit 6a88465

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

src/ragas/embeddings/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
from ragas.embeddings.base import BaseRagasEmbeddings, HuggingfaceEmbeddings
1+
from ragas.embeddings.base import (
2+
BaseRagasEmbeddings,
3+
HuggingfaceEmbeddings,
4+
LangchainEmbeddingsWrapper,
5+
)
26

37
__all__ = [
48
"HuggingfaceEmbeddings",
59
"BaseRagasEmbeddings",
10+
"LangchainEmbeddingsWrapper",
611
]

src/ragas/evaluation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ragas.metrics.base import Metric, MetricWithEmbeddings, MetricWithLLM
1717
from ragas.metrics.critique import AspectCritique
1818
from ragas.run_config import RunConfig
19+
from ragas.exceptions import ExceptionInRunner
1920

2021
# from ragas.metrics.critique import AspectCritique
2122
from ragas.validation import (
@@ -202,6 +203,8 @@ def evaluate(
202203
try:
203204
# get the results
204205
results = executor.results()
206+
if results == []:
207+
raise ExceptionInRunner()
205208

206209
# convert results to dataset_like
207210
for i, _ in enumerate(dataset):

src/ragas/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ def __init__(self, evolution: Evolution):
2525
self.evolution = evolution
2626
msg = f"Max retries exceeded for evolution {evolution.__class__.__name__}."
2727
super().__init__(msg)
28+
29+
30+
class ExceptionInRunner(RagasException):
31+
"""
32+
Exception raised when an exception is raised in the executor.
33+
"""
34+
35+
def __init__(self):
36+
msg = "The runner thread which was running the jobs raised an exeception. Read the traceback above to debug it. You can also pass `raise_exception=False` incase you want to show only a warning message instead."
37+
super().__init__(msg)

src/ragas/executor.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import typing as t
66
from dataclasses import dataclass, field
7-
from threading import Thread
7+
import threading
88

99
import numpy as np
1010
from tqdm.auto import tqdm
@@ -14,7 +14,16 @@
1414
logger = logging.getLogger(__name__)
1515

1616

17-
class Runner(Thread):
17+
def runner_exception_hook(args: threading.ExceptHookArgs):
18+
print(args)
19+
raise args.exc_type
20+
21+
22+
# set a custom exception hook
23+
# threading.excepthook = runner_exception_hook
24+
25+
26+
class Runner(threading.Thread):
1827
def __init__(
1928
self,
2029
jobs: t.List[t.Tuple[t.Coroutine, str]],

src/ragas/metrics/_answer_similarity.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,29 @@ async def _ascore(
5454
) -> float:
5555
assert self.embeddings is not None, "embeddings must be set"
5656

57-
ground_truth, answers = row["ground_truth"], row["answer"]
58-
ground_truth = [item[0] for item in ground_truth]
57+
ground_truth = t.cast(str, row["ground_truth"])
58+
answer = t.cast(str, row["answer"])
5959

6060
if self.is_cross_encoder and isinstance(self.embeddings, HuggingfaceEmbeddings):
6161
raise NotImplementedError(
6262
"async score [ascore()] not implemented for HuggingFace embeddings"
6363
)
6464
else:
65-
embeddings_1 = np.array(await self.embeddings.embed_texts(ground_truth))
66-
embeddings_2 = np.array(await self.embeddings.embed_texts(answers))
65+
embedding_1 = np.array(await self.embeddings.embed_text(ground_truth))
66+
embedding_2 = np.array(await self.embeddings.embed_text(answer))
6767
# Normalization factors of the above embeddings
68-
norms_1 = np.linalg.norm(embeddings_1, axis=1, keepdims=True)
69-
norms_2 = np.linalg.norm(embeddings_2, axis=1, keepdims=True)
70-
embeddings_1_normalized = embeddings_1 / norms_1
71-
embeddings_2_normalized = embeddings_2 / norms_2
72-
similarity = embeddings_1_normalized @ embeddings_2_normalized.T
73-
if similarity.size == 1:
74-
scores = similarity.flatten()
75-
else:
76-
scores = np.diagonal(similarity)
77-
78-
assert isinstance(scores, np.ndarray), "Expects ndarray"
68+
norms_1 = np.linalg.norm(embedding_1, keepdims=True)
69+
norms_2 = np.linalg.norm(embedding_2, keepdims=True)
70+
embedding_1_normalized = embedding_1 / norms_1
71+
embedding_2_normalized = embedding_2 / norms_2
72+
similarity = embedding_1_normalized @ embedding_2_normalized.T
73+
score = similarity.flatten()
74+
75+
assert isinstance(score, np.ndarray), "Expects ndarray"
7976
if self.threshold:
80-
scores = scores >= self.threshold
77+
score = score >= self.threshold
8178

82-
return scores.tolist()[0]
79+
return score.tolist()[0]
8380

8481

8582
answer_similarity = AnswerSimilarity()

0 commit comments

Comments
 (0)