Skip to content

feat: Unify the use of sentence_segmenter #1629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/ragas/metrics/_bleu_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BleuScore(SingleTurnMetric):
)
weights: t.Tuple[float, ...] = (0.25, 0.25, 0.25, 0.25)
sentence_segmenter: t.Optional[HasSegmentMethod] = None
language: str = "english"

def __post_init__(self):
try:
Expand All @@ -26,7 +27,8 @@ def __post_init__(self):
raise ImportError(
"nltk is required for bleu score. Please install it using `pip install nltk`"
)
self.segmenter = get_segmenter()
if not self.sentence_segmenter:
self.sentence_segmenter = get_segmenter(language=self.language, clean=False)
self.word_tokenizer = word_tokenize
self.corpus_bleu = corpus_bleu

Expand All @@ -36,8 +38,13 @@ def init(self, run_config: RunConfig):
async def _single_turn_ascore(
self, sample: SingleTurnSample, callbacks: Callbacks
) -> float:
reference_sentences = self.segmenter.segment(sample.reference)
response_sentences = self.segmenter.segment(sample.response)

assert (
self.sentence_segmenter is not None
), "Sentence segmenter is not initialized"

reference_sentences = self.sentence_segmenter.segment(sample.reference)
response_sentences = self.sentence_segmenter.segment(sample.response)

reference = [
[self.word_tokenizer(reference)] for reference in reference_sentences
Expand Down
17 changes: 14 additions & 3 deletions src/ragas/metrics/_factual_correctness.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from numpy.typing import NDArray
from pydantic import BaseModel, Field

from ragas.metrics._faithfulness import NLIStatementInput, NLIStatementPrompt
from ragas.metrics._faithfulness import (
HasSegmentMethod,
NLIStatementInput,
NLIStatementPrompt,
)
from ragas.metrics.base import (
MetricType,
MetricWithLLM,
Expand Down Expand Up @@ -212,6 +216,8 @@ class FactualCorrectness(MetricWithLLM, SingleTurnMetric):
coverage: t.Literal["low", "high"] = "low"
claim_decomposition_prompt: PydanticPrompt = ClaimDecompositionPrompt()
nli_prompt: PydanticPrompt = NLIStatementPrompt()
sentence_segmenter: t.Optional[HasSegmentMethod] = None
language: str = "english"

def __post_init__(self):
value = f"{self.atomicity}_atomicity_{self.coverage}_coverage"
Expand All @@ -224,7 +230,8 @@ def __post_init__(self):
logger.warning(
f"No examples found for the atomicity and coverage level: {value}"
)
self.segmenter = get_segmenter(language="english")
if not self.sentence_segmenter:
self.sentence_segmenter = get_segmenter(language=self.language, clean=False)

if type(self.beta) is not float:
raise ValueError(
Expand All @@ -235,7 +242,11 @@ async def decompose_claims(
self, response: str, callbacks: Callbacks
) -> t.List[str]:
assert self.llm is not None, "LLM must be set"
sentences = self.segmenter.segment(response)
assert (
self.sentence_segmenter is not None
), "Sentence segmenter is not initialized"

sentences = self.sentence_segmenter.segment(response)
assert isinstance(sentences, list), "Segmenter must return a list of sentences"
prompt_input = ClaimDecompositionInput(response=response, sentences=sentences)
result = await self.claim_decomposition_prompt.generate(
Expand Down
Loading