Skip to content

Commit b428be3

Browse files
feat(GenerativeAI): Add new sample for embeddings feature (GoogleCloudPlatform#12629)
* Add new code samples * Add minor code refactoring & improving visibility
1 parent fac8cd2 commit b428be3

11 files changed

+218
-206
lines changed

generative_ai/embeddings/batch_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def embed_text_batch() -> BatchPredictionJob:
2828
import vertexai
2929
from vertexai.preview import language_models
3030

31-
# TODO(developer): Uncomment and set your project ID
31+
# TODO(developer): Update & uncomment line below
3232
# PROJECT_ID = "your-project-id"
3333
vertexai.init(project=PROJECT_ID, location="us-central1")
3434
input_uri = (
@@ -38,7 +38,7 @@ def embed_text_batch() -> BatchPredictionJob:
3838
output_uri = OUTPUT_URI
3939

4040
textembedding_model = language_models.TextEmbeddingModel.from_pretrained(
41-
"textembedding-gecko"
41+
"textembedding-gecko@003"
4242
)
4343

4444
batch_prediction_job = textembedding_model.batch_predict(

generative_ai/embeddings/batch_example_test.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

generative_ai/embeddings/document_retrieval_example.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,33 @@
1313
# limitations under the License.
1414

1515
# [START generativeaionvertexai_embedding]
16-
from typing import List, Optional
16+
from __future__ import annotations
1717

1818
from vertexai.language_models import TextEmbeddingInput, TextEmbeddingModel
1919

2020

21-
def embed_text(
22-
texts: list = None,
23-
task: str = "RETRIEVAL_DOCUMENT",
24-
dimensionality: Optional[int] = 256,
25-
) -> List[List[float]]:
21+
def embed_text() -> list[list[float]]:
2622
"""Embeds texts with a pre-trained, foundational model.
27-
Args:
28-
texts (List[str]): A list of texts to be embedded.
29-
task (str): The task type for embedding. Check the available tasks in the model's documentation.
30-
dimensionality (Optional[int]): The dimensionality of the output embeddings.
23+
3124
Returns:
32-
List[List[float]]: A list of lists containing the embedding vectors for each input text
25+
A list of lists containing the embedding vectors for each input text
3326
"""
34-
if texts is None:
35-
texts = ["banana muffins? ", "banana bread? banana muffins?"]
27+
28+
# A list of texts to be embedded.
29+
texts = ["banana muffins? ", "banana bread? banana muffins?"]
30+
# The dimensionality of the output embeddings.
31+
dimensionality = 256
32+
# The task type for embedding. Check the available tasks in the model's documentation.
33+
task = "RETRIEVAL_DOCUMENT"
34+
3635
model = TextEmbeddingModel.from_pretrained("text-embedding-004")
3736
inputs = [TextEmbeddingInput(text, task) for text in texts]
3837
kwargs = dict(output_dimensionality=dimensionality) if dimensionality else {}
3938
embeddings = model.get_embeddings(inputs, **kwargs)
39+
40+
print(embeddings)
4041
# Example response:
41-
# [[0.006135190837085247, -0.01462465338408947, 0.004978656303137541, ...],
42+
# [[0.006135190837085247, -0.01462465338408947, 0.004978656303137541, ...], [0.1234434666, ...]],
4243
return [embedding.values for embedding in embeddings]
4344

4445

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from vertexai.vision_models import MultiModalEmbeddingResponse
18+
19+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
20+
21+
22+
def generate_embeddings_with_lower_dimension() -> MultiModalEmbeddingResponse:
23+
"""Example of how to use lower dimensions when creating multimodal embeddings.
24+
25+
Read more @ https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-multimodal-embeddings#low-dimension
26+
27+
Returns:
28+
The multimodal embedding response.
29+
"""
30+
# [START generativeaionvertexai_embeddings_specify_lower_dimension]
31+
import vertexai
32+
33+
from vertexai.vision_models import Image, MultiModalEmbeddingModel
34+
35+
# TODO(developer): Update & uncomment line below
36+
# PROJECT_ID = "your-project-id"
37+
vertexai.init(project=PROJECT_ID, location="us-central1")
38+
39+
# TODO(developer): Try different dimenions: 128, 256, 512, 1408
40+
embedding_dimension = 128
41+
42+
model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding@001")
43+
image = Image.load_from_file(
44+
"gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png")
45+
46+
embeddings = model.get_embeddings(
47+
image=image,
48+
contextual_text="Colosseum",
49+
dimension=embedding_dimension,
50+
)
51+
52+
print(f"Image Embedding: {embeddings.image_embedding}")
53+
print(f"Text Embedding: {embeddings.text_embedding}")
54+
55+
# Example response:
56+
# Image Embedding: [0.0622573346, -0.0406507477, 0.0260440577, ...]
57+
# Text Embedding: [0.27469793, -0.146258667, 0.0222803634, ...]
58+
59+
# [END generativeaionvertexai_embeddings_specify_lower_dimension]
60+
61+
return embeddings
62+
63+
64+
if __name__ == "__main__":
65+
generate_embeddings_with_lower_dimension()

generative_ai/embeddings/model_tuning_test.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

generative_ai/embeddings/multimodal_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@
2222
def get_image_video_text_embeddings() -> MultiModalEmbeddingResponse:
2323
"""Example of how to generate multimodal embeddings from image, video, and text.
2424
25-
Read more @ https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#video-best-practices
25+
Read more @ https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-multimodal-embeddings#img-txt-vid-embedding
2626
"""
2727
# [START generativeaionvertexai_multimodal_embedding_image_video_text]
2828
import vertexai
2929

3030
from vertexai.vision_models import Image, MultiModalEmbeddingModel, Video
3131
from vertexai.vision_models import VideoSegmentConfig
3232

33-
# TODO(developer): Uncomment and set your project ID
33+
# TODO(developer): Update & uncomment line below
3434
# PROJECT_ID = "your-project-id"
3535
vertexai.init(project=PROJECT_ID, location="us-central1")
3636

37-
model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding")
37+
model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding@001")
3838

3939
image = Image.load_from_file(
4040
"gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png"

generative_ai/embeddings/multimodal_image_example.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
2020

2121

22-
def get_image_embeddings() -> MultiModalEmbeddingResponse:
22+
def get_image_text_embeddings() -> MultiModalEmbeddingResponse:
2323
"""Example of how to generate multimodal embeddings from image and text.
2424
25-
Read more @ https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#low-dimension
25+
Read more @ https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-multimodal-embeddings#text-image-embedding
2626
"""
2727
# [START generativeaionvertexai_multimodal_embedding_image]
2828
import vertexai
2929
from vertexai.vision_models import Image, MultiModalEmbeddingModel
3030

31-
# TODO(developer): Uncomment and set your project ID
31+
# TODO(developer): Update & uncomment line below
3232
# PROJECT_ID = "your-project-id"
3333
vertexai.init(project=PROJECT_ID, location="us-central1")
3434

35-
model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding")
35+
model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding@001")
3636
image = Image.load_from_file(
3737
"gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png"
3838
)
@@ -54,4 +54,4 @@ def get_image_embeddings() -> MultiModalEmbeddingResponse:
5454

5555

5656
if __name__ == "__main__":
57-
get_image_embeddings()
57+
get_image_text_embeddings()

generative_ai/embeddings/multimodal_test.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

generative_ai/embeddings/multimodal_video_example.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,27 @@
1919

2020

2121
def get_video_embeddings() -> MultiModalEmbeddingResponse:
22-
"""Example of how to generate multimodal embeddings from video and text.
22+
"""Example of how to use the multimodal embedding model to get embeddings only for video content.
2323
24-
Read more at https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#video-best-practices
24+
Read more at https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-multimodal-embeddings#vid-embedding
2525
"""
2626
# [START generativeaionvertexai_multimodal_embedding_video]
2727
import vertexai
2828

2929
from vertexai.vision_models import MultiModalEmbeddingModel, Video
3030
from vertexai.vision_models import VideoSegmentConfig
3131

32-
# TODO(developer): Uncomment and set your project ID
32+
# TODO(developer): Update & uncomment line below
3333
# PROJECT_ID = "your-project-id"
3434
vertexai.init(project=PROJECT_ID, location="us-central1")
3535

36-
model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding")
36+
model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding@001")
3737

3838
embeddings = model.get_embeddings(
3939
video=Video.load_from_file(
4040
"gs://cloud-samples-data/vertex-ai-vision/highway_vehicles.mp4"
4141
),
4242
video_segment_config=VideoSegmentConfig(end_offset_sec=1),
43-
contextual_text="Cars on Highway",
4443
)
4544

4645
# Video Embeddings are segmented based on the video_segment_config.
@@ -51,12 +50,10 @@ def get_video_embeddings() -> MultiModalEmbeddingResponse:
5150
)
5251
print(f"Embedding: {video_embedding.embedding}")
5352

54-
print(f"Text Embedding: {embeddings.text_embedding}")
5553
# Example response:
5654
# Video Embeddings:
5755
# Video Segment: 0.0 - 1.0
5856
# Embedding: [-0.0206376351, 0.0123456789, ...]
59-
# Text Embedding: [-0.0207006913, -0.00251061679, ...]
6057

6158
# [END generativeaionvertexai_multimodal_embedding_video]
6259

0 commit comments

Comments
 (0)