Skip to content

Commit 48b82ab

Browse files
authored
Easy fix, Groundedness metric, 5 retries early break. (#1935)
Easy fix Groundedness metric, 5 retries early break. Added logger to the 3 nv_metrics retries. Fixed input context max lenght to 7k to avoid 8k break.
1 parent 2bc29a2 commit 48b82ab

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/ragas/metrics/_nv_metrics.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class AnswerAccuracy(MetricWithLLM, SingleTurnMetric):
7979
"{answer1}: {sentence_true}\n\n"
8080
"Rating: "
8181
)
82-
retry = 5
82+
retry = 5 # Number of retries if rating is not in the first 8 tokens.
8383

8484
def process_score(self, response):
8585
for i in range(5):
@@ -214,6 +214,7 @@ class ContextRelevance(MetricWithLLM, SingleTurnMetric):
214214
"Do not try to explain.\n"
215215
"Based on the provided Question and Context, the Relevance score is ["
216216
)
217+
retry = 5 # Number of retries if rating is not in the first 8 tokens.
217218

218219
def process_score(self, response):
219220
for i in [2, 1, 0]:
@@ -247,11 +248,11 @@ async def _single_turn_ascore(
247248

248249
try:
249250
score0 = score1 = np.nan
250-
for retry in range(5):
251+
for retry in range(self.retry):
251252
formatted_prompt = StringPromptValue(
252253
text=self.template_relevance1.format(
253254
query=sample.user_input,
254-
context="\n".join(sample.retrieved_contexts)[:4192],
255+
context="\n".join(sample.retrieved_contexts)[:7000],
255256
)
256257
)
257258
req = self.llm.agenerate_text(
@@ -263,12 +264,14 @@ async def _single_turn_ascore(
263264
score0 = self.process_score(resp.generations[0][0].text)
264265
if score0 == score0:
265266
break
267+
else:
268+
logger.warning(f"Retry: {retry}")
266269

267-
for retry in range(5):
270+
for retry in range(self.retry):
268271
formatted_prompt = StringPromptValue(
269272
text=self.template_relevance1.format(
270273
query=sample.user_input,
271-
context="\n".join(sample.retrieved_contexts)[:4192],
274+
context="\n".join(sample.retrieved_contexts)[:7000],
272275
)
273276
)
274277
req = self.llm.agenerate_text(
@@ -280,6 +283,8 @@ async def _single_turn_ascore(
280283
score1 = self.process_score(resp.generations[0][0].text)
281284
if score1 == score1:
282285
break
286+
else:
287+
logger.warning(f"Retry: {retry}")
283288

284289
score = self.average_scores(score0, score1)
285290

@@ -343,6 +348,7 @@ class ResponseGroundedness(MetricWithLLM, SingleTurnMetric):
343348
"Do not explain."
344349
"Based on the provided context and response, the Groundedness score is:"
345350
)
351+
retry = 5 # Number of retries if rating is not in the first 8 tokens.
346352

347353
def process_score(self, response):
348354
for i in [2, 1, 0]:
@@ -376,10 +382,10 @@ async def _single_turn_ascore(
376382

377383
try:
378384
score0 = score1 = np.nan
379-
for retry in range(5):
385+
for retry in range(self.retry):
380386
formatted_prompt = StringPromptValue(
381387
text=self.template_groundedness1.format(
382-
context="\n".join(sample.retrieved_contexts)[:8192],
388+
context="\n".join(sample.retrieved_contexts)[:7000],
383389
response=sample.response,
384390
)
385391
)
@@ -390,11 +396,15 @@ async def _single_turn_ascore(
390396
)
391397
resp = await req
392398
score0 = self.process_score(resp.generations[0][0].text)
399+
if score0 == score0:
400+
break
401+
else:
402+
logger.warning(f"Retry: {retry}")
393403

394-
for retry in range(5):
404+
for retry in range(self.retry):
395405
formatted_prompt = StringPromptValue(
396406
text=self.template_groundedness2.format(
397-
context="\n".join(sample.retrieved_contexts)[:8192],
407+
context="\n".join(sample.retrieved_contexts)[:7000],
398408
response=sample.response,
399409
)
400410
)
@@ -405,6 +415,10 @@ async def _single_turn_ascore(
405415
)
406416
resp = await req
407417
score1 = self.process_score(resp.generations[0][0].text)
418+
if score1 == score1:
419+
break
420+
else:
421+
logger.warning(f"Retry: {retry}")
408422

409423
score = self.average_scores(score0, score1)
410424

0 commit comments

Comments
 (0)