Skip to content

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

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

Conversation

nardosalemu
Copy link

@nardosalemu nardosalemu commented Jun 4, 2025

Description

Fixes #

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • Required CI tests pass (see CI testing)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

Note: Any check with (dev), (experimental), or (legacy) can be ignored and should not block your PR from merging (see CI testing).

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Jun 4, 2025
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 @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 a modelSelectionConfig with featureSelectionPreference set to PRIORITIZE_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'.
  • 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 with vertexai: true.
    • Includes modelSelectionConfig with featureSelectionPreference: 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

  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

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 in genai/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 in genai/text-generation/textgen-with-txt-routing.js lacks explicit error handling for the API call to ai.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 file genai/test/textgen-with-txt-routing.test.js (line 23) is marked async. This is generally unnecessary for describe blocks in Mocha, as asynchronous setup/teardown or tests should be in hooks (before, after, etc.) or it 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}}
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 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.

Suggested change
const generate_content_config = {modelSelectionConfig: {featureSelectionPreference: FeatureSelectionPreference.PRIORITIZE_COST}}
const generateContentConfig = {modelSelectionConfig: {featureSelectionPreference: FeatureSelectionPreference.PRIORITIZE_COST}}

Comment on lines 35 to 47
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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

@nardosalemu nardosalemu force-pushed the main branch 7 times, most recently from a22c123 to 5d720ed Compare June 4, 2025 22:55
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.

1 participant