Skip to content

feat(generativeai): Create genai_sdk_supervised_test_example.py #13353

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
50 changes: 50 additions & 0 deletions generative_ai/model_tuning/genai_sdk_supervised_test_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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")
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 providing a default value or raising an exception if the GOOGLE_CLOUD_PROJECT environment variable is not set. This will make the script more robust.

Suggested change
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-default-project-id") # Replace your-default-project-id with an appropriate default or raise an exception if it's mandatory



def genai_sdk_gemini_test_tuned_endpoint() -> str:
# [START genaisdk_gemini_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.

high

This TODO comment should be addressed. Either update the project ID or remove the comment if it's no longer needed.

  PROJECT_ID = "your-project-id" # Replace with your actual project ID
  client = genai.Client(
      vertexai=True,
      project=PROJECT_ID,
      location="us-central1",
  )

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

This name variable contains a hardcoded value. It would be better to make this configurable via an environment variable or command-line argument to make the script more reusable.

  name = os.getenv("TUNING_JOB_NAME", "projects/12345678/locations/us-central1/tuningJobs/123456789012345") # Replace with your actual tuning job name or retrieve it dynamically

tuning_job = client.tunings.get(name=name)

contents = "Why is the sky blue?"

# Tests the tuned model Endpoint.
# If intermediate checkpoints are enabled, the tuned model Endpoint will be the default checkpoint Endpoint.
response = client.models.generate_content(
model=tuning_job.tuned_model.endpoint,
contents=contents,
)
print(response.text)
Comment on lines +39 to +43
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Consider adding error handling around the generate_content call. If the API call fails, the program will crash. You should catch the exception and log an error message.

  try:
    response = client.models.generate_content(
        model=tuning_job.tuned_model.endpoint,
        contents=contents,
    )
    print(response.text)
  except Exception as e:
    print(f"Error generating content: {e}")
    return ""


# [END genaisdk_gemini_test_tuned_endpoint]
return response.text


if __name__ == "__main__":
genai_sdk_gemini_test_tuned_endpoint()