-
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
base: main
Are you sure you want to change the base?
Conversation
Add a Gen AI SDK to test a tuned endpoint.
Here is the summary of possible violations 😱 There is a possible violation for not having product prefix.
The end of the violation section. All the stuff below is FYI purposes only. Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
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.
Hello @yishan-pu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
This pull request adds a new Python script, genai_sdk_supervised_test_example.py
, to demonstrate how to test a tuned endpoint using the Gen AI SDK. The script initializes a Gen AI client, retrieves a tuning job, and then sends a sample query to the tuned model's endpoint. The response text is then printed to the console.
Highlights
- New Script: A new script
genai_sdk_supervised_test_example.py
is added to showcase testing of tuned endpoints. - Gen AI SDK Usage: The script uses the Gen AI SDK to interact with a tuned model endpoint.
- Endpoint Testing: Demonstrates how to send a query to a tuned model endpoint and print the response.
Changelog
- generative_ai/model_tuning/genai_sdk_supervised_test_example.py
- Creates a new file with a function
genai_sdk_gemini_test_tuned_endpoint
to test a tuned model endpoint. - Imports the
genai
module from thegoogle
package. - Initializes a
genai.Client
withvertexai=True
, project ID, and location. - Retrieves a tuning job using a hardcoded name.
- Sends a sample query "Why is the sky blue?" to the tuned model's endpoint.
- Prints the response text to the console.
- Includes a main block to execute the function when the script is run.
- Creates a new file with a function
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
A tuned model's grace,
A query sent, a response found,
AI's gentle hum.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
The code introduces a new example for testing a tuned generative AI endpoint using the GenAI SDK. The example retrieves a tuning job and uses its tuned model endpoint to generate content. Overall, the code is clear and functional, but there are a few areas that could be improved for robustness and clarity.
Summary of Findings
- Missing Error Handling: The code lacks error handling for the API calls. It's important to handle potential exceptions to prevent the program from crashing and to provide informative error messages.
- Hardcoded Values: The code uses hardcoded values for the project ID and tuning job name. These should be configurable or dynamically retrieved to make the example more reusable.
- TODO Comment: The TODO comment on line 24 indicates that the project ID needs to be updated. This should be addressed before merging.
Merge Readiness
The code is functional but needs improvements in error handling and configurability. The TODO comment should also be addressed. I recommend addressing these issues before merging. I am unable to directly approve the pull request, and users should have others review and approve this code before merging.
# TODO(developer): Update and un-comment below lines | ||
# PROJECT_ID = "your-project-id" |
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.
response = client.models.generate_content( | ||
model=tuning_job.tuned_model.endpoint, | ||
contents=contents, | ||
) | ||
print(response.text) |
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 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 ""
|
||
import os | ||
|
||
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") |
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.
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 |
location="us-central1", | ||
) | ||
|
||
name = "projects/12345678/locations/us-central1/tuningJobs/123456789012345" |
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.
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
Add a Gen AI SDK to test a tuned endpoint.