-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Merged
utsav1810
merged 42 commits into
GoogleCloudPlatform:main
from
rudrakhsha-crest:model-armor-sanitization-snippets
May 19, 2025
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
874955b
Added CRUD code snippets with codeowners file
rudrakhsha-crest 8270ed7
Solved linting errors
rudrakhsha-crest 17f2eae
Added sanitization code snippets along with test cases
rudrakhsha-crest 58e1542
Added header comment
rudrakhsha-crest 1ba8375
Removed hardcoded value from test file
rudrakhsha-crest 944f6b2
Removed crud related files
rudrakhsha-crest 5e7b3ca
Merge branch 'main' into model-armor-sanitization-snippets
rudrakhsha-crest 2dd3bc1
Addressed gemini bot comments
rudrakhsha-crest 072b4aa
Merge branch 'model-armor-sanitization-snippets' of github.com:rudrak…
rudrakhsha-crest bda14a7
Updated prompts, tests and log statements for sanitization APIs
rudrakhsha-crest 8a662b5
chore(cloud-sql): migrate mysql, postgres samples to new CI (#4055)
rudrakhsha-crest f0470d6
chore(compute): Migrate region tag (#4047)
rudrakhsha-crest 5ffb243
chore(compute): Renove older region tag (#4062)
rudrakhsha-crest 5b4fc3c
feat: support testing on forked repos (#4057)
rudrakhsha-crest 27a2d51
fix: add id-token to workflows (#4063)
rudrakhsha-crest c7373ee
chore: update the minimum language based on maintenance mode
rudrakhsha-crest a24e395
chore(deps): pin dependencies (#4035)
rudrakhsha-crest 342078f
Added region tags to snippets
rudrakhsha-crest e21f39d
Merge branch 'main' into model-armor-sanitization-snippets
rudrakhsha-crest 31bb0c3
Updated region tags for code snippets based on errors
rudrakhsha-crest 00ea0f1
Merge branch 'model-armor-sanitization-snippets' of github.com:rudrak…
rudrakhsha-crest 57ec967
Removed extra csam assertions in sanitization tests
rudrakhsha-crest 6fdca28
Addressed review comments
rudrakhsha-crest 3cab31c
Updated test cases for sanitization snippets, added test for rai filter
rudrakhsha-crest 8894a90
Updated tests as per review comments
rudrakhsha-crest 1b2ff96
updated commented variables sample values in code snippets
rudrakhsha-crest b1231b6
Addressed review comments and updated tests accordingly
rudrakhsha-crest fe096ab
Merge branch 'main' into model-armor-sanitization-snippets
telpirion 2be1a60
removed-todo-developer-comments
harshnasitcrest fc773b3
add-new-line-eslintrc
harshnasitcrest fbb6fe9
fix-linting-warnings-and-errors
harshnasitcrest 16a94e4
fixed-tests
harshnasitcrest 5e2f8f9
Merge branch 'main' into model-armor-sanitization-snippets
harshnasitcrest 02f5121
Merge branch 'main' into model-armor-sanitization-snippets
07999c6
Merge branch 'main' into model-armor-sanitization-snippets
437c0a7
Merge branch 'main' into model-armor-sanitization-snippets
f29917c
Merge branch 'main' into model-armor-sanitization-snippets
71140eb
Merge branch 'main' into model-armor-sanitization-snippets
harshnasitcrest 96bd553
revert-security-center-change
harshnasitcrest e4d88d4
revert-security-center-change
harshnasitcrest 80f8897
revert-security-center-change
harshnasitcrest 745b75a
fix-minor-lint
harshnasitcrest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Sanitizes a model response using Model Armor filters. | ||
| * | ||
| * @param {string} projectId - Google Cloud project ID where the template exists. | ||
| * @param {string} locationId - Google Cloud location (region) of the template, e.g., 'us-central1'. | ||
| * @param {string} templateId - Identifier of the template to use for sanitization. | ||
| * @param {string} modelResponse - The text response from a model that needs to be sanitized. | ||
| */ | ||
| async function sanitizeModelResponse( | ||
| projectId, | ||
| locationId, | ||
| templateId, | ||
| modelResponse | ||
| ) { | ||
| // [START modelarmor_sanitize_model_response] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const projectId = process.env.PROJECT_ID || 'your-project-id'; | ||
| // const locationId = process.env.LOCATION_ID || 'us-central1'; | ||
| // const templateId = process.env.TEMPLATE_ID || 'template-id'; | ||
| // const modelResponse = 'unsanitized model output'; | ||
| const {ModelArmorClient} = require('@google-cloud/modelarmor').v1; | ||
|
|
||
| const client = new ModelArmorClient({ | ||
| apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`, | ||
| }); | ||
|
|
||
| const request = { | ||
| name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, | ||
| modelResponseData: { | ||
| text: modelResponse, | ||
| }, | ||
| }; | ||
|
|
||
| const [response] = await client.sanitizeModelResponse(request); | ||
| console.log(JSON.stringify(response, null, 2)); | ||
| // [END modelarmor_sanitize_model_response] | ||
| return response; | ||
| } | ||
|
|
||
| module.exports = sanitizeModelResponse; | ||
62 changes: 62 additions & 0 deletions
62
model-armor/snippets/sanitizeModelResponseWithUserPrompt.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Sanitizes a model response with context from the original user prompt. | ||
| * | ||
| * @param {string} projectId - Google Cloud project ID where the template exists. | ||
| * @param {string} locationId - Google Cloud location (region) of the template, e.g., 'us-central1'. | ||
| * @param {string} templateId - Identifier of the template to use for sanitization. | ||
| * @param {string} modelResponse - The text response from a model that needs to be sanitized. | ||
| * @param {string} userPrompt - The original user prompt that generated the model response. | ||
| */ | ||
| async function sanitizeModelResponseWithUserPrompt( | ||
| projectId, | ||
| locationId, | ||
| templateId, | ||
| modelResponse, | ||
| userPrompt | ||
| ) { | ||
| // [START modelarmor_sanitize_model_response_with_user_prompt] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const projectId = process.env.PROJECT_ID || 'your-project-id'; | ||
| // const locationId = process.env.LOCATION_ID || 'us-central1'; | ||
| // const templateId = process.env.TEMPLATE_ID || 'template-id'; | ||
| // const modelResponse = 'unsanitized model output'; | ||
| // const userPrompt = 'unsafe user prompt'; | ||
| const {ModelArmorClient} = require('@google-cloud/modelarmor').v1; | ||
|
|
||
| const client = new ModelArmorClient({ | ||
| apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`, | ||
| }); | ||
|
|
||
| const request = { | ||
| name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, | ||
| modelResponseData: { | ||
| text: modelResponse, | ||
| }, | ||
| userPrompt: userPrompt, | ||
| }; | ||
|
|
||
| const [response] = await client.sanitizeModelResponse(request); | ||
| console.log(JSON.stringify(response, null, 2)); | ||
| return response; | ||
| // [END modelarmor_sanitize_model_response_with_user_prompt] | ||
| } | ||
|
|
||
| module.exports = sanitizeModelResponseWithUserPrompt; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Sanitizes a user prompt using Model Armor filters. | ||
| * | ||
| * @param {string} projectId - Google Cloud project ID where the template exists. | ||
| * @param {string} locationId - Google Cloud location (region) of the template. | ||
| * @param {string} templateId - Identifier of the template to use for sanitization. | ||
| * @param {string} userPrompt - The user's text prompt that needs to be sanitized. | ||
| */ | ||
| async function sanitizeUserPrompt( | ||
| projectId, | ||
| locationId, | ||
| templateId, | ||
| userPrompt | ||
| ) { | ||
| // [START modelarmor_sanitize_user_prompt] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const projectId = process.env.PROJECT_ID || 'your-project-id'; | ||
| // const locationId = process.env.LOCATION_ID || 'us-central1'; | ||
| // const templateId = process.env.TEMPLATE_ID || 'template-id'; | ||
| // const userPrompt = 'unsafe user prompt'; | ||
| const {ModelArmorClient} = require('@google-cloud/modelarmor').v1; | ||
|
|
||
| const client = new ModelArmorClient({ | ||
| apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`, | ||
| }); | ||
|
|
||
| const request = { | ||
| name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, | ||
| userPromptData: { | ||
| text: userPrompt, | ||
| }, | ||
| }; | ||
|
|
||
| const [response] = await client.sanitizeUserPrompt(request); | ||
| console.log(JSON.stringify(response, null, 2)); | ||
| return response; | ||
| // [END modelarmor_sanitize_user_prompt] | ||
| } | ||
|
|
||
| module.exports = sanitizeUserPrompt; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Sanitize/Screen PDF file content using the Model Armor API. | ||
| * | ||
| * @param {string} projectId - Google Cloud project ID. | ||
| * @param {string} locationId - Google Cloud location. | ||
| * @param {string} templateId - The template ID used for sanitization. | ||
| * @param {string} pdfContentFilename - Path to a PDF file. | ||
| */ | ||
| async function screenPdfFile( | ||
| projectId, | ||
| locationId, | ||
| templateId, | ||
| pdfContentFilename | ||
| ) { | ||
| // [START modelarmor_screen_pdf_file] | ||
| /** | ||
| * TODO(developer): Uncomment these variables before running the sample. | ||
| */ | ||
| // const projectId = process.env.PROJECT_ID || 'your-project-id'; | ||
| // const locationId = process.env.LOCATION_ID || 'us-central1'; | ||
| // const templateId = process.env.TEMPLATE_ID || 'template-id'; | ||
| // const pdfContentFilename = 'path/to/file.pdf'; | ||
|
|
||
| // Imports the Model Armor library | ||
| const modelarmor = require('@google-cloud/modelarmor'); | ||
| const {ModelArmorClient} = modelarmor.v1; | ||
| const {protos} = modelarmor; | ||
| const ByteItemType = | ||
| protos.google.cloud.modelarmor.v1.ByteDataItem.ByteItemType; | ||
|
|
||
| const fs = require('fs'); | ||
|
|
||
| const pdfContent = fs.readFileSync(pdfContentFilename); | ||
| const pdfContentBase64 = pdfContent.toString('base64'); | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const client = new ModelArmorClient({ | ||
| apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`, | ||
| }); | ||
|
|
||
| const request = { | ||
| name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, | ||
| userPromptData: { | ||
| byteItem: { | ||
| byteDataType: ByteItemType.PDF, | ||
| byteData: pdfContentBase64, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const [response] = await client.sanitizeUserPrompt(request); | ||
| console.log(JSON.stringify(response, null, 2)); | ||
| return response; | ||
| // [END modelarmor_screen_pdf_file] | ||
| } | ||
|
|
||
| module.exports = screenPdfFile; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.