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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yishan-pu
Copy link

Add a Gen AI SDK to test a tuned endpoint.

Add a Gen AI SDK to test a tuned endpoint.
@yishan-pu yishan-pu requested review from a team as code owners May 7, 2025 18:17
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label May 7, 2025
Copy link

snippet-bot bot commented May 7, 2025

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.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 the google package.
    • Initializes a genai.Client with vertexai=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.
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

  1. 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +24 to +25
# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
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",
  )

Comment on lines +39 to +43
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.

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 ""


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

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

@msampathkumar msampathkumar assigned msampathkumar and unassigned nicain May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants