-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
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,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") | ||
|
||
|
||
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
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. |
||
client = genai.Client( | ||
vertexai=True, | ||
project=PROJECT_ID, | ||
location="us-central1", | ||
) | ||
|
||
name = "projects/12345678/locations/us-central1/tuningJobs/123456789012345" | ||
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. This 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
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 error handling around the 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() |
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 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.