Skip to content

Commit fb608d0

Browse files
committed
fix: format and tests
1 parent 6254668 commit fb608d0

File tree

3 files changed

+34
-45
lines changed

3 files changed

+34
-45
lines changed

ragas/src/ragas/testset/transforms/relationship_builders/cosine.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ class CosineSimilarityBuilder(RelationshipBuilder):
1414
threshold: float = 0.9
1515
block_size: int = 1024
1616

17-
def _validate_embedding_shapes(self, embeddings: t.List[t.Any]):
18-
if not embeddings:
19-
raise ValueError(f"No nodes have a valid {self.property_name}")
20-
first_len = len(embeddings[0])
21-
for idx, emb in enumerate(embeddings):
22-
if len(emb) != first_len:
23-
raise ValueError(
24-
f"Embedding at index {idx} has length {len(emb)}, expected {first_len}. "
25-
"All embeddings must have the same length."
26-
)
27-
2817
def _block_cosine_similarity(self, i: np.ndarray, j: np.ndarray):
2918
"""Calculate cosine similarity matrix between two sets of embeddings."""
3019
i_norm = i / np.linalg.norm(i, axis=1, keepdims=True)

ragas/tests/unit/test_cosine_relationship_builders.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ def test__cosine_similarity(n_test_embeddings):
172172
result = builder._block_cosine_similarity(embeddings, embeddings)
173173

174174
assert result.shape == expected.shape, "Result shape does not match expected shape"
175-
assert np.allclose(
176-
result, expected, atol=1e-5
177-
), "Cosine similarity does not match expected values"
175+
assert np.allclose(result, expected, atol=1e-5), (
176+
"Cosine similarity does not match expected values"
177+
)
178178

179179

180180
# Test for the internal _find_similar_embedding_pairs method
@@ -212,14 +212,14 @@ def test__find_similar_embedding_pairs(n_test_embeddings, threshold, block_size)
212212

213213
for i, j, similarity_float in result:
214214
assert i < j, "Pairs should be ordered (i < j)"
215-
assert (
216-
similarity_float >= threshold
217-
), f"Similarity {similarity_float} should be >= {threshold}"
215+
assert similarity_float >= threshold, (
216+
f"Similarity {similarity_float} should be >= {threshold}"
217+
)
218218
for x, y, expected_similarity in expected:
219219
if i == x and j == y:
220-
assert similarity_float == pytest.approx(
221-
expected_similarity
222-
), "Cosine similarity does not match expected value"
220+
assert similarity_float == pytest.approx(expected_similarity), (
221+
"Cosine similarity does not match expected value"
222+
)
223223

224224
break
225225

@@ -230,9 +230,9 @@ async def test_no_self_similarity_relationships(self, simple_kg):
230230
builder = CosineSimilarityBuilder(property_name="embedding", threshold=0.1)
231231
relationships = await builder.transform(copy.deepcopy(simple_kg))
232232
for r in relationships:
233-
assert (
234-
r.source.id != r.target.id
235-
), "Self-relationships should not be created"
233+
assert r.source.id != r.target.id, (
234+
"Self-relationships should not be created"
235+
)
236236

237237
@pytest.mark.asyncio
238238
async def test_no_duplicate_relationships(self, simple_kg):
@@ -260,9 +260,9 @@ async def test_all_below_threshold(self):
260260
kg = KnowledgeGraph(nodes=[node1, node2])
261261
builder = CosineSimilarityBuilder(property_name="embedding", threshold=0.5)
262262
relationships = await builder.transform(kg)
263-
assert (
264-
len(relationships) == 0
265-
), "No relationships should be created below threshold"
263+
assert len(relationships) == 0, (
264+
"No relationships should be created below threshold"
265+
)
266266

267267
@pytest.mark.asyncio
268268
async def test_all_above_threshold(self):
@@ -287,7 +287,7 @@ async def test_malformed_embedding_raises(self):
287287
async def test_cosine_similarity_builder_empty_graph(self):
288288
kg = KnowledgeGraph(nodes=[])
289289
builder = CosineSimilarityBuilder(property_name="embedding")
290-
with pytest.raises(ValueError, match="No nodes have a valid embedding"):
290+
with pytest.raises(ValueError):
291291
await builder.transform(kg)
292292

293293
@pytest.mark.asyncio
@@ -351,9 +351,9 @@ async def test_apply_transforms_cosine_similarity_builder(self, simple_kg):
351351
# Should mutate kg in-place
352352
apply_transforms(kg, builder, run_config=RunConfig(max_workers=2))
353353
# Check that relationships were added
354-
assert any(
355-
r.type == "cosine_similarity" for r in kg.relationships
356-
), "No cosine_similarity relationships found after apply_transforms"
354+
assert any(r.type == "cosine_similarity" for r in kg.relationships), (
355+
"No cosine_similarity relationships found after apply_transforms"
356+
)
357357
# Check that expected relationship exists
358358
assert any(
359359
str(r.source.id) == "f353e5c2-e432-4d1e-84a8-d750c93d4edf"
@@ -419,9 +419,9 @@ async def test_apply_transforms_summary_cosine_similarity_builder(simple_kg):
419419
)
420420
kg = simple_kg
421421
apply_transforms(kg, builder, run_config=RunConfig(max_workers=2))
422-
assert any(
423-
r.type == "summary_cosine_similarity" for r in kg.relationships
424-
), "No summary_cosine_similarity relationships found after apply_transforms"
422+
assert any(r.type == "summary_cosine_similarity" for r in kg.relationships), (
423+
"No summary_cosine_similarity relationships found after apply_transforms"
424+
)
425425
assert any(
426426
str(r.source.id) == "f353e5c2-e432-4d1e-84a8-d750c93d4edf"
427427
and str(r.target.id) == "437c8c08-cef6-4ebf-a35f-93d6168b61a4"

ragas/tests/unit/test_traditional_relationship_builders.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ def test__find_similar_embedding_pairs_jaccard(n_test_sets, max_len, threshold):
262262
assert len(result) == len(expected)
263263
for i, j, similarity_float in result:
264264
assert i < j, "Pairs should be ordered (i < j)"
265-
assert similarity_float >= threshold, (
266-
f"Similarity {similarity_float} should be >= {threshold}"
267-
)
265+
assert (
266+
similarity_float >= threshold
267+
), f"Similarity {similarity_float} should be >= {threshold}"
268268
for x, y, expected_similarity in expected:
269269
if i == x and j == y:
270270
assert similarity_float == pytest.approx(expected_similarity)
@@ -277,9 +277,9 @@ async def test_no_self_similarity_relationships(self, simple_kg):
277277
builder = JaccardSimilarityBuilder(property_name="entities", threshold=0.1)
278278
relationships = await builder.transform(copy.deepcopy(simple_kg))
279279
for r in relationships:
280-
assert r.source.id != r.target.id, (
281-
"Self-relationships should not be created"
282-
)
280+
assert (
281+
r.source.id != r.target.id
282+
), "Self-relationships should not be created"
283283

284284
@pytest.mark.asyncio
285285
async def test_no_duplicate_relationships(self, simple_kg):
@@ -307,9 +307,9 @@ async def test_all_below_threshold(self):
307307
kg = KnowledgeGraph(nodes=[node1, node2])
308308
builder = JaccardSimilarityBuilder(property_name="entities", threshold=0.1)
309309
relationships = await builder.transform(kg)
310-
assert len(relationships) == 0, (
311-
"No relationships should be created below threshold"
312-
)
310+
assert (
311+
len(relationships) == 0
312+
), "No relationships should be created below threshold"
313313

314314
@pytest.mark.asyncio
315315
async def test_all_above_threshold(self):
@@ -379,9 +379,9 @@ async def test_apply_transforms_cosine_similarity_builder(self, simple_kg):
379379
# Should mutate kg in-place
380380
apply_transforms(kg, builder, run_config=RunConfig(max_workers=2))
381381
# Check that relationships were added
382-
assert any(r.type == "jaccard_similarity" for r in kg.relationships), (
383-
"No jaccard_similarity relationships found after apply_transforms"
384-
)
382+
assert any(
383+
r.type == "jaccard_similarity" for r in kg.relationships
384+
), "No jaccard_similarity relationships found after apply_transforms"
385385
# Check that expected relationship exists
386386
assert any(
387387
str(r.source.id) == "f353e5c2-e432-4d1e-84a8-d750c93d4edf"

0 commit comments

Comments
 (0)