Skip to content

feat(modelarmor): Added sanitization modelarmor code snippets #5267

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

Conversation

tirthrajsinh-zala-crest
Copy link
Contributor

@tirthrajsinh-zala-crest tirthrajsinh-zala-crest commented Apr 8, 2025

Description

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

Checklist

  • I have followed Contributing Guidelines from CONTRIBUTING.MD
  • Tests pass: go test -v ./.. (see Testing)
  • Code formatted: gofmt (see Formatting)
  • Vetting pass: go vet (see Formatting)
  • 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 sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners 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

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Apr 8, 2025
Copy link

@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 @tirthrajsinh-zala-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 new code snippets for sanitizing user prompts, model responses, and PDF files using the Model Armor API. It includes a new file for creating Model Armor templates, along with corresponding tests and necessary module updates. The changes aim to provide examples of how to use Model Armor to protect against harmful content in user inputs and model outputs.

Highlights

  • New Functionality: Adds code snippets for sanitizing model responses with and without user prompts, as well as for screening PDF files.
  • Template Creation: Introduces a function to create Model Armor templates with predefined filters for PI, jailbreak attempts, and malicious URIs.
  • Testing: Includes comprehensive tests for the new sanitization and screening functionalities, ensuring proper integration and functionality.
  • Module Updates: Updates the go.mod and go.sum files to include the necessary dependencies for the Model Armor API.

Changelog

Click here to see the changelog
  • modelarmor/create_template.go
    • Introduces a new function createModelArmorTemplate to create Model Armor templates with specified filters.
    • Includes example usage and documentation for creating templates.
  • modelarmor/go.mod
    • Adds dependencies for cloud.google.com/go/modelarmor, github.com/google/uuid, google.golang.org/api, google.golang.org/grpc, and google.golang.org/protobuf.
    • Updates the Go module version to 1.23.0.
  • modelarmor/go.sum
    • Adds checksums for the new dependencies introduced in go.mod.
  • modelarmor/modelarmor_test.go
    • Adds tests for sanitizing model responses, user prompts, and screening PDF files.
    • Includes helper functions for creating and cleaning up Model Armor templates.
    • Uses testutil.SystemTest for integration testing with the Google Cloud environment.
  • modelarmor/sanitize_model_response.go
    • Introduces a new function sanitizeModelResponse to sanitize model responses using a Model Armor template.
    • Includes example usage and documentation for sanitizing model responses.
  • modelarmor/sanitize_model_response_with_user_prompt.go
    • Introduces a new function sanitizeModelResponseWithUserPrompt to sanitize model responses with a user prompt using a Model Armor template.
    • Includes example usage and documentation for sanitizing model responses with user prompts.
  • modelarmor/sanitize_user_prompt.go
    • Introduces a new function sanitizeUserPrompt to sanitize user prompts using a Model Armor template.
    • Includes example usage and documentation for sanitizing user prompts.
  • modelarmor/screen_pdf_file.go
    • Introduces a new function screenPDFFile to screen PDF file content using a Model Armor template.
    • Includes example usage and documentation for screening PDF files.
  • modelarmor/testdata/env/test.env
    • Adds environment variables for GOLANG_SAMPLES_ORGANIZATION_ID and GOLANG_SAMPLES_FOLDER_ID.
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 template's born,
Filters rise, armor is sworn,
To guard the model.

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

@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 pull request introduces new functionalities for sanitizing model responses, user prompts, and screening PDF files using the Model Armor API. The code includes examples for creating Model Armor templates and performing sanitization tasks. Overall, the code is well-structured and easy to follow. However, there are some areas that could be improved for better error handling and code clarity.

Summary of Findings

  • Error Handling: The code uses fmt.Errorf for error wrapping, which is good. However, consider adding more context to the error messages to provide better debugging information.
  • Test Environment: The test suite relies on environment variables and external files. Ensure that these dependencies are properly documented and handled to avoid test failures in different environments.
  • Redundant Imports: The modelarmor_test.go file contains a commented-out import statement for modelarmorpb. Remove this unused import to reduce clutter.

Merge Readiness

The pull request is almost ready for merging. Before merging, address the high and medium severity issues to ensure the code is robust and maintainable. I am unable to directly approve this pull request, and users should have others review and approve this code before merging.

@tirthrajsinh-zala-crest tirthrajsinh-zala-crest marked this pull request as ready for review April 9, 2025 10:06
@tirthrajsinh-zala-crest tirthrajsinh-zala-crest requested review from a team as code owners April 9, 2025 10:06
Copy link

snippet-bot bot commented Apr 9, 2025

Here is the summary of changes.

You are about to add 5 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

@telpirion
Copy link
Collaborator

Please split this PR into multiple PRs of <= 500 LoC each. Thank you.

@telpirion telpirion marked this pull request as draft April 10, 2025 16:58
@tirthrajsinh-zala-crest
Copy link
Contributor Author

Split this current PR into the smaller PR chunks as listed below, hence closing this PR.

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.

2 participants