Skip to content

Commit 09676ca

Browse files
authored
feat: remove examples (#1708)
1 parent 040616a commit 09676ca

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

src/ragas/metrics/_aspect_critic.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ def __init__(
118118
llm=llm,
119119
)
120120

121-
self.definition = definition
121+
self._definition = definition
122122
self.single_turn_prompt = single_turn_prompt or SingleTurnAspectCriticPrompt()
123123
self.multi_turn_prompt = multi_turn_prompt or MultiTurnAspectCriticPrompt()
124124
self.max_retries = max_retries
125125

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

@@ -135,7 +135,19 @@ def __init__(
135135
)
136136

137137
def __repr__(self) -> str:
138-
return f"{self.name}(definition='{self.definition}', required_columns={self.required_columns}, llm={self.llm})"
138+
return f"{self.name}(definition='{self._definition}', required_columns={self.required_columns}, llm={self.llm})"
139+
140+
@property
141+
def definition(self) -> str:
142+
return self._definition
143+
144+
@definition.setter
145+
def definition(self, value: str) -> None:
146+
self._definition = value
147+
# Update the instruction for both prompts with the new definition
148+
instruction = f"Evaluate the Input based on the criterial defined. Use only 'Yes' (1) and 'No' (0) as verdict.\nCriteria Definition: {self._definition}"
149+
self.single_turn_prompt.instruction = instruction
150+
self.multi_turn_prompt.instruction = instruction
139151

140152
def _compute_score(
141153
self, safe_loaded_responses: t.List[AspectCriticOutput]

src/ragas/metrics/_simple_criteria.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ def __init__(
118118
_required_columns=required_columns,
119119
)
120120

121-
self.definition = definition
121+
self._definition = definition
122122
self.single_turn_prompt = single_turn_prompt or SingleTurnSimpleCriteriaPrompt()
123123
self.multi_turn_prompt = multi_turn_prompt or MultiTurnSimpleCriteriaPrompt()
124124

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

@@ -134,7 +134,19 @@ def __init__(
134134
)
135135

136136
def __repr__(self) -> str:
137-
return f"{self.name}(required_columns={self.required_columns}, llm={self.llm}, definition={self.definition})"
137+
return f"{self.name}(required_columns={self.required_columns}, llm={self.llm}, definition={self._definition})"
138+
139+
@property
140+
def definition(self) -> str:
141+
return self._definition
142+
143+
@definition.setter
144+
def definition(self, value: str) -> None:
145+
self._definition = value
146+
# Update the instruction for both prompts with the new definition
147+
instruction = f"Evaluate the Input based on the criterial defined. Give a score between 0 and 5.\nCriteria Definition: {self._definition}"
148+
self.single_turn_prompt.instruction = instruction
149+
self.multi_turn_prompt.instruction = instruction
138150

139151
def _compute_score(
140152
self, safe_loaded_responses: t.List[SimpleCriteriaOutput]

src/ragas/testset/synthesizers/testset_schema.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TestsetPacket(BaseModel):
4040
"""
4141

4242
samples_original: t.List[TestsetSample]
43-
run_id: str = Field(default_factory=lambda: str(uuid4()))
43+
run_id: str
4444
created_at: str = Field(default_factory=lambda: datetime.now().isoformat())
4545

4646

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

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

6162
def to_evaluation_dataset(self) -> EvaluationDataset:
@@ -137,7 +138,7 @@ def total_cost(
137138
def upload(self, base_url: str = RAGAS_API_URL, verbose: bool = True) -> str:
138139
import requests
139140

140-
packet = TestsetPacket(samples_original=self.samples)
141+
packet = TestsetPacket(samples_original=self.samples, run_id=self.run_id)
141142
response = requests.post(
142143
f"{base_url}/alignment/testset", json=packet.model_dump()
143144
)

tests/unit/test_metric.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import typing as t
22
from dataclasses import dataclass, field
33

4+
import pytest
5+
46
from ragas.dataset_schema import SingleTurnSample
7+
from ragas.metrics import AspectCritic, SimpleCriteriaScore
58
from ragas.metrics.base import MetricType
69

710

@@ -89,3 +92,20 @@ async def _single_turn_ascore(self, sample: SingleTurnSample, callbacks):
8992
user_input="a", response="b", retrieved_contexts=["c"]
9093
).to_dict()
9194
)
95+
96+
97+
@pytest.mark.parametrize("metric", [AspectCritic, SimpleCriteriaScore])
98+
def test_metrics_with_definition(metric):
99+
"""
100+
Test the general metrics like AspectCritic, SimpleCriteriaScore
101+
"""
102+
103+
m = metric(name="metric", definition="test")
104+
105+
# check if the definition is set
106+
assert m.definition == "test"
107+
108+
# check if the definition is updated and the instruction along with it
109+
m.definition = "this is a new definition"
110+
assert m.definition == "this is a new definition"
111+
assert "this is a new definition" in m.single_turn_prompt.instruction

0 commit comments

Comments
 (0)