From b1649726d9b7d9e869216e43e5d8e4e0949894e4 Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Fri, 14 Aug 2026 21:39:01 +0000 Subject: [PATCH 1/3] 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. --- ...enerate_embeddings_with_lower_dimension.py | 67 +++++++++++++++++++ genai/embeddings/test_embeddings_examples.py | 5 ++ 2 files changed, 72 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..0b04a31204 --- /dev/null +++ b/genai/embeddings/generate_embeddings_with_lower_dimension.py @@ -0,0 +1,67 @@ +# 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 = os.getenv("LOCATION_ID", "us-central1") + +# 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-preview" +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( + enterprise=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, + ) + + embeddings = response.embeddings[0].values + + print(f"Embeddings (dim={len(embeddings)}): {embeddings[:3]}...") + + 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 a6afaa0070..fcf0385d82 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 os.environ["GOOGLE_GENAI_USE_ENTERPRISE"] = "True" @@ -40,3 +41,7 @@ def test_code_retrieval_example() -> None: def test_model_tuning_example() -> None: response = model_tuning_example.tune_embedding_model() assert response + +def test_generate_embeddings_with_lower_dimension() -> None: + response = generate_embeddings_with_lower_dimension.generate_embeddings_with_lower_dimension() + assert response From 23af44e54f61eabe71768c578f08ae9b641401ec Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Fri, 14 Aug 2026 22:14:10 +0000 Subject: [PATCH 2/3] Fixed region: required to be global. --- .../embeddings/generate_embeddings_with_lower_dimension.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/genai/embeddings/generate_embeddings_with_lower_dimension.py b/genai/embeddings/generate_embeddings_with_lower_dimension.py index 0b04a31204..41c3b672b9 100644 --- a/genai/embeddings/generate_embeddings_with_lower_dimension.py +++ b/genai/embeddings/generate_embeddings_with_lower_dimension.py @@ -19,12 +19,12 @@ # TODO (Developer) Set environment variables PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -LOCATION_ID = os.getenv("LOCATION_ID", "us-central1") +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-preview" +EMBEDDING_MODEL = "gemini-embedding-2" CONTEXTUAL_TEXT = "Colosseum" @@ -35,7 +35,7 @@ def generate_embeddings_with_lower_dimension() -> genai.types.EmbedContentRespon """ client = genai.Client( - enterprise=True, + vertexai=True, project=PROJECT_ID, location=LOCATION_ID, ) From 1a6a692362e0b52a8152bfc392d184c1313aee54 Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Mon, 17 Aug 2026 20:25:38 +0000 Subject: [PATCH 3/3] Addressed comments on defensive programming. --- .../generate_embeddings_with_lower_dimension.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/genai/embeddings/generate_embeddings_with_lower_dimension.py b/genai/embeddings/generate_embeddings_with_lower_dimension.py index 41c3b672b9..27feb6a555 100644 --- a/genai/embeddings/generate_embeddings_with_lower_dimension.py +++ b/genai/embeddings/generate_embeddings_with_lower_dimension.py @@ -57,9 +57,13 @@ def generate_embeddings_with_lower_dimension() -> genai.types.EmbedContentRespon config=config, ) - embeddings = response.embeddings[0].values + if response.embeddings: - print(f"Embeddings (dim={len(embeddings)}): {embeddings[:3]}...") + embeddings = response.embeddings[0].values + + print(f"Embeddings (dim={len(embeddings)}): {embeddings[:3]}...\n") + + print(response) return response