Skip to content

Commit 23f4047

Browse files
refactor(genai): Update samples to use latest version of Gen AI SDK (#13257)
* feat(genai): Update samples to use latest version of GenAI SDK * refactor(genai): minor code formatting changes * refactor(genai): code reformatting changes * refactor(genai): Update README.md * fix(genai): lint error * fix(genai): lint error
1 parent 917ec7a commit 23f4047

37 files changed

+119
-76
lines changed

genai/README.md

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,76 @@ To run these samples, we recommend using either Google Cloud Shell, Cloud Code I
1616

1717
The samples are organized into the following categories:
1818

19-
### Batch Prediction (`batch_prediction`)
19+
### [Batch Prediction](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/batch_prediction/)
2020

21-
Demonstrates how to use batch prediction with Generative AI models. This allows efficient processing of large datasets. See the [Batch Prediction documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/batch-prediction-gemini) for more details.
21+
Demonstrates how to use batch prediction with Generative AI models. This allows efficient processing of large datasets.
22+
See the [Batch Prediction documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/batch-prediction-gemini)
23+
for more details.
2224

23-
### Content Cache (`content_cache`)
25+
### [Bounding Box](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/bounding_box/)
2426

25-
Illustrates how to create, update, use, and delete content caches. Caches store frequently used content to improve performance and reduce costs. See the [Content Cache documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview) for more information.
27+
Demonstrates how to use Bounding Box with Generative AI models. This allows for object detection and localization within
28+
images and video. see the [Bounding Box documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/bounding-box-detection)
29+
for more details.
2630

27-
### Controlled Generation (`controlled_generation`)
31+
### [Content Cache](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/content_cache/)
2832

29-
Provides examples of how to control various aspects of the generated content, such as length, format, safety attributes, and more. This allows for tailoring the output to specific requirements and constraints. See the [Controlled Generation documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output) for details.
33+
Illustrates how to create, update, use, and delete content caches. Caches store frequently used content to improve
34+
performance and reduce costs. See the [Content Cache documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/context-cache/context-cache-overview)
35+
for more information.
3036

31-
### Count Tokens (`count_tokens`)
37+
### [Controlled Generation](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/controlled_generation/)
3238

33-
Shows how to estimate token usage for inputs and outputs of Generative AI models. Understanding token consumption is crucial for managing costs and optimizing performance. See the [Token Counting documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/list-token) for more details.
39+
Provides examples of how to control various aspects of the generated content, such as length, format, safety attributes,
40+
and more. This allows for tailoring the output to specific requirements and constraints.
41+
See the [Controlled Generation documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output)
42+
for details.
3443

35-
### Express Mode (`express_mode`)
44+
### [Count Tokens](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/count_tokens/)
3645

37-
Demonstrates how to use Express Mode for simpler and faster interactions with Generative AI models using an API key. This mode is ideal for quick prototyping and experimentation. See the [Express Mode documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/start/express-mode/overview) for details.
46+
Shows how to estimate token usage for inputs and outputs of Generative AI models. Understanding token consumption is
47+
crucial for managing costs and optimizing performance. See the [Token Counting documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/list-token)
48+
for more details.
3849

39-
### Live API (`live_api`)
50+
### [Express Mode](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/express_mode/)
4051

41-
Provides examples of using the Generative AI [Live API](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal-live-api). This allows for real-time interactions and dynamic content generation.
52+
Demonstrates how to use Express Mode for simpler and faster interactions with Generative AI models using an API key.
53+
This mode is ideal for quick prototyping and experimentation. See the [Express Mode documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/start/express-mode/overview)
54+
for details.
4255

43-
### Safety (`safety`)
56+
### [Live API](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/live_api/)
4457

45-
Provides examples demonstrating how to configure and apply safety settings to Generative AI models. This includes techniques for content filtering and moderation to ensure responsible AI usage. See the [Safety documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes) for details.
58+
Provides examples of using the Generative AI [Live API](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal-live-api).
59+
This allows for real-time interactions and dynamic content generation.
4660

47-
### Text Generation (`text_generation`)
61+
### [Provisioned Throughput](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/live_api/)
4862

49-
Provides examples of generating text using various input modalities (text, images, audio, video) and features like asynchronous generation, chat, and text streaming. See the [Text Generation documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/send-chat-prompts-gemini) for details.
63+
Provides examples demonstrating how to use Provisioned Throughput with Generative AI models. This feature provides a
64+
fixed-cost monthly subscription or weekly service that reserves throughput for supported generative AI models on Vertex AI.
65+
See the [Provisioned Throughput](https://cloud.google.com/vertex-ai/generative-ai/docs/provisioned-throughput) for details.
5066

51-
### Tools (`tools`)
67+
### [Safety](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/safety/)
5268

53-
Showcases how to use tools like function calling, code execution, and grounding with Google Search to enhance Generative AI interactions. See the [Tools documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling) for more information.
69+
Provides examples demonstrating how to configure and apply safety settings to Generative AI models. This includes
70+
techniques for content filtering and moderation to ensure responsible AI usage. See the
71+
[Safety documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/configure-safety-attributes)
72+
for details.
73+
74+
### [Text Generation](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/text_generation/)
75+
76+
Provides examples of generating text using various input modalities (text, images, audio, video) and features like
77+
asynchronous generation, chat, and text streaming. See the[Text Generation documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/send-chat-prompts-gemini)
78+
for details.
79+
80+
### [Tools](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/tools/)
81+
82+
Showcases how to use tools like function calling, code execution, and grounding with Google Search to enhance
83+
Generative AI interactions. See the [Tools documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling) for more information.
84+
85+
### [Video Generation](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/video_generation/)
86+
87+
Provides examples of generating videos using text & images input modalities. See the
88+
[Video Generation documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/video/generate-videos) for details.
5489

5590
## Contributing
5691

genai/batch_prediction/batchpredict_embeddings_with_gcs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def generate_content(output_uri: str) -> str:
5959
# Job state: JOB_STATE_RUNNING
6060
# ...
6161
# Job state: JOB_STATE_SUCCEEDED
62-
6362
# [END googlegenaisdk_batchpredict_embeddings_with_gcs]
6463
return job.state
6564

genai/batch_prediction/batchpredict_with_bq.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def generate_content(output_uri: str) -> str:
5454
# Job state: JOB_STATE_RUNNING
5555
# ...
5656
# Job state: JOB_STATE_SUCCEEDED
57-
5857
# [END googlegenaisdk_batchpredict_with_bq]
5958
return job.state
6059

genai/batch_prediction/batchpredict_with_gcs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def generate_content(output_uri: str) -> str:
5555
# Job state: JOB_STATE_RUNNING
5656
# ...
5757
# Job state: JOB_STATE_SUCCEEDED
58-
5958
# [END googlegenaisdk_batchpredict_with_gcs]
6059
return job.state
6160

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-genai==1.2.0
1+
google-genai==1.7.0

genai/bounding_box/boundingbox_with_txt_img.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,13 @@ def generate_content() -> str:
1818
import requests
1919

2020
from google import genai
21-
from google.genai.types import (
22-
GenerateContentConfig,
23-
HttpOptions,
24-
Part,
25-
SafetySetting,
26-
)
21+
from google.genai.types import GenerateContentConfig, HttpOptions, Part, SafetySetting
2722

2823
from PIL import Image, ImageColor, ImageDraw
2924

3025
from pydantic import BaseModel
3126

27+
# Helper class to represent a bounding box
3228
class BoundingBox(BaseModel):
3329
"""
3430
Represents a bounding box with its 2D coordinates and associated label.
@@ -42,6 +38,7 @@ class BoundingBox(BaseModel):
4238
box_2d: list[int]
4339
label: str
4440

41+
# Helper function to plot bounding boxes on an image
4542
def plot_bounding_boxes(image_uri: str, bounding_boxes: list[BoundingBox]) -> None:
4643
"""
4744
Plots bounding boxes on an image with markers for each a name, using PIL, normalized coordinates, and different colors.
@@ -51,7 +48,6 @@ def plot_bounding_boxes(image_uri: str, bounding_boxes: list[BoundingBox]) -> No
5148
bounding_boxes: A list of bounding boxes containing the name of the object
5249
and their positions in normalized [y1 x1 y2 x2] format.
5350
"""
54-
5551
with Image.open(requests.get(image_uri, stream=True, timeout=10).raw) as im:
5652
width, height = im.size
5753
draw = ImageDraw.Draw(im)
@@ -92,7 +88,7 @@ def plot_bounding_boxes(image_uri: str, bounding_boxes: list[BoundingBox]) -> No
9288
),
9389
],
9490
response_mime_type="application/json",
95-
response_schema=list[BoundingBox],
91+
response_schema=list[BoundingBox], # Add BoundingBox class to the response schema
9692
)
9793

9894
image_uri = "https://storage.googleapis.com/generativeai-downloads/images/socks.jpg"

genai/bounding_box/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-genai==1.2.0
1+
google-genai==1.7.0
22
pillow==11.1.0

genai/content_cache/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-genai==1.2.0
1+
google-genai==1.7.0

genai/controlled_generation/ctrlgen_with_class_schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class Recipe(BaseModel):
5454
# ],
5555
# "recipe_name": "Classic Chocolate Chip Cookies"
5656
# }, ... ]
57-
5857
# [END googlegenaisdk_ctrlgen_with_class_schema]
5958
return response.text
6059

genai/controlled_generation/ctrlgen_with_enum_class_schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class InstrumentClass(enum.Enum):
4040
print(response.text)
4141
# Example output:
4242
# String
43-
4443
# [END googlegenaisdk_ctrlgen_with_enum_class_schema]
4544
return response.text
4645

0 commit comments

Comments
 (0)