Skip to content

Commit b46827c

Browse files
authored
feat: renamed sample operations and cleaned up test warnings (#1407)
this is a continuation of #1394 cleaned up all the pydantic issues because of this
1 parent 2f2c0b9 commit b46827c

23 files changed

+83
-69
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ build-backend = "setuptools.build_meta"
5959
write_to = "src/ragas/_version.py"
6060

6161
[tool.pytest.ini_options]
62-
addopts = "-n 4"
62+
addopts = "-n 4"
63+
asyncio_default_fixture_loop_scope = "function"

src/ragas/dataset_schema.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@
1313

1414

1515
class BaseEvalSample(BaseModel):
16-
def dict(self, **kwargs):
17-
row = super().dict(**kwargs)
18-
row = {k: v for k, v in row.items() if v is not None}
19-
return row
16+
def to_dict(self) -> t.Dict:
17+
"""
18+
Get the dictionary representation of the sample without attributes that are None.
19+
"""
20+
return self.model_dump(exclude_none=True)
2021

21-
def features(self):
22-
return set(self.dict().keys())
22+
def get_features(self) -> t.List[str]:
23+
"""
24+
Get the features of the sample that are not None.
25+
"""
26+
return list(self.to_dict().keys())
2327

2428

2529
class SingleTurnSample(BaseEvalSample):
30+
"""
31+
Represents evaluation samples for single-turn interactions.
32+
"""
33+
2634
user_input: t.Optional[str] = None
2735
retrieved_contexts: t.Optional[t.List[str]] = None
2836
reference_contexts: t.Optional[t.List[str]] = None
@@ -68,7 +76,7 @@ def validate_user_input(
6876
return messages
6977

7078
def to_messages(self):
71-
return [m.dict() for m in self.user_input]
79+
return [m.model_dump() for m in self.user_input]
7280

7381
def pretty_repr(self):
7482
lines = []
@@ -98,7 +106,7 @@ def get_sample_type(self):
98106
return type(self.samples[0])
99107

100108
def _to_list(self) -> t.List[t.Dict]:
101-
rows = [sample.dict() for sample in self.samples]
109+
rows = [sample.model_dump() for sample in self.samples]
102110

103111
if self.get_sample_type() == MultiTurnSample:
104112
for sample in rows:
@@ -130,7 +138,7 @@ def to_pandas(self) -> PandasDataframe:
130138
return pd.DataFrame(data)
131139

132140
def features(self):
133-
return self.samples[0].features()
141+
return self.samples[0].get_features()
134142

135143
@classmethod
136144
def from_list(cls, mapping: t.List[t.Dict]):

src/ragas/experimental/metrics/_faithfulness.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,5 @@ async def _ascore(self, row: t.Dict, callbacks: Callbacks) -> float:
248248
async def _single_turn_ascore(
249249
self: t.Self, sample: SingleTurnSample, callbacks: Callbacks
250250
) -> float:
251-
row = sample.dict()
251+
row = sample.to_dict()
252252
return await self._ascore(row, callbacks)

src/ragas/integrations/langchain.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ def __init__(self, metric: Metric, **kwargs: t.Any):
4848
t.cast(MetricWithLLM, self.metric).llm = LangchainLLMWrapper(llm)
4949
if isinstance(self.metric, MetricWithEmbeddings):
5050
embeddings = get_or_init(kwargs, "embeddings", OpenAIEmbeddings)
51-
t.cast(MetricWithEmbeddings, self.metric).embeddings = (
52-
LangchainEmbeddingsWrapper(embeddings)
53-
)
51+
t.cast(
52+
MetricWithEmbeddings, self.metric
53+
).embeddings = LangchainEmbeddingsWrapper(embeddings)
5454
self.metric.init(run_config)
5555

5656
assert isinstance(
@@ -132,7 +132,7 @@ def _validate(self, input: SingleTurnSample) -> None:
132132
# validate each example
133133
required_columns = self.metric.required_columns.get("SINGLE_TURN", [])
134134
for col in required_columns:
135-
if col not in input.features():
135+
if col not in input.get_features():
136136
raise ValueError(
137137
f'"{col}" is required in each example'
138138
f"for the metric[{self.metric.name}] you have chosen."

src/ragas/metrics/_answer_correctness.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _create_statements_prompt(self, question: str, text: str) -> PromptValue:
219219
async def _single_turn_ascore(
220220
self: t.Self, sample: SingleTurnSample, callbacks: Callbacks
221221
) -> float:
222-
row = sample.dict()
222+
row = sample.to_dict()
223223
score = await self._ascore(row, callbacks)
224224
return score
225225

src/ragas/metrics/_answer_relevance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _create_question_gen_prompt(self, row: t.Dict) -> PromptValue:
160160
async def _single_turn_ascore(
161161
self, sample: SingleTurnSample, callbacks: Callbacks
162162
) -> float:
163-
row = sample.dict()
163+
row = sample.to_dict()
164164
return await self._ascore(row, callbacks)
165165

166166
async def _ascore(self, row: t.Dict, callbacks: Callbacks) -> float:

src/ragas/metrics/_answer_similarity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __post_init__(self: t.Self):
6060
async def _single_turn_ascore(
6161
self, sample: SingleTurnSample, callbacks: Callbacks
6262
) -> float:
63-
row = sample.dict()
63+
row = sample.to_dict()
6464
return await self._ascore(row, callbacks)
6565

6666
async def _ascore(self: t.Self, row: t.Dict, callbacks: Callbacks) -> float:

src/ragas/metrics/_aspect_critic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _compute_score(
143143
async def _single_turn_ascore(
144144
self: t.Self, sample: SingleTurnSample, callbacks: Callbacks
145145
) -> float:
146-
row = sample.dict()
146+
row = sample.to_dict()
147147
return await self._ascore(row, callbacks)
148148

149149
async def _ascore(self: t.Self, row: t.Dict, callbacks: Callbacks) -> float:

src/ragas/metrics/_context_entities_recall.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async def get_entities(
175175
async def _single_turn_ascore(
176176
self, sample: SingleTurnSample, callbacks: Callbacks
177177
) -> float:
178-
row = sample.dict()
178+
row = sample.to_dict()
179179
return await self._ascore(row, callbacks)
180180

181181
async def _ascore(

src/ragas/metrics/_context_precision.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _calculate_average_precision(
154154
async def _single_turn_ascore(
155155
self, sample: SingleTurnSample, callbacks: Callbacks
156156
) -> float:
157-
row = sample.dict()
157+
row = sample.to_dict()
158158
return await self._ascore(row, callbacks)
159159

160160
async def _ascore(

0 commit comments

Comments
 (0)