Skip to content

Commit

Permalink
Fix/hhem: divide by zero error (explodinggradients#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjmachan authored and shahules786 committed Dec 16, 2024
1 parent 0b3b9e4 commit 4d99bf6
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/ragas/metrics/_faithfulness.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,7 @@ async def _create_statements(

text, question = row["response"], row["user_input"]
sentences = self.sentence_segmenter.segment(text)
sentences_with_index = {
i: sentence
for i, sentence in enumerate(sentences)
if sentence.strip().endswith((".", "。", "!", "!"))
}
sentences_with_index = {i: sentence for i, sentence in enumerate(sentences)}

statements_simplified = await self.statement_prompt.generate(
llm=self.llm,
Expand Down Expand Up @@ -320,7 +316,7 @@ async def _ascore(self, row: t.Dict, callbacks: Callbacks) -> float:
assert self.llm is not None, "LLM is not set"

statements_simplified = await self._create_statements(row, callbacks)
if statements_simplified is None:
if len(statements_simplified.sentences) == 0:
return np.nan

statements = []
Expand All @@ -334,7 +330,9 @@ async def _ascore(self, row: t.Dict, callbacks: Callbacks) -> float:
batch_scores = (
self.nli_classifier.predict(input_pairs).cpu().detach().round()
)
scores += batch_scores
# convert tensor to list of floats
scores.extend(batch_scores.tolist())

return sum(scores) / len(scores)


Expand Down

0 comments on commit 4d99bf6

Please sign in to comment.