Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions genai/embeddings/generate_embeddings_with_lower_dimension.py
Original file line number Diff line number Diff line change
@@ -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 = "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,
)
Comment on lines +37 to +41

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The google-genai SDK uses the vertexai parameter to initialize the client for Vertex AI, rather than enterprise. Using enterprise=True will result in a TypeError at runtime.

Suggested change
client = genai.Client(
enterprise=True,
project=PROJECT_ID,
location=LOCATION_ID,
)
client = genai.Client(
vertexai=True,
project=PROJECT_ID,
location=LOCATION_ID,
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not, tested locally.


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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The google-genai SDK's Part class does not have a from_text classmethod. To create a text part, you should instantiate Part directly with the text parameter.

Suggested change
text_part = genai.types.Part.from_text(text=CONTEXTUAL_TEXT)
text_part = genai.types.Part(text=CONTEXTUAL_TEXT)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally, it does.


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]
5 changes: 5 additions & 0 deletions genai/embeddings/test_embeddings_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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