FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[pull] main from GoogleCloudPlatform:main by pull[bot] · Pull Request #294 · Reality2byte/python-docs-samples · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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]
68 changes: 68 additions & 0 deletions genai/embeddings/multimodal_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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]
13 changes: 13 additions & 0 deletions genai/embeddings/test_embeddings_examples.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

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
import multimodal_example


os.environ["GOOGLE_GENAI_USE_ENTERPRISE"] = "True"
os.environ["GOOGLE_CLOUD_LOCATION"] = "global"
Expand All @@ -45,6 +48,16 @@ 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


def test_multimodal_embedding_image() -> None:
response = multimodal_embedding_image.embed_content()
assert response
Expand Down
Loading

Back | FazBrowse Home | New Git URL