-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat(generativeai): Create genai_sdk_supervised_example.py #13350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yishan-pu
wants to merge
1
commit into
GoogleCloudPlatform:main
Choose a base branch
from
yishan-pu:patch-7
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+78
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
generative_ai/model_tuning/genai_sdk_supervised_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 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.genai import types | ||
|
||
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
||
|
||
def genai_sdk_gemini_tuning_basic() -> types.TuningJob: | ||
# [START genaisdk_gemini_tuning_basic] | ||
import time | ||
|
||
from google import genai | ||
from google.genai import types | ||
|
||
# TODO(developer): Update and un-comment below lines | ||
# PROJECT_ID = "your-project-id" | ||
client = genai.Client( | ||
vertexai=True, | ||
project=PROJECT_ID, | ||
location="us-central1", | ||
) | ||
|
||
tuning_job = client.tunings.tune( | ||
base_model="gemini-2.0-flash-lite-001", | ||
train_dataset="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl", | ||
config=types.CreateTuningJobConfig( | ||
tuned_model_display_name="Example tuning job", | ||
# Set to True to disable tuning intermediate checkpoints. | ||
# export_last_checkpoint_only=True, | ||
), | ||
) | ||
|
||
running_states = set([ | ||
"JOB_STATE_PENDING", | ||
"JOB_STATE_RUNNING", | ||
]) | ||
|
||
while tuning_job.state in running_states: | ||
print(tuning_job.state) | ||
tuning_job = client.tunings.get(name=tuning_job.name) | ||
time.sleep(60) | ||
Comment on lines
+54
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a try-except block to handle potential exceptions during the API call. This will make the example more robust and prevent it from crashing if the API call fails. try:
tuning_job = client.tunings.get(name=tuning_job.name)
except Exception as e:
print(f"Error getting tuning job: {e}")
break # Exit the loop if there's an error |
||
|
||
print(tuning_job.tuned_model.model) | ||
print(tuning_job.tuned_model.endpoint) | ||
print(tuning_job.experiment) | ||
# Example response: | ||
# projects/123456789012/locations/us-central1/models/1234567890@1 | ||
# projects/123456789012/locations/us-central1/endpoints/123456789012345 | ||
# projects/123456789012/locations/us-central1/metadataStores/default/contexts/tuning-experiment-2025010112345678 | ||
|
||
if tuning_job.tuned_model.checkpoints: | ||
for i in range(len(tuning_job.tuned_model.checkpoints)): | ||
checkpoint = tuning_job.tuned_model.checkpoints[i] | ||
print(f"Checkpoint {i + 1}: ", checkpoint) | ||
# Example response: | ||
# Checkpoint 1: checkpoint_id='1' epoch=1 step=10 endpoint='projects/123456789012/locations/us-central1/endpoints/123456789000000' | ||
# Checkpoint 2: checkpoint_id='2' epoch=2 step=20 endpoint='projects/123456789012/locations/us-central1/endpoints/123456789012345' | ||
|
||
# [END genaisdk_gemini_tuning_basic] | ||
return tuning_job | ||
|
||
|
||
if __name__ == "__main__": | ||
genai_sdk_gemini_tuning_basic() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
PROJECT_ID
should be explicitly set if the environment variable is not found, or the program should exit with an error message. Consider using a default value or raising an exception if the environment variable is not set. This will prevent unexpected behavior if the environment variable is missing.