-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(genai): Add samples for Model Optimizer #4104
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
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 @nardosalemu, 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
Hello team, gemini-code-assist here to provide a summary of this pull request. This PR, authored by nardosalemu, introduces new sample code for the Google Cloud GenAI platform, specifically focusing on the 'Model Optimizer' feature. The changes include adding a new Node.js sample demonstrating text generation with a routing configuration that prioritizes cost, along with a corresponding test file to verify its functionality.
Highlights
- New GenAI Sample: Adds a new sample demonstrating text generation using the
@google/genai
SDK. - Model Optimizer Feature: The sample showcases the use of a specific model (
model-optimizer-exp-04-09
) and includes amodelSelectionConfig
withfeatureSelectionPreference
set toPRIORITIZE_COST
, likely related to the Model Optimizer feature. - Text Generation with Routing: The core functionality of the sample is generating text content from a prompt ('How does AI work?') while applying the specified routing configuration.
- Automated Testing: Includes a new test file using Mocha and Chai to ensure the sample's
generateContent
function works as expected, asserting that the output is non-empty and contains 'AI'.
Changelog
Click here to see the changelog
- genai/test/textgen-with-txt-routing.test.js
- Added a new test file for the
textgen-with-txt-routing
sample. - Uses Mocha and Chai for testing.
- Tests the
generateContent
function, asserting non-empty output containing 'AI'.
- Added a new test file for the
- genai/text-generation/textgen-with-txt-routing.js
- Added a new sample file demonstrating text generation with routing configuration.
- Uses the
@google/genai
SDK. - Initializes
GoogleGenAI
withvertexai: true
. - Includes
modelSelectionConfig
withfeatureSelectionPreference: FeatureSelectionPreference.PRIORITIZE_COST
. - Calls
ai.models.generateContent
with a specific model and prompt. - Exports the
generateContent
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.
New code arrives, a sample bright,
For GenAI's power, day and night.
With routing set,
And tests we've met,
It shines within the code's pure light.
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
This pull request introduces new sample code for the Model Optimizer feature within Generative AI, which is a valuable addition. The code for the sample and its test is generally clear and demonstrates the intended functionality.
I have a couple of suggestions focused on improving adherence to common JavaScript conventions and enhancing the robustness of the sample code. These are detailed in the review comments.
Please also ensure that the checklist items in the pull request description (such as confirming tests pass and linting is successful) are completed before merging.
Summary of Findings
- JavaScript Naming Conventions: The variable
generate_content_config
ingenai/text-generation/textgen-with-txt-routing.js
uses snake_case. JavaScript typically uses camelCase (e.g.,generateContentConfig
) for better readability and consistency. (Severity: medium, commented) - Error Handling for API Calls: The
generateContent
function ingenai/text-generation/textgen-with-txt-routing.js
lacks explicit error handling for the API call toai.models.generateContent
. Adding a try-catch block would make the sample more robust. (Severity: medium, commented) - Test Suite Structure (Minor): The
describe
block in the test filegenai/test/textgen-with-txt-routing.test.js
(line 23) is markedasync
. This is generally unnecessary fordescribe
blocks in Mocha, as asynchronous setup/teardown or tests should be in hooks (before
,after
, etc.) orit
blocks. (Severity: low, not commented due to review settings) - Hardcoded Values in Sample (Minor): The sample
genai/text-generation/textgen-with-txt-routing.js
uses a hardcoded model name (model-optimizer-exp-04-09
) and prompt. This is acceptable for a focused sample demonstrating a specific feature but worth noting if adapting for broader use. (Severity: low, not commented due to review settings) - Copyright Year (Minor): The copyright year in the newly added files is listed as 2025 (e.g., line 1 of
genai/text-generation/textgen-with-txt-routing.js
). Please verify if this is intended or if it should be the current year. (Severity: low, not commented due to review settings)
Merge Readiness
I recommend addressing the medium-severity issues identified—specifically regarding JavaScript naming conventions and the addition of error handling—before this pull request is merged. These changes will enhance the code's quality, maintainability, and robustness, aligning it more closely with common best practices.
As a reviewer, I am not authorized to approve the pull request directly. Please ensure it undergoes further review and approval according to your team's established process once the suggested changes have been considered or implemented.
location: location, | ||
}); | ||
|
||
const generate_content_config = {modelSelectionConfig: {featureSelectionPreference: FeatureSelectionPreference.PRIORITIZE_COST}} |
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 adhering to JavaScript's idiomatic naming convention by using camelCase for variable names. For instance, generateContentConfig
would be more conventional than generate_content_config
.
This change improves consistency and readability, aligning with common practices in the JavaScript ecosystem and the general guidance of the Google Samples Style Guide towards idiomatic code.
const generate_content_config = {modelSelectionConfig: {featureSelectionPreference: FeatureSelectionPreference.PRIORITIZE_COST}} | |
const generateContentConfig = {modelSelectionConfig: {featureSelectionPreference: FeatureSelectionPreference.PRIORITIZE_COST}} |
const response = await ai.models.generateContent({ | ||
model: 'model-optimizer-exp-04-09', | ||
contents: 'How does AI work?', | ||
config: generate_content_config, | ||
}); | ||
|
||
console.log(response.text); | ||
|
||
return 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.
The API call to ai.models.generateContent
could potentially encounter errors (e.g., network issues, API misconfigurations, or service-side problems). Adding a try-catch
block would make this sample more robust and provide a more complete example for users who might adapt this code for their own applications.
While samples aim for conciseness, demonstrating basic error handling is often beneficial for illustrating best practices.
try {
const response = await ai.models.generateContent({
model: 'model-optimizer-exp-04-09',
contents: 'How does AI work?',
config: generate_content_config, // Note: If the variable name is changed based on other feedback, update here too.
});
console.log(response.text);
return response.text;
} catch (error) {
console.error('Failed to generate content:', error);
throw error; // Re-throw the error to allow callers to handle it or to fail visibly.
}
a22c123
to
5d720ed
Compare
Description
Fixes #
Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.
Checklist
npm test
(see Testing)npm run lint
(see Style)GoogleCloudPlatform/nodejs-docs-samples
. Not a fork.