Skip to content

Commit 10061a8

Browse files
ahgraberjjmachan
andauthored
feat: check embedding shapes in CosineSimilarityBuilders (#2069)
It is possible that a knowledge graph might be updated with a different configuration leading to embeddings of different dimension. This change provides an informative error in this case. Co-authored-by: jjmachan <jamesjithin97@gmail.com>
1 parent 169b37f commit 10061a8

File tree

1 file changed

+13
-0
lines changed
  • ragas/src/ragas/testset/transforms/relationship_builders

1 file changed

+13
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ def _find_similar_embedding_pairs(
3131
if pair[0] < pair[1]
3232
]
3333

34+
def _validate_embedding_shapes(self, embeddings: t.List[t.Any]):
35+
if not embeddings:
36+
return
37+
first_len = len(embeddings[0])
38+
for idx, emb in enumerate(embeddings):
39+
if len(emb) != first_len:
40+
raise ValueError(
41+
f"Embedding at index {idx} has length {len(emb)}, expected {first_len}. "
42+
"All embeddings must have the same length."
43+
)
44+
3445
async def transform(self, kg: KnowledgeGraph) -> t.List[Relationship]:
3546
if self.property_name is None:
3647
self.property_name = "embedding"
@@ -42,6 +53,7 @@ async def transform(self, kg: KnowledgeGraph) -> t.List[Relationship]:
4253
raise ValueError(f"Node {node.id} has no {self.property_name}")
4354
embeddings.append(embedding)
4455

56+
self._validate_embedding_shapes(embeddings)
4557
similar_pairs = self._find_similar_embedding_pairs(
4658
np.array(embeddings), self.threshold
4759
)
@@ -85,6 +97,7 @@ async def transform(self, kg: KnowledgeGraph) -> t.List[Relationship]:
8597
]
8698
if not embeddings:
8799
raise ValueError(f"No nodes have a valid {self.property_name}")
100+
self._validate_embedding_shapes(embeddings)
88101
similar_pairs = self._find_similar_embedding_pairs(
89102
np.array(embeddings), self.threshold
90103
)

0 commit comments

Comments
 (0)