From dc227de33a54a251afac5aa96fa09a3f6c0b339a Mon Sep 17 00:00:00 2001 From: David del Real Sifuentes Date: Fri, 21 Aug 2026 16:24:36 +0000 Subject: [PATCH] chore(generative_ai): remove samples migrated to genai Deletes embeddings and model tuning examples that have been migrated to use the new `google-genai` SDK. --- generative_ai/embeddings/batch_example.py | 60 ----------- ...enerate_embeddings_with_lower_dimension.py | 64 ----------- .../embeddings/multimodal_example.py | 76 ------------- generative_ai/embeddings/noxfile_config.py | 42 -------- .../embeddings/requirements-test.txt | 4 - generative_ai/embeddings/requirements.txt | 11 -- .../embeddings/test_embeddings_examples.py | 102 ------------------ .../model_tuning/supervised_cancel_example.py | 40 ------- .../supervised_tuning_examples_test.py | 45 -------- 9 files changed, 444 deletions(-) delete mode 100644 generative_ai/embeddings/batch_example.py delete mode 100644 generative_ai/embeddings/generate_embeddings_with_lower_dimension.py delete mode 100644 generative_ai/embeddings/multimodal_example.py delete mode 100644 generative_ai/embeddings/noxfile_config.py delete mode 100644 generative_ai/embeddings/requirements-test.txt delete mode 100644 generative_ai/embeddings/requirements.txt delete mode 100644 generative_ai/embeddings/test_embeddings_examples.py delete mode 100644 generative_ai/model_tuning/supervised_cancel_example.py delete mode 100644 generative_ai/model_tuning/supervised_tuning_examples_test.py diff --git a/generative_ai/embeddings/batch_example.py b/generative_ai/embeddings/batch_example.py deleted file mode 100644 index bffb7419ae4..00000000000 --- a/generative_ai/embeddings/batch_example.py +++ /dev/null @@ -1,60 +0,0 @@ -# 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 - -from google.cloud.aiplatform import BatchPredictionJob - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def embed_text_batch(OUTPUT_URI: str) -> BatchPredictionJob: - """Example of how to generate embeddings from text using batch processing. - - Read more: https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/batch-prediction-genai-embeddings - """ - # [START generativeaionvertexai_embedding_batch] - import vertexai - - from vertexai.preview import language_models - - # TODO(developer): Update & uncomment line below - # PROJECT_ID = "your-project-id" - vertexai.init(project=PROJECT_ID, location="us-central1") - input_uri = ( - "gs://cloud-samples-data/generative-ai/embeddings/embeddings_input.jsonl" - ) - # Format: `"gs://your-bucket-unique-name/directory/` or `bq://project_name.llm_dataset` - output_uri = OUTPUT_URI - - textembedding_model = language_models.TextEmbeddingModel.from_pretrained( - "textembedding-gecko@003" - ) - - batch_prediction_job = textembedding_model.batch_predict( - dataset=[input_uri], - destination_uri_prefix=output_uri, - ) - print(batch_prediction_job.display_name) - print(batch_prediction_job.resource_name) - print(batch_prediction_job.state) - # Example response: - # BatchPredictionJob 2024-09-10 15:47:51.336391 - # projects/1234567890/locations/us-central1/batchPredictionJobs/123456789012345 - # JobState.JOB_STATE_SUCCEEDED - # [END generativeaionvertexai_embedding_batch] - return batch_prediction_job - - -if __name__ == "__main__": - embed_text_batch() diff --git a/generative_ai/embeddings/generate_embeddings_with_lower_dimension.py b/generative_ai/embeddings/generate_embeddings_with_lower_dimension.py deleted file mode 100644 index d2db506a391..00000000000 --- a/generative_ai/embeddings/generate_embeddings_with_lower_dimension.py +++ /dev/null @@ -1,64 +0,0 @@ -# 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 - -from vertexai.vision_models import MultiModalEmbeddingResponse - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def generate_embeddings_with_lower_dimension() -> MultiModalEmbeddingResponse: - """Example of how to use lower dimensions when creating multimodal embeddings. - - Read more @ https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-multimodal-embeddings#low-dimension - - Returns: - The multimodal embedding response. - """ - # [START generativeaionvertexai_embeddings_specify_lower_dimension] - import vertexai - - from vertexai.vision_models import Image, MultiModalEmbeddingModel - - # TODO(developer): Update & uncomment line below - # PROJECT_ID = "your-project-id" - vertexai.init(project=PROJECT_ID, location="us-central1") - - # TODO(developer): Try different dimenions: 128, 256, 512, 1408 - embedding_dimension = 128 - - model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding@001") - image = Image.load_from_file( - "gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png" - ) - - embeddings = model.get_embeddings( - image=image, - contextual_text="Colosseum", - dimension=embedding_dimension, - ) - - print(f"Image Embedding: {embeddings.image_embedding}") - print(f"Text Embedding: {embeddings.text_embedding}") - - # Example response: - # Image Embedding: [0.0622573346, -0.0406507477, 0.0260440577, ...] - # Text Embedding: [0.27469793, -0.146258667, 0.0222803634, ...] - # [END generativeaionvertexai_embeddings_specify_lower_dimension] - return embeddings - - -if __name__ == "__main__": - generate_embeddings_with_lower_dimension() diff --git a/generative_ai/embeddings/multimodal_example.py b/generative_ai/embeddings/multimodal_example.py deleted file mode 100644 index d04318c2a8a..00000000000 --- a/generative_ai/embeddings/multimodal_example.py +++ /dev/null @@ -1,76 +0,0 @@ -# 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 - -from vertexai.vision_models import MultiModalEmbeddingResponse - -PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - - -def get_image_video_text_embeddings() -> MultiModalEmbeddingResponse: - """Example of how to generate multimodal embeddings from image, video, and text. - - Read more @ https://cloud.google.com/vertex-ai/generative-ai/docs/embeddings/get-multimodal-embeddings#img-txt-vid-embedding - """ - # [START generativeaionvertexai_multimodal_embedding_image_video_text] - import vertexai - - from vertexai.vision_models import Image, MultiModalEmbeddingModel, Video - from vertexai.vision_models import VideoSegmentConfig - - # TODO(developer): Update & uncomment line below - # PROJECT_ID = "your-project-id" - vertexai.init(project=PROJECT_ID, location="us-central1") - - model = MultiModalEmbeddingModel.from_pretrained("multimodalembedding@001") - - image = Image.load_from_file( - "gs://cloud-samples-data/vertex-ai/llm/prompts/landmark1.png" - ) - video = Video.load_from_file( - "gs://cloud-samples-data/vertex-ai-vision/highway_vehicles.mp4" - ) - - embeddings = model.get_embeddings( - image=image, - video=video, - video_segment_config=VideoSegmentConfig(end_offset_sec=1), - contextual_text="Cars on Highway", - ) - - print(f"Image Embedding: {embeddings.image_embedding}") - - # Video Embeddings are segmented based on the video_segment_config. - print("Video Embeddings:") - for video_embedding in embeddings.video_embeddings: - print( - f"Video Segment: {video_embedding.start_offset_sec} - {video_embedding.end_offset_sec}" - ) - print(f"Embedding: {video_embedding.embedding}") - - print(f"Text Embedding: {embeddings.text_embedding}") - # Example response: - # Image Embedding: [-0.0123144267, 0.0727186054, 0.000201397663, ...] - # Video Embeddings: - # Video Segment: 0.0 - 1.0 - # Embedding: [-0.0206376351, 0.0345234685, ...] - # Text Embedding: [-0.0207006838, -0.00251058186, ...] - - # [END generativeaionvertexai_multimodal_embedding_image_video_text] - return embeddings - - -if __name__ == "__main__": - get_image_video_text_embeddings() diff --git a/generative_ai/embeddings/noxfile_config.py b/generative_ai/embeddings/noxfile_config.py deleted file mode 100644 index 0973c8621c7..00000000000 --- a/generative_ai/embeddings/noxfile_config.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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": ["3.8", "3.9", "3.11", "3.12", "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/embeddings/requirements-test.txt b/generative_ai/embeddings/requirements-test.txt deleted file mode 100644 index baa23bf9c3e..00000000000 --- a/generative_ai/embeddings/requirements-test.txt +++ /dev/null @@ -1,4 +0,0 @@ -backoff==2.2.1 -google-api-core==2.19.0 -pytest==9.0.3; python_version >= "3.10" -pytest-asyncio==0.23.6 diff --git a/generative_ai/embeddings/requirements.txt b/generative_ai/embeddings/requirements.txt deleted file mode 100644 index be325f58d2b..00000000000 --- a/generative_ai/embeddings/requirements.txt +++ /dev/null @@ -1,11 +0,0 @@ -pandas==2.2.3; python_version == '3.7' -pandas==2.2.3; python_version == '3.8' -pandas==2.2.3; python_version > '3.8' -pillow==12.3.0 -google-cloud-aiplatform[full]==1.157.0 -sentencepiece==0.2.1 -google-auth==2.29.0 -anthropic[vertex]==0.28.0 -numpy<3 -openai==1.68.2 -immutabledict==4.2.0 diff --git a/generative_ai/embeddings/test_embeddings_examples.py b/generative_ai/embeddings/test_embeddings_examples.py deleted file mode 100644 index 46eeddec881..00000000000 --- a/generative_ai/embeddings/test_embeddings_examples.py +++ /dev/null @@ -1,102 +0,0 @@ -# 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 - -import backoff -import batch_example -import code_retrieval_example -import document_retrieval_example -import generate_embeddings_with_lower_dimension -from google.api_core.exceptions import FailedPrecondition, ResourceExhausted -import google.auth -from google.cloud import aiplatform -from google.cloud.aiplatform import initializer as aiplatform_init -import model_tuning_example -import multimodal_example - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_embed_text_batch() -> None: - batch_prediction_job = batch_example.embed_text_batch( - "gs://python-docs-samples-tests/" - ) - assert batch_prediction_job - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_multimodal_embedding_image_video_text() -> None: - embeddings = multimodal_example.get_image_video_text_embeddings() - assert embeddings is not None - assert embeddings.image_embedding is not None - assert embeddings.video_embeddings is not None - assert embeddings.text_embedding is not None - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_generate_embeddings_with_lower_dimension() -> None: - embeddings = ( - generate_embeddings_with_lower_dimension.generate_embeddings_with_lower_dimension() - ) - assert embeddings is not None - assert embeddings.image_embedding is not None - assert len(embeddings.image_embedding) == 128 - assert embeddings.text_embedding is not None - assert len(embeddings.text_embedding) == 128 - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_text_embed_text() -> None: - embeddings = document_retrieval_example.embed_text() - assert [len(e) for e in embeddings] == [3072, 3072] - - -@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10) -def test_code_embed_text() -> None: - texts = [ - "banana bread?", - "banana muffin?", - "banana?", - ] - dimensionality = 256 - embeddings = code_retrieval_example.embed_text( - texts=texts, - task="CODE_RETRIEVAL_QUERY", - dimensionality=dimensionality, - ) - assert [len(e) for e in embeddings] == [dimensionality or 768] * len(texts) - - -@backoff.on_exception(backoff.expo, FailedPrecondition, max_time=300) -def dispose(tuning_job) -> None: # noqa: ANN001 - if tuning_job._status.name == "PIPELINE_STATE_RUNNING": - tuning_job._cancel() - - -def test_tune_embedding_model() -> None: - credentials, _ = google.auth.default( # Set explicit credentials with Oauth scopes. - scopes=["https://www.googleapis.com/auth/cloud-platform"] - ) - aiplatform.init( - api_endpoint="us-central1-aiplatform.googleapis.com:443", - project=os.getenv("GOOGLE_CLOUD_PROJECT"), - staging_bucket="gs://ucaip-samples-us-central1/training_pipeline_output", - credentials=credentials, - ) - tuning_job = model_tuning_example.tune_embedding_model( - aiplatform_init.global_config.api_endpoint - ) - try: - assert tuning_job._status.name != "PIPELINE_STATE_FAILED" - finally: - dispose(tuning_job) diff --git a/generative_ai/model_tuning/supervised_cancel_example.py b/generative_ai/model_tuning/supervised_cancel_example.py deleted file mode 100644 index 41fc8313d7c..00000000000 --- a/generative_ai/model_tuning/supervised_cancel_example.py +++ /dev/null @@ -1,40 +0,0 @@ -# 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") -LOCATION = "us-central1" - - -def cancel_tuning_job() -> None: - # [START generativeaionvertexai_cancel_tuning_job] - import vertexai - from vertexai.tuning import sft - - # TODO(developer): Update and un-comment below lines - # PROJECT_ID = "your-project-id" - # LOCATION = "us-central1" - vertexai.init(project=PROJECT_ID, location=LOCATION) - - tuning_job_id = "4982013113894174720" - job = sft.SupervisedTuningJob( - f"projects/{PROJECT_ID}/locations/{LOCATION}/tuningJobs/{tuning_job_id}" - ) - job.cancel() - # [END generativeaionvertexai_cancel_tuning_job] - - -if __name__ == "__main__": - cancel_tuning_job() diff --git a/generative_ai/model_tuning/supervised_tuning_examples_test.py b/generative_ai/model_tuning/supervised_tuning_examples_test.py deleted file mode 100644 index 28894b77a04..00000000000 --- a/generative_ai/model_tuning/supervised_tuning_examples_test.py +++ /dev/null @@ -1,45 +0,0 @@ -# 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 pytest - -import supervised_advanced_example -import supervised_cancel_example -import supervised_example -import supervised_get_example -import supervised_list_example - - -@pytest.mark.skip(reason="Skip due to tuning taking a long time.") -def test_gemini_tuning() -> None: - response = supervised_example.gemini_tuning_basic() - assert response - - response = supervised_advanced_example.gemini_tuning_advanced() - assert response - - -def test_get_tuning_job() -> None: - response = supervised_get_example.get_tuning_job() - assert response - - -def test_list_tuning_jobs() -> None: - response = supervised_list_example.list_tuning_jobs() - assert response - - -@pytest.mark.skip(reason="Skip due to tuning taking a long time.") -def test_cancel_tuning_job() -> None: - supervised_cancel_example.cancel_tuning_job()