-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: add code samples for tuning with intermediate checkpoints #13366
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
google-genai==1.7.0 | ||
google-genai==1.15.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# 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. | ||
|
||
|
||
def create_with_checkpoints() -> str: | ||
# [START googlegenaisdk_tuning_with_checkpoints_create] | ||
import time | ||
|
||
from google import genai | ||
from google.genai.types import HttpOptions, CreateTuningJobConfig | ||
|
||
client = genai.Client(http_options=HttpOptions(api_version="v1")) | ||
|
||
tuning_job = client.tunings.tune( | ||
base_model="gemini-2.0-flash-lite-001", | ||
training_dataset="gs://cloud-samples-data/ai-platform/generative_ai/gemini-2_0/text/sft_train_data.jsonl", | ||
config=CreateTuningJobConfig( | ||
tuned_model_display_name="Example tuning job", | ||
# Set to True to disable tuning intermediate checkpoints. Default is False. | ||
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. 3 issues
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. Hi Sampath, export_last_checkpoint_only is the variable name defined by the Gen AI SDK, and aligns with the API and the UI. |
||
export_last_checkpoint_only=False, | ||
), | ||
) | ||
|
||
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) | ||
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. |
||
|
||
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, checkpoint in enumerate(tuning_job.tuned_model.checkpoints): | ||
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 googlegenaisdk_tuning_with_checkpoints_create] | ||
return tuning_job.name | ||
|
||
|
||
if __name__ == "__main__": | ||
create_with_checkpoints() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 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. | ||
|
||
|
||
def get_tuned_model_with_checkpoints(name: str) -> str: | ||
# [START googlegenaisdk_tuning_with_checkpoints_get_model] | ||
from google import genai | ||
from google.genai.types import HttpOptions | ||
|
||
client = genai.Client(http_options=HttpOptions(api_version="v1")) | ||
|
||
# Get the tuning job and the tuned model. | ||
# Eg. name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345" | ||
tuning_job = client.tunings.get(name=name) | ||
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. Kindly do not use Generic variable names like Use something like |
||
tuned_model = client.models.get(model=tuning_job.tuned_model.model) | ||
print(tuned_model) | ||
# Example response: | ||
# Model(name='projects/123456789012/locations/us-central1/models/1234567890@1', ...) | ||
|
||
print(f"Default checkpoint: {tuned_model.default_checkpoint_id}") | ||
# Example response: | ||
# Default checkpoint: 2 | ||
|
||
if tuned_model.checkpoints: | ||
for _, checkpoint in enumerate(tuned_model.checkpoints): | ||
print(f"Checkpoint {checkpoint.checkpoint_id}: ", checkpoint) | ||
# Example response: | ||
# Checkpoint 1: checkpoint_id='1' epoch=1 step=10 | ||
# Checkpoint 2: checkpoint_id='2' epoch=2 step=20 | ||
|
||
# [END googlegenaisdk_tuning_with_checkpoints_get_model] | ||
return tuned_model.name | ||
|
||
|
||
if __name__ == "__main__": | ||
tuning_job_name = input("Tuning job name: ") | ||
get_tuned_model_with_checkpoints(tuning_job_name) | ||
msampathkumar marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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. | ||
|
||
|
||
def list_checkpoints(name: str) -> str: | ||
# [START googlegenaisdk_tuning_with_checkpoints_list_checkpoints] | ||
from google import genai | ||
from google.genai.types import HttpOptions | ||
|
||
client = genai.Client(http_options=HttpOptions(api_version="v1")) | ||
|
||
# Get the tuning job and the tuned model. | ||
# Eg. name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345" | ||
tuning_job = client.tunings.get(name=name) | ||
|
||
if tuning_job.tuned_model.checkpoints: | ||
for i, checkpoint in enumerate(tuning_job.tuned_model.checkpoints): | ||
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 googlegenaisdk_tuning_with_checkpoints_list_checkpoints] | ||
return tuning_job.name | ||
|
||
|
||
if __name__ == "__main__": | ||
tuning_job_name = input("Tuning job name: ") | ||
list_checkpoints(tuning_job_name) | ||
msampathkumar marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Do not use
gemini-2_0
(model family version or model versions) in the file name.You can use
gemini_sft
orgemini_flash_sft
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.
Thanks for the feedback! I'll update the filename to
genai_flash_sft
to better reflect the content.