Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(generativeai): Create genai_sdk_supervised_checkpoints_test_exa… #13347

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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 genai_sdk_gemini_checkpoints_test_tuned_endpoint() -> str:
# [START genaisdk_gemini_checkpoints_test_tuned_endpoint]
from google import genai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's good practice to provide a more descriptive comment explaining why the developer needs to update and uncomment these lines. For example, specify what your-project-id refers to and how to obtain it.

Suggested change
# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# TODO(developer): Update with your project ID, which can be found in the Google Cloud Console, and uncomment below lines
# PROJECT_ID = "your-project-id"

client = genai.Client(
vertexai=True,
project=PROJECT_ID,
location="us-central1",
)

name = "projects/12345678/locations/us-central1/tuningJobs/123456789012345"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The name variable is hardcoded. While this is an example, it would be beneficial to provide guidance on how a user would obtain the correct tuning job name. Consider adding a comment that explains where to find this information in the Google Cloud Console or via the API.

Suggested change
name = "projects/12345678/locations/us-central1/tuningJobs/123456789012345"
# TODO(developer): Replace with your actual tuning job name.
# You can find the tuning job name in the Google Cloud Console or via the API.
name = "projects/12345678/locations/us-central1/tuningJobs/123456789012345"

tuning_job = client.tunings.get(name=name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding error handling in case the tuning job is not found or if there are issues retrieving it. This would make the example more robust.

Suggested change
tuning_job = client.tunings.get(name=name)
try:
tuning_job = client.tunings.get(name=name)
except Exception as e:
print(f"Error retrieving tuning job: {e}")
return


contents = "Why is the sky blue?"

# Tests the default checkpoint
response = client.models.generate_content(
model=tuning_job.tuned_model.endpoint,
contents=contents,
)
print(response.text)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It would be beneficial to add a check to ensure that the response is valid before printing the text. This could prevent errors if the API call fails.

  if response and response.text:
    print(response.text)
  else:
    print("No response text")


# Tests Checkpoint 1
checkpoint1_response = client.models.generate_content(
model=tuning_job.tuned_model.checkpoints[0].endpoint,
contents=contents,
)
print(checkpoint1_response.text)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the default checkpoint, add a check to ensure that the response is valid before printing the text for checkpoint 1.

  if checkpoint1_response and checkpoint1_response.text:
    print(checkpoint1_response.text)
  else:
    print("No response text for checkpoint 1")


# Tests Checkpoint 2
checkpoint2_response = client.models.generate_content(
model=tuning_job.tuned_model.checkpoints[1].endpoint,
contents=contents,
)
print(checkpoint2_response.text)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the default checkpoint and checkpoint 1, add a check to ensure that the response is valid before printing the text for checkpoint 2.

  if checkpoint2_response and checkpoint2_response.text:
    print(checkpoint2_response.text)
  else:
    print("No response text for checkpoint 2")


# [END genaisdk_gemini_checkpoints_test_tuned_endpoint]
return response.text


if __name__ == "__main__":
genai_sdk_gemini_checkpoints_test_tuned_endpoint()