Skip to content
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

feat: remove examples #1708

Merged
merged 7 commits into from
Nov 29, 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
18 changes: 15 additions & 3 deletions src/ragas/metrics/_aspect_critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ def __init__(
llm=llm,
)

self.definition = definition
self._definition = definition
self.single_turn_prompt = single_turn_prompt or SingleTurnAspectCriticPrompt()
self.multi_turn_prompt = multi_turn_prompt or MultiTurnAspectCriticPrompt()
self.max_retries = max_retries

# update the instruction for the prompts with the definition
instruction = f"Evaluate the Input based on the criterial defined. Use only 'Yes' (1) and 'No' (0) as verdict.\nCriteria Definition: {self.definition}"
instruction = f"Evaluate the Input based on the criterial defined. Use only 'Yes' (1) and 'No' (0) as verdict.\nCriteria Definition: {self._definition}"
self.single_turn_prompt.instruction = instruction
self.multi_turn_prompt.instruction = instruction

Expand All @@ -135,7 +135,19 @@ def __init__(
)

def __repr__(self) -> str:
return f"{self.name}(definition='{self.definition}', required_columns={self.required_columns}, llm={self.llm})"
return f"{self.name}(definition='{self._definition}', required_columns={self.required_columns}, llm={self.llm})"

@property
def definition(self) -> str:
return self._definition

@definition.setter
def definition(self, value: str) -> None:
self._definition = value
# Update the instruction for both prompts with the new definition
instruction = f"Evaluate the Input based on the criterial defined. Use only 'Yes' (1) and 'No' (0) as verdict.\nCriteria Definition: {self._definition}"
self.single_turn_prompt.instruction = instruction
self.multi_turn_prompt.instruction = instruction

def _compute_score(
self, safe_loaded_responses: t.List[AspectCriticOutput]
Expand Down
18 changes: 15 additions & 3 deletions src/ragas/metrics/_simple_criteria.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ def __init__(
_required_columns=required_columns,
)

self.definition = definition
self._definition = definition
self.single_turn_prompt = single_turn_prompt or SingleTurnSimpleCriteriaPrompt()
self.multi_turn_prompt = multi_turn_prompt or MultiTurnSimpleCriteriaPrompt()

# update the instruction for the prompts with the definition
instruction = f"Evaluate the Input based on the criterial defined. Give a score between 0 and 5.\nCriteria Definition: {self.definition}"
instruction = f"Evaluate the Input based on the criterial defined. Give a score between 0 and 5.\nCriteria Definition: {self._definition}"
self.single_turn_prompt.instruction = instruction
self.multi_turn_prompt.instruction = instruction

Expand All @@ -134,7 +134,19 @@ def __init__(
)

def __repr__(self) -> str:
return f"{self.name}(required_columns={self.required_columns}, llm={self.llm}, definition={self.definition})"
return f"{self.name}(required_columns={self.required_columns}, llm={self.llm}, definition={self._definition})"

@property
def definition(self) -> str:
return self._definition

@definition.setter
def definition(self, value: str) -> None:
self._definition = value
# Update the instruction for both prompts with the new definition
instruction = f"Evaluate the Input based on the criterial defined. Give a score between 0 and 5.\nCriteria Definition: {self._definition}"
self.single_turn_prompt.instruction = instruction
self.multi_turn_prompt.instruction = instruction

def _compute_score(
self, safe_loaded_responses: t.List[SimpleCriteriaOutput]
Expand Down
5 changes: 3 additions & 2 deletions src/ragas/testset/synthesizers/testset_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TestsetPacket(BaseModel):
"""

samples_original: t.List[TestsetSample]
run_id: str = Field(default_factory=lambda: str(uuid4()))
run_id: str
created_at: str = Field(default_factory=lambda: datetime.now().isoformat())


Expand All @@ -56,6 +56,7 @@ class Testset(RagasDataset[TestsetSample]):
"""

samples: t.List[TestsetSample]
run_id: str = field(default_factory=lambda: str(uuid4()), repr=False, compare=False)
cost_cb: t.Optional[CostCallbackHandler] = field(default=None, repr=False)

def to_evaluation_dataset(self) -> EvaluationDataset:
Expand Down Expand Up @@ -137,7 +138,7 @@ def total_cost(
def upload(self, base_url: str = RAGAS_API_URL, verbose: bool = True) -> str:
import requests

packet = TestsetPacket(samples_original=self.samples)
packet = TestsetPacket(samples_original=self.samples, run_id=self.run_id)
response = requests.post(
f"{base_url}/alignment/testset", json=packet.model_dump()
)
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_metric.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import typing as t
from dataclasses import dataclass, field

import pytest

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import AspectCritic, SimpleCriteriaScore
from ragas.metrics.base import MetricType


Expand Down Expand Up @@ -89,3 +92,20 @@ async def _single_turn_ascore(self, sample: SingleTurnSample, callbacks):
user_input="a", response="b", retrieved_contexts=["c"]
).to_dict()
)


@pytest.mark.parametrize("metric", [AspectCritic, SimpleCriteriaScore])
def test_metrics_with_definition(metric):
"""
Test the general metrics like AspectCritic, SimpleCriteriaScore
"""

m = metric(name="metric", definition="test")

# check if the definition is set
assert m.definition == "test"

# check if the definition is updated and the instruction along with it
m.definition = "this is a new definition"
assert m.definition == "this is a new definition"
assert "this is a new definition" in m.single_turn_prompt.instruction
Loading