Skip to content

Commit 8ebc27d

Browse files
feat(genai): Add new folder and samples for model optimizer (#13385)
* feat(genai): add new folder for model optimizer * chore(genai): sync nox test config files * feat(genai): update README.md * feat(genai): Update caching to use `v1` version
1 parent 5003f35 commit 8ebc27d

19 files changed

+145
-15
lines changed

genai/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,23 @@ Demonstrates how to use Express Mode for simpler and faster interactions with Ge
5353
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)
5454
for details.
5555

56+
### [Image Generation](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/image_generation/)
57+
58+
Demonstrates how to generate image and edit images using Generative AI models. Check [Image Generation with Gemini Flash](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/image-generation)
59+
and [Imagen on Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/image/overview) for details.
60+
61+
5662
### [Live API](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/live_api/)
5763

5864
Provides examples of using the Generative AI [Live API](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal-live-api).
5965
This allows for real-time interactions and dynamic content generation.
6066

67+
### [Model Optimizer](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/model_optimizer/)
68+
69+
Provides examples of using the Generative AI [Model Optimizer](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/vertex-ai-model-optimizer).
70+
Vertex AI Model Optimizer is a dynamic endpoint designed to simplify model selection by automatically applying the
71+
Gemini model which best meets your needs.
72+
6173
### [Provisioned Throughput](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/genai/live_api/)
6274

6375
Provides examples demonstrating how to use Provisioned Throughput with Generative AI models. This feature provides a

genai/bounding_box/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

genai/content_cache/contentcache_create_with_txt_gcs_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def create_content_cache() -> str:
1818
from google import genai
1919
from google.genai.types import Content, CreateCachedContentConfig, HttpOptions, Part
2020

21-
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
21+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
2222

2323
system_instruction = """
2424
You are an expert researcher. You always stick to the facts in the sources provided, and never make up new facts.

genai/content_cache/contentcache_delete.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
def delete_context_caches(cache_name: str) -> str:
1717
# [START googlegenaisdk_contentcache_delete]
1818
from google import genai
19-
from google.genai.types import HttpOptions
20-
21-
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
2219

20+
client = genai.Client()
2321
# Delete content cache using name
2422
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
2523
client.caches.delete(name=cache_name)

genai/content_cache/contentcache_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def list_context_caches() -> str:
1818
from google import genai
1919
from google.genai.types import HttpOptions
2020

21-
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
21+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
2222

2323
content_cache_list = client.caches.list()
2424

genai/content_cache/contentcache_update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def update_content_cache(cache_name: str) -> str:
2222
from google import genai
2323
from google.genai.types import HttpOptions, UpdateCachedContentConfig
2424

25-
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
25+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
2626

2727
# Get content cache by name
2828
# cache_name = "projects/111111111111/locations/us-central1/cachedContents/1111111111111111111"

genai/content_cache/contentcache_use_with_txt.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def generate_content(cache_name: str) -> str:
1818
from google import genai
1919
from google.genai.types import GenerateContentConfig, HttpOptions
2020

21-
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
22-
21+
client = genai.Client(http_options=HttpOptions(api_version="v1"))
2322
# Use content cache to generate text response
2423
# E.g cache_name = 'projects/111111111111/locations/us-central1/cachedContents/1111111111111111111'
2524
response = client.models.generate_content(

genai/embeddings/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

genai/express_mode/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

genai/image_generation/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.7", "3.8", "3.9", "3.10", "3.11", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2025 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+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_modeloptimizer_with_txt]
18+
from google import genai
19+
from google.genai.types import (
20+
FeatureSelectionPreference,
21+
GenerateContentConfig,
22+
HttpOptions,
23+
ModelSelectionConfig
24+
)
25+
26+
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
27+
response = client.models.generate_content(
28+
model="model-optimizer-exp-04-09",
29+
contents="How does AI work?",
30+
config=GenerateContentConfig(
31+
model_selection_config=ModelSelectionConfig(
32+
feature_selection_preference=FeatureSelectionPreference.BALANCED # Options: PRIORITIZE_QUALITY, BALANCED, PRIORITIZE_COST
33+
),
34+
),
35+
)
36+
print(response.text)
37+
# Example response:
38+
# Okay, let's break down how AI works. It's a broad field, so I'll focus on the ...
39+
#
40+
# Here's a simplified overview:
41+
# ...
42+
# [END googlegenaisdk_modeloptimizer_with_txt]
43+
return response.text
44+
45+
46+
if __name__ == "__main__":
47+
generate_content()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 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+
# http://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+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
backoff==2.2.1
2+
google-api-core==2.19.0
3+
pytest==8.2.0
4+
pytest-asyncio==0.23.6
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==1.16.1
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2025 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+
import os
15+
16+
import modeloptimizer_with_txt
17+
18+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
19+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
20+
# The project name is included in the CICD pipeline
21+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
22+
23+
24+
def test_modeloptimizer_with_txt() -> None:
25+
assert modeloptimizer_with_txt.generate_content()

genai/provisioned_throughput/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

genai/text_generation/model_optimizer_textgen_with_txt.py

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

1515

16+
# TODO: Migrate model_optimizer samples to /model_optimizer
17+
# and deprecate following sample
1618
def generate_content() -> str:
1719
# [START googlegenaisdk_model_optimizer_textgen_with_txt]
1820
from google import genai

genai/text_generation/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

genai/video_generation/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"],
25+
"ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.12"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

0 commit comments

Comments
 (0)