Skip to content

feat(modelarmor): Added snippets for sanitization #4051

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 28 commits into
base: main
Choose a base branch
from

Conversation

rudrakhsha-crest
Copy link

@rudrakhsha-crest rudrakhsha-crest commented Mar 28, 2025

Description

Created samples for user prompt and model response sanitation using model armor APIs.

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)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
    Model Armor API
  • 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

@rudrakhsha-crest rudrakhsha-crest requested review from a team as code owners March 28, 2025 12:20
Copy link

snippet-bot bot commented Mar 28, 2025

Here is the summary of changes.

You are about to add 4 region tags.

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

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Mar 28, 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 @rudrakhsha-crest, 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 introduces snippets for sanitizing user prompts and model responses using the Model Armor API. It includes code for creating templates with various configurations, such as RAI filters, SDP settings, labels, and metadata. The snippets demonstrate how to sanitize user prompts and model responses, including PDF content, and how to manage templates (create, delete, get, list, update). The changes also include updates to the .github/blunderbuss.yml and CODEOWNERS files to include the model-armor API.

Highlights

  • New snippets: Adds new snippets for creating Model Armor templates with RAI filters, SDP settings, labels, and metadata.
  • Sanitization: Introduces snippets for sanitizing user prompts and model responses, including PDF content, using Model Armor templates.
  • Template management: Provides snippets for managing Model Armor templates (create, delete, get, list, update).
  • Configuration: Updates .github/blunderbuss.yml and CODEOWNERS to include the model-armor API.

Changelog

Click here to see the changelog
  • .github/blunderbuss.yml
    • Adds api: modelarmor to the issue and PR auto-assigner, associating it with the GoogleCloudPlatform/cloud-modelarmor-team.
  • CODEOWNERS
    • Adds model-armor to the CODEOWNERS file, assigning ownership to @GoogleCloudPlatform/nodejs-samples-reviewers, @GoogleCloudPlatform/cloud-samples-reviewers, and @GoogleCloudPlatform/cloud-modelarmor-team.
  • model-armor/package.json
    • Creates a package.json file for the model-armor samples, defining dependencies, scripts, and metadata.
  • model-armor/snippets/createTemplate.js
    • Adds a snippet for creating a Model Armor template with Responsible AI (RAI) filters.
  • model-armor/snippets/createTemplateWithAdvancedSdp.js
    • Adds a snippet for creating a Model Armor template with advanced SDP settings enabled.
  • model-armor/snippets/createTemplateWithBasicSdp.js
    • Adds a snippet for creating a Model Armor template with basic SDP settings enabled.
  • model-armor/snippets/createTemplateWithLabels.js
    • Adds a snippet for creating a Model Armor template with Responsible AI (RAI) filters and custom labels.
  • model-armor/snippets/createTemplateWithMetadata.js
    • Adds a snippet for creating a Model Armor template with template metadata.
  • model-armor/snippets/deleteTemplate.js
    • Adds a snippet for deleting a Model Armor template.
  • model-armor/snippets/getTemplate.js
    • Adds a snippet for retrieving a Model Armor template by its ID.
  • model-armor/snippets/listTemplates.js
    • Adds a snippet for listing all Model Armor templates in a specified project and location.
  • model-armor/snippets/listTemplatesWithFilter.js
    • Adds a snippet for listing Model Armor templates that match a specific filter criteria.
  • model-armor/snippets/sanitizeModelResponse.js
    • Adds a snippet for sanitizing a model response using Model Armor filters.
  • model-armor/snippets/sanitizeModelResponseWithUserPrompt.js
    • Adds a snippet for sanitizing a model response with context from the original user prompt.
  • model-armor/snippets/sanitizeUserPrompt.js
    • Adds a snippet for sanitizing a user prompt using Model Armor filters.
  • model-armor/snippets/screenPdfFile.js
    • Adds a snippet for sanitizing/screening PDF content using the Model Armor API.
  • model-armor/snippets/updateTemplate.js
    • Adds a snippet for updating an existing Model Armor template.
  • model-armor/snippets/updateTemplateLabels.js
    • Adds a snippet for updating the labels of an existing Model Armor template.
  • model-armor/snippets/updateTemplateMetadata.js
    • Adds a snippet for updating the metadata of an existing Model Armor template.
  • model-armor/snippets/updateTemplateWithMaskConfiguration.js
    • Adds a snippet for updating an existing Model Armor template with a specific update mask.
  • model-armor/test/.eslintrc.yml
    • Adds an .eslintrc.yml file for the model-armor tests, configuring the environment for Mocha.
  • model-armor/test/modelarmor.test.js
    • Adds a test suite for the Model Armor snippets, covering user prompt sanitization, model response sanitization, and PDF sanitization.
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.


Did you know?

The concept of using filters to sanitize content dates back to ancient libraries, where librarians would curate and restrict access to certain texts based on their content.

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.

@rudrakhsha-crest rudrakhsha-crest changed the title feat(model-armor) - Snippets for User prompt and Model response sanitization feat(modelarmor):Snippets for User prompt and Model response sanitization Mar 28, 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.

Code Review

This pull request introduces snippets for user prompt and model response sanitization using the Model Armor API. The changes include adding new files for various sanitization scenarios and updating the blunderbuss configuration and CODEOWNERS file. Overall, the code seems well-structured and addresses the intended functionality. However, there are a few areas that could be improved for clarity and maintainability.

Summary of Findings

  • Missing Error Handling: The code lacks explicit error handling for API calls and other operations, which could lead to unhandled exceptions and unexpected behavior. It's important to add error handling to ensure the application is robust and provides informative error messages.
  • Copyright Year: The copyright year is set to 2025 in all the new files. Please confirm if this is the correct year, or if it should be updated to the current year.
  • TODO comments: The code contains TODO comments that should be addressed before merging. These comments indicate areas where the code needs further implementation or clarification.

Merge Readiness

The pull request is not quite ready for merging. There are a few issues that need to be addressed before merging, including adding error handling, confirming the copyright year, and addressing the TODO comments. I am unable to approve this pull request, and recommend that another reviewer also take a look at this code before merging.

@rudrakhsha-crest rudrakhsha-crest changed the title feat(modelarmor):Snippets for User prompt and Model response sanitization feat(modelarmor): Added samples for user prompt and model response sanitization in model armor Mar 28, 2025
Copy link
Contributor

@telpirion telpirion left a comment

Choose a reason for hiding this comment

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

See internal discussion.

@telpirion telpirion marked this pull request as draft April 11, 2025 20:08
@telpirion telpirion self-assigned this Apr 11, 2025
@telpirion telpirion added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Apr 11, 2025
@rudrakhsha-crest rudrakhsha-crest changed the title feat(modelarmor): Added samples for user prompt and model response sanitization in model armor feat(modelarmor): Added snippets for sanitization Apr 18, 2025
@rudrakhsha-crest rudrakhsha-crest force-pushed the model-armor-sanitization-snippets branch 7 times, most recently from 765ab68 to 7ebf8e9 Compare April 18, 2025 12:28
…oudPlatform#4055)

* chore(cloud-sql): migrate mysql, postgres samples to new CI

* rename to match postgres version

* acquireTimeout warns in mysql2, remove

* mysql2 query returns [results, fields], so only return fields

* remove unused test scripts

* revert bot-suggested rename: would cause excessive region tag change
* chore(compute): Migrate region tag

* chore(compute): Rename region tag
overriding failure as this is jsut removing the region tag.
* feat: support testing on forked repos

* add pull_request to test on PR

* remove prod suffix

* decode json on matrix paths

* add experimental to check names

* add experimental to job names

* run test only if paths is not empty

* test explicitly against empty array

* move experimental as a prefix

* success check if no paths are found

* always set check to done

* remove pull_request trigger

* rearrange if check

* add more comments
* feat: support testing on forked repos

* add pull_request to test on PR

* remove prod suffix

* decode json on matrix paths

* add experimental to check names

* add experimental to job names

* run test only if paths is not empty

* test explicitly against empty array

* move experimental as a prefix

* success check if no paths are found

* always set check to done

* remove pull_request trigger

* rearrange if check

* add more comments

* fix: add id-token to workflows
@rudrakhsha-crest rudrakhsha-crest force-pushed the model-armor-sanitization-snippets branch from 7ebf8e9 to 342078f Compare April 18, 2025 12:29
@rudrakhsha-crest rudrakhsha-crest marked this pull request as ready for review April 18, 2025 12:32
Copy link

@utsav1810 utsav1810 left a comment

Choose a reason for hiding this comment

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

Please make sure there the prompts are exactly same as in the python samples repo. I have highlighted a couple of differences here. Please double check other prompts as well.

Copy link

@harshnasitcrest harshnasitcrest left a comment

Choose a reason for hiding this comment

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

LGTM ✅
Verified that all user prompts and model responses are sourced from Python samples.


---
env:
mocha: true
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: add new line at bottom of file.

@telpirion telpirion removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Apr 24, 2025
Copy link
Contributor

@telpirion telpirion left a comment

Choose a reason for hiding this comment

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

Looking much better! Thank you so much.

I left a couple of small comments for your next pass--accept or reject as you see fit. Once I see your acknowledgement of those comments, I will manage merging this PR for you.


module.exports = sanitizeModelResponse;

// TODO(developer): Uncomment below lines before running the sample.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I would cut this. Although this is a nice-to-have for developers who might want to run this sample as main, it's a little unsightly to have this commented out.

Up to you whether to keep or cut.


module.exports = sanitizeModelResponseWithUserPrompt;

// TODO(developer): Uncomment below lines before running the sample.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: same as previous.


module.exports = sanitizeUserPrompt;

// TODO(developer): Uncomment below lines before running the sample.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: same as previous.

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.

4 participants