From 27fe5f1ddc824a8fd8a489f107076094390e0674 Mon Sep 17 00:00:00 2001 From: David del Real Date: Tue, 18 Aug 2026 16:36:51 -0600 Subject: [PATCH 1/2] feat(genai/embeddings): migrate lower dimension embeddings to genai (#14512) * feat(genai/embeddings): migrate lower dimension embeddings to genai Migrates the sample for generating multimodal embeddings with lower dimensionality to use the new `google-genai` SDK and adds the corresponding test. * Fixed region: required to be global. * Addressed comments on defensive programming. --- ...enerate_embeddings_with_lower_dimension.py | 71 +++++++++++++++++++ genai/embeddings/test_embeddings_examples.py | 6 ++ 2 files changed, 77 insertions(+) create mode 100644 genai/embeddings/generate_embeddings_with_lower_dimension.py diff --git a/genai/embeddings/generate_embeddings_with_lower_dimension.py b/genai/embeddings/generate_embeddings_with_lower_dimension.py new file mode 100644 index 0000000000..27feb6a555 --- /dev/null +++ b/genai/embeddings/generate_embeddings_with_lower_dimension.py @@ -0,0 +1,71 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START aiplatform_genai_embeddings_specify_lower_dimension] +import os + +from google import genai + +# TODO (Developer) Set environment variables +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") +LOCATION_ID = "global" + +# Supported dimensions: 128, 256, 512, 1408 (or up to 3072 for gemini-embedding-2) +EMBEDDING_DIMENSION = 128 +IMAGE_URI = "gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png" +EMBEDDING_MODEL = "gemini-embedding-2" +CONTEXTUAL_TEXT = "Colosseum" + + +def generate_embeddings_with_lower_dimension() -> genai.types.EmbedContentResponse: + """Generates multimodal embeddings (image + text) with custom lower dimensionality + + using the modern google-genai SDK. + """ + + client = genai.Client( + vertexai=True, + project=PROJECT_ID, + location=LOCATION_ID, + ) + + image_part = genai.types.Part.from_uri( + file_uri=IMAGE_URI, + mime_type="image/png", + ) + + text_part = genai.types.Part.from_text(text=CONTEXTUAL_TEXT) + + contents = genai.types.Content(parts=[image_part, text_part]) + + config = genai.types.EmbedContentConfig(output_dimensionality=EMBEDDING_DIMENSION) + + response = client.models.embed_content( + model=EMBEDDING_MODEL, + contents=[contents], + config=config, + ) + + if response.embeddings: + + embeddings = response.embeddings[0].values + + print(f"Embeddings (dim={len(embeddings)}): {embeddings[:3]}...\n") + + print(response) + + return response + + +# [END aiplatform_genai_embeddings_specify_lower_dimension] diff --git a/genai/embeddings/test_embeddings_examples.py b/genai/embeddings/test_embeddings_examples.py index 7e13633be8..a375b0151c 100644 --- a/genai/embeddings/test_embeddings_examples.py +++ b/genai/embeddings/test_embeddings_examples.py @@ -20,6 +20,7 @@ import code_retrieval_example import embeddings_docretrieval_with_txt +import generate_embeddings_with_lower_dimension import model_tuning_example import multimodal_embedding_image import multimodal_embedding_video @@ -45,6 +46,11 @@ def test_model_tuning_example() -> None: assert response +def test_generate_embeddings_with_lower_dimension() -> None: + response = generate_embeddings_with_lower_dimension.generate_embeddings_with_lower_dimension() + assert response + + def test_multimodal_embedding_image() -> None: response = multimodal_embedding_image.embed_content() assert response From 9e0b2831b813cef1b438f77d592d045ce7f7b7fb Mon Sep 17 00:00:00 2001 From: David del Real Date: Tue, 18 Aug 2026 18:14:30 -0600 Subject: [PATCH 2/2] feat(genai/embeddings): migrate multimodal embeddings to genai (#14515) * feat(genai/embeddings): migrate multimodal embeddings to genai Migrates the sample for generating multimodal (image, video, text) embeddings to the new `google-genai` SDK and adds the corresponding test. * Added missing parenthesis on method call. * Removed main method. * arrange import order * fixed linting issues. --- genai/embeddings/multimodal_example.py | 68 ++++++++++++++++++++ genai/embeddings/test_embeddings_examples.py | 7 ++ 2 files changed, 75 insertions(+) create mode 100644 genai/embeddings/multimodal_example.py diff --git a/genai/embeddings/multimodal_example.py b/genai/embeddings/multimodal_example.py new file mode 100644 index 0000000000..a6c76e0e2a --- /dev/null +++ b/genai/embeddings/multimodal_example.py @@ -0,0 +1,68 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START aiplatform_genai_multimodal_embedding_image_video_text] + +import os + +from google import genai + +# Environment configuration +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-project-id") +LOCATION_ID = "global" + +EMBEDDING_MODEL = "gemini-embedding-2" +IMAGE_URI = "gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png" +VIDEO_URI = "gs://cloud-samples-data/vertex-ai-vision/highway_vehicles.mp4" +CONTEXTUAL_TEXT = "Cars on Highway" + + +def get_image_video_text_embeddings() -> genai.types.EmbedContentResponse: + """Generates multimodal embeddings from image, video, and text using the google-genai SDK.""" + + client = genai.Client( + vertexai=True, + project=PROJECT_ID, + location=LOCATION_ID, + ) + + image_part = genai.types.Part.from_uri( + file_uri=IMAGE_URI, + mime_type="image/png", + ) + + video_part = genai.types.Part.from_uri( + file_uri=VIDEO_URI, + mime_type="video/mp4", + ) + + content = genai.types.Content( + parts=[image_part, video_part, genai.types.Part.from_text(text=CONTEXTUAL_TEXT)] + ) + + # Joint/Interleaved Multimodal Embedding (Image + Video + Text in same vector space) + response = client.models.embed_content(model=EMBEDDING_MODEL, contents=content) + + if response.embeddings: + + vector = response.embeddings[0].values + + print(f"Embeddings ({len(vector)} dims): {vector[:3]}...") + + print(response) + + return response + + +# [END aiplatform_genai_multimodal_embedding_image_video_text] diff --git a/genai/embeddings/test_embeddings_examples.py b/genai/embeddings/test_embeddings_examples.py index a375b0151c..7f1f988efb 100644 --- a/genai/embeddings/test_embeddings_examples.py +++ b/genai/embeddings/test_embeddings_examples.py @@ -24,6 +24,8 @@ import model_tuning_example import multimodal_embedding_image import multimodal_embedding_video +import multimodal_example + os.environ["GOOGLE_GENAI_USE_ENTERPRISE"] = "True" os.environ["GOOGLE_CLOUD_LOCATION"] = "global" @@ -46,6 +48,11 @@ def test_model_tuning_example() -> None: assert response +def test_multimodal_example() -> None: + response = multimodal_example.get_image_video_text_embeddings() + assert response + + def test_generate_embeddings_with_lower_dimension() -> None: response = generate_embeddings_with_lower_dimension.generate_embeddings_with_lower_dimension() assert response