|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Tests for apache_beam.ml.rag.embeddings.vertex_ai.""" |
| 18 | + |
| 19 | +import shutil |
| 20 | +import tempfile |
| 21 | +import unittest |
| 22 | + |
| 23 | +import apache_beam as beam |
| 24 | +from apache_beam.ml.rag.types import Chunk |
| 25 | +from apache_beam.ml.rag.types import Content |
| 26 | +from apache_beam.ml.rag.types import Embedding |
| 27 | +from apache_beam.ml.transforms.base import MLTransform |
| 28 | +from apache_beam.testing.test_pipeline import TestPipeline |
| 29 | +from apache_beam.testing.util import assert_that |
| 30 | +from apache_beam.testing.util import equal_to |
| 31 | + |
| 32 | +# pylint: disable=ungrouped-imports |
| 33 | +try: |
| 34 | + import vertexai # pylint: disable=unused-import |
| 35 | + from apache_beam.ml.rag.embeddings.vertex_ai import VertexAITextEmbeddings |
| 36 | + VERTEX_AI_AVAILABLE = True |
| 37 | +except ImportError: |
| 38 | + VERTEX_AI_AVAILABLE = False |
| 39 | + |
| 40 | + |
| 41 | +def chunk_approximately_equals(expected, actual): |
| 42 | + """Compare embeddings allowing for numerical differences.""" |
| 43 | + if not isinstance(expected, Chunk) or not isinstance(actual, Chunk): |
| 44 | + return False |
| 45 | + |
| 46 | + return ( |
| 47 | + expected.id == actual.id and expected.metadata == actual.metadata and |
| 48 | + expected.content == actual.content and |
| 49 | + len(expected.embedding.dense_embedding) == len( |
| 50 | + actual.embedding.dense_embedding) and |
| 51 | + all(isinstance(x, float) for x in actual.embedding.dense_embedding)) |
| 52 | + |
| 53 | + |
| 54 | +@unittest.skipIf( |
| 55 | + not VERTEX_AI_AVAILABLE, "Vertex AI dependencies not available") |
| 56 | +class VertexAITextEmbeddingsTest(unittest.TestCase): |
| 57 | + def setUp(self): |
| 58 | + self.artifact_location = tempfile.mkdtemp(prefix='vertex_ai_') |
| 59 | + self.test_chunks = [ |
| 60 | + Chunk( |
| 61 | + content=Content(text="This is a test sentence."), |
| 62 | + id="1", |
| 63 | + metadata={ |
| 64 | + "source": "test.txt", "language": "en" |
| 65 | + }), |
| 66 | + Chunk( |
| 67 | + content=Content(text="Another example."), |
| 68 | + id="2", |
| 69 | + metadata={ |
| 70 | + "source": "test.txt", "language": "en" |
| 71 | + }) |
| 72 | + ] |
| 73 | + |
| 74 | + def tearDown(self) -> None: |
| 75 | + shutil.rmtree(self.artifact_location) |
| 76 | + |
| 77 | + def test_embedding_pipeline(self): |
| 78 | + # gecko@002 produces 768-dimensional embeddings |
| 79 | + expected = [ |
| 80 | + Chunk( |
| 81 | + id="1", |
| 82 | + embedding=Embedding(dense_embedding=[0.0] * 768), |
| 83 | + metadata={ |
| 84 | + "source": "test.txt", "language": "en" |
| 85 | + }, |
| 86 | + content=Content(text="This is a test sentence.")), |
| 87 | + Chunk( |
| 88 | + id="2", |
| 89 | + embedding=Embedding(dense_embedding=[0.0] * 768), |
| 90 | + metadata={ |
| 91 | + "source": "test.txt", "language": "en" |
| 92 | + }, |
| 93 | + content=Content(text="Another example.")) |
| 94 | + ] |
| 95 | + |
| 96 | + embedder = VertexAITextEmbeddings(model_name="textembedding-gecko@002") |
| 97 | + |
| 98 | + with TestPipeline() as p: |
| 99 | + embeddings = ( |
| 100 | + p |
| 101 | + | beam.Create(self.test_chunks) |
| 102 | + | MLTransform(write_artifact_location=self.artifact_location). |
| 103 | + with_transform(embedder)) |
| 104 | + |
| 105 | + assert_that( |
| 106 | + embeddings, equal_to(expected, equals_fn=chunk_approximately_equals)) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + unittest.main() |
0 commit comments