-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat(generativeai): Create genai_sdk_supervised_checkpoints_set_default_example.py #13346
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
base: main
Are you sure you want to change the base?
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# 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_checkpoints_set_default_example() -> types.Model: | ||
# [START genaisdk_gemini_tuning_checkpoints_set_default] | ||
from google import genai | ||
|
||
client = genai.Client( | ||
vertexai=True, | ||
project=PROJECT_ID, | ||
location="us-central1", | ||
) | ||
|
||
name = ( | ||
"projects/123456789012/locations/us-central1/tuningJobs/123456789012345" | ||
) | ||
tuning_job = client.tunings.get(name=name) | ||
tuned_model = client.models.get(model=tuning_job.tuned_model.model) | ||
|
||
Comment on lines
+35
to
+37
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. It's good practice to include error handling. Wrap the API calls in try:
tuning_job = client.tunings.get(name=name)
tuned_model = client.models.get(model=tuning_job.tuned_model.model)
except Exception as e:
print(f"Error getting tuning job or model: {e}")
return None |
||
print(f"Default checkpoint: {tuned_model.default_checkpoint_id}") | ||
print(f"Tuned model endpoint: {tuning_job.tuned_model.endpoint}") | ||
# Example response: | ||
# Default checkpoint: 2 | ||
# projects/123456789012/locations/us-central1/endpoints/123456789012345 | ||
|
||
tuned_model = client.models.update( | ||
model=tuned_model.name, | ||
config=types.UpdateModelConfig(default_checkpoint_id="1"), | ||
) | ||
Comment on lines
+44
to
+47
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. Add error handling here as well, similar to the previous API calls. This will ensure that the example handles potential errors when updating the model. try:
tuned_model = client.models.update(
model=tuned_model.name,
config=types.UpdateModelConfig(default_checkpoint_id="1"),
)
except Exception as e:
print(f"Error updating model: {e}")
return None |
||
|
||
print(f"Default checkpoint: {tuned_model.default_checkpoint_id}") | ||
print(f"Tuned model endpoint: {tuning_job.tuned_model.endpoint}") | ||
# Example response: | ||
# Default checkpoint: 1 | ||
# projects/123456789012/locations/us-central1/endpoints/123456789000000 | ||
|
||
# [END genaisdk_gemini_tuning_checkpoints_set_default] | ||
return tuned_model | ||
|
||
|
||
if __name__ == "__main__": | ||
genai_sdk_gemini_tuning_checkpoints_set_default_example() | ||
Comment on lines
+59
to
+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. |
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.
Consider using environment variables for the
name
to make the example more configurable and avoid hardcoding project-specific information. This would allow users to easily adapt the example to their own projects without modifying the code directly.