From 227626c65bc975588a909d48212aa3eb90c9bb37 Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Tue, 8 Apr 2025 23:59:42 -0700 Subject: [PATCH 1/8] feat: add code samples for model optimizer --- .../text_generation/text_example02.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 generative_ai/text_generation/text_example02.py diff --git a/generative_ai/text_generation/text_example02.py b/generative_ai/text_generation/text_example02.py new file mode 100644 index 00000000000..66f36d039d2 --- /dev/null +++ b/generative_ai/text_generation/text_example02.py @@ -0,0 +1,52 @@ +# Copyright 2024 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. +import os + +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") + + +def generate_from_text_input() -> str: + # [START generativeaionvertexai_model_optimizer_generate_from_text_input] + import vertexai + from vertexai.generative_models import GenerativeModel, GenerationConfig + + # TODO(developer): Update and un-comment below line + # PROJECT_ID = "your-project-id" + vertexai.init(project=PROJECT_ID, location="us-central1") + + model = GenerativeModel("model-optimizer-exp-04-09", generation_config=GenerationConfig( + model_config=GenerationConfig.ModelConfig( + # valid options are: PRIORITIZE_QUALITY, BALANCED, PRIORITIZE_COST + feature_selection_preference=GenerationConfig.ModelConfig.FeatureSelectionPreference.PRIORITIZE_COST + ) + )) + + response = model.generate_content( + "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?" + ) + + print(response.text) + # Example response: + # **Emphasizing the Dried Aspect:** + # * Everlasting Blooms + # * Dried & Delightful + # * The Petal Preserve + # ... + + # [END generativeaionvertexai_model_optimizer_generate_from_text_input] + return response.text + + +if __name__ == "__main__": + generate_from_text_input() From 599ab8afeb6439fb1cd1b77e028df5b230bf1462 Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Wed, 9 Apr 2025 11:53:45 -0700 Subject: [PATCH 2/8] refactor: rename file to be model optimizer specific --- .../{text_example02.py => model_optimizer_text01.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename generative_ai/text_generation/{text_example02.py => model_optimizer_text01.py} (100%) diff --git a/generative_ai/text_generation/text_example02.py b/generative_ai/text_generation/model_optimizer_text01.py similarity index 100% rename from generative_ai/text_generation/text_example02.py rename to generative_ai/text_generation/model_optimizer_text01.py From 3857ccd28ef5deb019f02ef2bb8583d181fe8063 Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Wed, 9 Apr 2025 12:19:56 -0700 Subject: [PATCH 3/8] refactor: set up requirements.txt and nox_config.py --- .../text_generation/noxfile_config.py | 42 +++++++++++++++++++ .../text_generation/requirements.txt | 1 + 2 files changed, 43 insertions(+) create mode 100644 generative_ai/text_generation/noxfile_config.py create mode 100644 generative_ai/text_generation/requirements.txt diff --git a/generative_ai/text_generation/noxfile_config.py b/generative_ai/text_generation/noxfile_config.py new file mode 100644 index 00000000000..962ba40a926 --- /dev/null +++ b/generative_ai/text_generation/noxfile_config.py @@ -0,0 +1,42 @@ +# Copyright 2021 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 +# +# http://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. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +# You can copy this file into your directory, then it will be imported from +# the noxfile.py. + +# The source of truth: +# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["2.7", "3.7", "3.8", "3.10", "3.11", "3.13"], + # Old samples are opted out of enforcing Python type hints + # All new samples should feature them + "enforce_type_hints": True, + # An envvar key for determining the project id to use. Change it + # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a + # build specific Cloud project. You can also use your own string + # to use your own Cloud project. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', + # If you need to use a specific version of pip, + # change pip_version_override to the string representation + # of the version number, for example, "20.2.4" + "pip_version_override": None, + # A dictionary you want to inject into your test. Don't put any + # secrets here. These values will override predefined values. + "envs": {}, +} diff --git a/generative_ai/text_generation/requirements.txt b/generative_ai/text_generation/requirements.txt new file mode 100644 index 00000000000..b9e7fab64da --- /dev/null +++ b/generative_ai/text_generation/requirements.txt @@ -0,0 +1 @@ +google-cloud-aiplatform==1.88.0 From 9c3acca6879748102b2545f0e2533f89190efaa6 Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Wed, 9 Apr 2025 12:00:52 -0700 Subject: [PATCH 4/8] refactor: remove redundant todo comment --- generative_ai/text_generation/model_optimizer_text01.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/generative_ai/text_generation/model_optimizer_text01.py b/generative_ai/text_generation/model_optimizer_text01.py index 66f36d039d2..e5af568fc6e 100644 --- a/generative_ai/text_generation/model_optimizer_text01.py +++ b/generative_ai/text_generation/model_optimizer_text01.py @@ -13,7 +13,8 @@ # limitations under the License. import os -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") +# Replace 'your-project-id' with a default value if needed +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-project-id") def generate_from_text_input() -> str: @@ -21,8 +22,6 @@ def generate_from_text_input() -> str: import vertexai from vertexai.generative_models import GenerativeModel, GenerationConfig - # TODO(developer): Update and un-comment below line - # PROJECT_ID = "your-project-id" vertexai.init(project=PROJECT_ID, location="us-central1") model = GenerativeModel("model-optimizer-exp-04-09", generation_config=GenerationConfig( From f3ca97b46c38dd74d722a28fa7ddef0d2c7c37f2 Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Wed, 9 Apr 2025 12:42:32 -0700 Subject: [PATCH 5/8] chore: update to copyright 2025 --- generative_ai/text_generation/model_optimizer_text01.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generative_ai/text_generation/model_optimizer_text01.py b/generative_ai/text_generation/model_optimizer_text01.py index e5af568fc6e..9e0890351dd 100644 --- a/generative_ai/text_generation/model_optimizer_text01.py +++ b/generative_ai/text_generation/model_optimizer_text01.py @@ -1,4 +1,4 @@ -# Copyright 2024 Google LLC +# Copyright 2025 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From b45c64bd8134978194e848ad0e474506027c7355 Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Wed, 9 Apr 2025 22:13:55 -0700 Subject: [PATCH 6/8] refactor: rename file model_optimizer_text01.py -> textgen_model_optimizer_with_txt.py --- ...el_optimizer_text01.py => textgen_model_optimizer_with_txt.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename generative_ai/text_generation/{model_optimizer_text01.py => textgen_model_optimizer_with_txt.py} (100%) diff --git a/generative_ai/text_generation/model_optimizer_text01.py b/generative_ai/text_generation/textgen_model_optimizer_with_txt.py similarity index 100% rename from generative_ai/text_generation/model_optimizer_text01.py rename to generative_ai/text_generation/textgen_model_optimizer_with_txt.py From d886d53895f33258a08d2460df6e01a7327150dc Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Wed, 9 Apr 2025 22:16:38 -0700 Subject: [PATCH 7/8] chore: updated region tag --- .../text_generation/textgen_model_optimizer_with_txt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generative_ai/text_generation/textgen_model_optimizer_with_txt.py b/generative_ai/text_generation/textgen_model_optimizer_with_txt.py index 9e0890351dd..483e5773d9e 100644 --- a/generative_ai/text_generation/textgen_model_optimizer_with_txt.py +++ b/generative_ai/text_generation/textgen_model_optimizer_with_txt.py @@ -18,7 +18,7 @@ def generate_from_text_input() -> str: - # [START generativeaionvertexai_model_optimizer_generate_from_text_input] + # [START generativeaionvertexai_textgen_model_optimizer_with_txt] import vertexai from vertexai.generative_models import GenerativeModel, GenerationConfig @@ -43,7 +43,7 @@ def generate_from_text_input() -> str: # * The Petal Preserve # ... - # [END generativeaionvertexai_model_optimizer_generate_from_text_input] + # [END generativeaionvertexai_textgen_model_optimizer_with_txt] return response.text From 52cc2cbb1719a38d0e7fe69c1ca75f038a8eb0bc Mon Sep 17 00:00:00 2001 From: Yan Qiao Date: Thu, 10 Apr 2025 19:41:36 -0700 Subject: [PATCH 8/8] chore: add test file --- .../text_generation/requirements-test.txt | 1 + .../text_generation/text_generation_test.py | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 generative_ai/text_generation/requirements-test.txt create mode 100644 generative_ai/text_generation/text_generation_test.py diff --git a/generative_ai/text_generation/requirements-test.txt b/generative_ai/text_generation/requirements-test.txt new file mode 100644 index 00000000000..15d066af319 --- /dev/null +++ b/generative_ai/text_generation/requirements-test.txt @@ -0,0 +1 @@ +pytest==8.2.0 diff --git a/generative_ai/text_generation/text_generation_test.py b/generative_ai/text_generation/text_generation_test.py new file mode 100644 index 00000000000..a0175caa41d --- /dev/null +++ b/generative_ai/text_generation/text_generation_test.py @@ -0,0 +1,20 @@ +# Copyright 2025 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. + +import textgen_model_optimizer_with_txt + + +def test_model_optimizer_with_txt() -> None: + response = textgen_model_optimizer_with_txt.generate_from_text_input() + assert response