Skip to content

Commit 4fe6277

Browse files
holtskinnerm-strzelczykgcf-owl-bot[bot]
authored
samples(generative-ai): Update Grounding Samples and remove Dimensions from MM Embeddings for Video (GoogleCloudPlatform#11858)
* samples(aiplatform): Update Grounding with Google Search sample to use GA endpoint * Add variable for data_store_path * Change Gemini 1.5 Pro to 1.5 Flash * `line` -> `lines` * fix: Remove Dimensions from Video MM Embeddings - Add to Image Embeddings so one example exists * Change test text for test_gemini_single_turn_video_example * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Maciej Strzelczyk <strzelczyk@google.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent e1b000b commit 4fe6277

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

generative_ai/gemini_grounding_example.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,26 @@
1313
# limitations under the License.
1414

1515

16-
from vertexai.preview.generative_models import GenerationResponse
16+
from vertexai.generative_models import GenerationResponse
1717

1818

1919
def generate_text_with_grounding_web(project_id: str) -> GenerationResponse:
2020
# [START generativeaionvertexai_gemini_grounding_with_web]
2121
import vertexai
2222

23-
from vertexai.preview.generative_models import grounding
24-
from vertexai.generative_models import GenerationConfig, GenerativeModel, Tool
23+
from vertexai.generative_models import (
24+
GenerationConfig,
25+
GenerativeModel,
26+
Tool,
27+
grounding,
28+
)
2529

2630
# TODO(developer): Update and un-comment below line
2731
# project_id = "PROJECT_ID"
2832

2933
vertexai.init(project=project_id, location="us-central1")
3034

31-
model = GenerativeModel(model_name="gemini-1.0-pro-002")
35+
model = GenerativeModel(model_name="gemini-1.5-flash-001")
3236

3337
# Use Google Search for grounding
3438
tool = Tool.from_google_search_retrieval(grounding.GoogleSearchRetrieval())
@@ -57,15 +61,14 @@ def generate_text_with_grounding_vertex_ai_search(
5761
from vertexai.preview.generative_models import grounding
5862
from vertexai.generative_models import GenerationConfig, GenerativeModel, Tool
5963

60-
# TODO(developer): Update and un-comment below line
64+
# TODO(developer): Update and un-comment below lines
6165
# project_id = "PROJECT_ID"
66+
# data_store_path = "projects/{project_id}/locations/{location}/collections/default_collection/dataStores/{data_store_id}"
6267

6368
vertexai.init(project=project_id, location="us-central1")
6469

65-
model = GenerativeModel(model_name="gemini-1.0-pro-002")
70+
model = GenerativeModel(model_name="gemini-1.5-flash-001")
6671

67-
# Use Vertex AI Search data store
68-
# Format: projects/{project_id}/locations/{location}/collections/default_collection/dataStores/{data_store_id}
6972
tool = Tool.from_retrieval(
7073
grounding.Retrieval(grounding.VertexAISearch(datastore=data_store_path))
7174
)

generative_ai/multimodal_embedding_image.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ def get_image_embeddings(
2121
project_id: str,
2222
image_path: str,
2323
contextual_text: Optional[str] = None,
24+
dimension: Optional[int] = 1408,
2425
) -> MultiModalEmbeddingResponse:
2526
"""Example of how to generate multimodal embeddings from image and text.
2627
2728
Args:
2829
project_id: Google Cloud Project ID, used to initialize vertexai
2930
image_path: Path to image (local or Google Cloud Storage) to generate embeddings for.
3031
contextual_text: Text to generate embeddings for.
32+
dimension: Dimension for the returned embeddings.
33+
https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#low-dimension
3134
"""
3235
# [START aiplatform_sdk_multimodal_embedding_image]
3336
import vertexai
@@ -42,6 +45,7 @@ def get_image_embeddings(
4245
embeddings = model.get_embeddings(
4346
image=image,
4447
contextual_text=contextual_text,
48+
dimension=dimension,
4549
)
4650
print(f"Image Embedding: {embeddings.image_embedding}")
4751
print(f"Text Embedding: {embeddings.text_embedding}")

generative_ai/multimodal_embedding_image_video_text.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def get_image_video_text_embeddings(
2222
image_path: str,
2323
video_path: str,
2424
contextual_text: Optional[str] = None,
25-
dimension: Optional[int] = 1408,
2625
video_segment_config: Optional[VideoSegmentConfig] = None,
2726
) -> MultiModalEmbeddingResponse:
2827
"""Example of how to generate multimodal embeddings from image, video, and text.
@@ -33,8 +32,6 @@ def get_image_video_text_embeddings(
3332
image_path: Path to image (local or Google Cloud Storage) to generate embeddings for.
3433
video_path: Path to video (local or Google Cloud Storage) to generate embeddings for.
3534
contextual_text: Text to generate embeddings for.
36-
dimension: Dimension for the returned embeddings.
37-
https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#low-dimension
3835
video_segment_config: Define specific segments to generate embeddings for.
3936
https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#video-best-practices
4037
"""
@@ -56,7 +53,6 @@ def get_image_video_text_embeddings(
5653
video=video,
5754
video_segment_config=video_segment_config,
5855
contextual_text=contextual_text,
59-
dimension=dimension,
6056
)
6157

6258
print(f"Image Embedding: {embeddings.image_embedding}")

generative_ai/multimodal_embedding_video.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def get_video_embeddings(
2121
project_id: str,
2222
video_path: str,
2323
contextual_text: Optional[str] = None,
24-
dimension: Optional[int] = 1408,
2524
video_segment_config: Optional[VideoSegmentConfig] = None,
2625
) -> MultiModalEmbeddingResponse:
2726
"""Example of how to generate multimodal embeddings from video and text.
@@ -31,8 +30,6 @@ def get_video_embeddings(
3130
location: Google Cloud Region, used to initialize vertexai
3231
video_path: Path to video (local or Google Cloud Storage) to generate embeddings for.
3332
contextual_text: Text to generate embeddings for.
34-
dimension: Dimension for the returned embeddings.
35-
https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#low-dimension
3633
video_segment_config: Define specific segments to generate embeddings for.
3734
https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-multimodal-embeddings#video-best-practices
3835
"""
@@ -52,7 +49,6 @@ def get_video_embeddings(
5249
video=video,
5350
video_segment_config=video_segment_config,
5451
contextual_text=contextual_text,
55-
dimension=dimension,
5652
)
5753

5854
# Video Embeddings are segmented based on the video_segment_config.

generative_ai/test_gemini_examples.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ def test_gemini_single_turn_video_example() -> None:
102102
text = gemini_single_turn_video_example.generate_text(PROJECT_ID)
103103
text = text.lower()
104104
assert len(text) > 0
105-
assert any([_ in text for _ in ("zoo", "tiger", "leaf", "water")])
105+
assert any(
106+
[_ in text for _ in ("zoo", "tiger", "leaf", "water", "animals", "photos")]
107+
)
106108

107109

108110
@pytest.mark.skip(

0 commit comments

Comments
 (0)