Skip to content

Commit ea5eb0a

Browse files
committed
rag: reuse chunk_approximately_equals util as prep for openai integration
1 parent 69473af commit ea5eb0a

3 files changed

Lines changed: 19 additions & 32 deletions

File tree

sdks/python/apache_beam/ml/rag/embeddings/huggingface_test.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from apache_beam.ml.rag.types import Chunk
2828
from apache_beam.ml.rag.types import Content
2929
from apache_beam.ml.rag.types import Embedding
30+
from apache_beam.ml.rag.test_utils import TestHelpers
3031
from apache_beam.ml.transforms.base import MLTransform
3132
from apache_beam.testing.test_pipeline import TestPipeline
3233
from apache_beam.testing.util import assert_that
@@ -40,19 +41,6 @@
4041
SENTENCE_TRANSFORMERS_AVAILABLE = False
4142

4243

43-
def chunk_approximately_equals(expected, actual):
44-
"""Compare embeddings allowing for numerical differences."""
45-
if not isinstance(expected, Chunk) or not isinstance(actual, Chunk):
46-
return False
47-
48-
return (
49-
expected.id == actual.id and expected.metadata == actual.metadata and
50-
expected.content == actual.content and
51-
len(expected.embedding.dense_embedding) == len(
52-
actual.embedding.dense_embedding) and
53-
all(isinstance(x, float) for x in actual.embedding.dense_embedding))
54-
55-
5644
@pytest.mark.uses_transformers
5745
@unittest.skipIf(
5846
not SENTENCE_TRANSFORMERS_AVAILABLE, "sentence-transformers not available")
@@ -105,7 +93,8 @@ def test_embedding_pipeline(self):
10593
with_transform(embedder))
10694

10795
assert_that(
108-
embeddings, equal_to(expected, equals_fn=chunk_approximately_equals))
96+
embeddings,
97+
equal_to(expected, equals_fn=TestHelpers.chunk_approximately_equals))
10998

11099

111100
if __name__ == '__main__':

sdks/python/apache_beam/ml/rag/embeddings/vertex_ai_test.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import unittest
2222

2323
import apache_beam as beam
24+
from apache_beam.ml.rag.test_utils import TestHelpers
2425
from apache_beam.ml.rag.types import Chunk
2526
from apache_beam.ml.rag.types import Content
2627
from apache_beam.ml.rag.types import Embedding
@@ -39,19 +40,6 @@
3940
VERTEX_AI_AVAILABLE = False
4041

4142

42-
def chunk_approximately_equals(expected, actual):
43-
"""Compare embeddings allowing for numerical differences."""
44-
if not isinstance(expected, Chunk) or not isinstance(actual, Chunk):
45-
return False
46-
47-
return (
48-
expected.id == actual.id and expected.metadata == actual.metadata and
49-
expected.content == actual.content and
50-
len(expected.embedding.dense_embedding) == len(
51-
actual.embedding.dense_embedding) and
52-
all(isinstance(x, float) for x in actual.embedding.dense_embedding))
53-
54-
5543
@unittest.skipIf(
5644
not VERTEX_AI_AVAILABLE, "Vertex AI dependencies not available")
5745
class VertexAITextEmbeddingsTest(unittest.TestCase):
@@ -104,7 +92,8 @@ def test_embedding_pipeline(self):
10492
with_transform(embedder))
10593

10694
assert_that(
107-
embeddings, equal_to(expected, equals_fn=chunk_approximately_equals))
95+
embeddings,
96+
equal_to(expected, equals_fn=TestHelpers.chunk_approximately_equals))
10897

10998

11099
if __name__ == '__main__':

sdks/python/apache_beam/ml/rag/test_utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ def find_free_port():
8080
# Return the port number assigned by OS.
8181
return s.getsockname()[1]
8282

83+
@staticmethod
84+
def chunk_approximately_equals(expected, actual):
85+
"""Compare embeddings allowing for numerical differences."""
86+
if not isinstance(expected, Chunk) or not isinstance(actual, Chunk):
87+
return False
88+
89+
return (
90+
expected.id == actual.id and expected.metadata == actual.metadata and
91+
expected.content == actual.content and
92+
len(expected.embedding.dense_embedding) == len(
93+
actual.embedding.dense_embedding) and
94+
all(isinstance(x, float) for x in actual.embedding.dense_embedding))
95+
8396

8497
class CustomMilvusContainer(MilvusContainer):
8598
"""Custom Milvus container with configurable ports and environment setup.
@@ -407,7 +420,3 @@ def assert_chunks_equivalent(
407420
# Validate field metadata.
408421
err_msg = f"Field Metadata doesn't match for chunk {actual.id}"
409422
assert a_f['metadata'] == e_f['metadata'], err_msg
410-
411-
412-
if __name__ == '__main__':
413-
unittest.main()

0 commit comments

Comments
 (0)