Skip to content

Commit 62ddb7d

Browse files
rudrakhsha-cresttelpirionharshnasitcrestdavidcavazos
authored
feat(modelarmor): Added snippets for sanitization (#4051)
* Added CRUD code snippets with codeowners file * Solved linting errors * Added sanitization code snippets along with test cases * Added header comment * Removed hardcoded value from test file * Removed crud related files * Addressed gemini bot comments * Updated prompts, tests and log statements for sanitization APIs * chore(cloud-sql): migrate mysql, postgres samples to new CI (#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 (#4047) * chore(compute): Migrate region tag * chore(compute): Rename region tag * chore(compute): Renove older region tag (#4062) overriding failure as this is jsut removing the region tag. * feat: support testing on forked repos (#4057) * 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 (#4063) * 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 * chore: update the minimum language based on maintenance mode * chore(deps): pin dependencies (#4035) * Added region tags to snippets * Updated region tags for code snippets based on errors * Removed extra csam assertions in sanitization tests * Addressed review comments * Updated test cases for sanitization snippets, added test for rai filter * Updated tests as per review comments * updated commented variables sample values in code snippets * Addressed review comments and updated tests accordingly * removed-todo-developer-comments * add-new-line-eslintrc * fix-linting-warnings-and-errors * fixed-tests * revert-security-center-change * revert-security-center-change * revert-security-center-change * fix-minor-lint --------- Co-authored-by: Eric Schmidt <[email protected]> Co-authored-by: Harsh Nasit <[email protected]> Co-authored-by: harshnasitcrest <[email protected]> Co-authored-by: David Cavazos <[email protected]>
1 parent 5fa1ee2 commit 62ddb7d

File tree

7 files changed

+832
-8
lines changed

7 files changed

+832
-8
lines changed

model-armor/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"test": "c8 mocha -p -j 2 --recursive test/ --timeout=60000"
1515
},
1616
"dependencies": {
17-
"@google-cloud/modelarmor": "^0.1.0"
17+
"@google-cloud/modelarmor": "^0.1.0",
18+
"@google-cloud/dlp": "^5.0.0"
1819
},
1920
"devDependencies": {
2021
"c8": "^10.0.0",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Sanitizes a model response using Model Armor filters.
19+
*
20+
* @param {string} projectId - Google Cloud project ID where the template exists.
21+
* @param {string} locationId - Google Cloud location (region) of the template, e.g., 'us-central1'.
22+
* @param {string} templateId - Identifier of the template to use for sanitization.
23+
* @param {string} modelResponse - The text response from a model that needs to be sanitized.
24+
*/
25+
async function sanitizeModelResponse(
26+
projectId,
27+
locationId,
28+
templateId,
29+
modelResponse
30+
) {
31+
// [START modelarmor_sanitize_model_response]
32+
/**
33+
* TODO(developer): Uncomment these variables before running the sample.
34+
*/
35+
// const projectId = process.env.PROJECT_ID || 'your-project-id';
36+
// const locationId = process.env.LOCATION_ID || 'us-central1';
37+
// const templateId = process.env.TEMPLATE_ID || 'template-id';
38+
// const modelResponse = 'unsanitized model output';
39+
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
40+
41+
const client = new ModelArmorClient({
42+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
43+
});
44+
45+
const request = {
46+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
47+
modelResponseData: {
48+
text: modelResponse,
49+
},
50+
};
51+
52+
const [response] = await client.sanitizeModelResponse(request);
53+
console.log(JSON.stringify(response, null, 2));
54+
// [END modelarmor_sanitize_model_response]
55+
return response;
56+
}
57+
58+
module.exports = sanitizeModelResponse;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Sanitizes a model response with context from the original user prompt.
19+
*
20+
* @param {string} projectId - Google Cloud project ID where the template exists.
21+
* @param {string} locationId - Google Cloud location (region) of the template, e.g., 'us-central1'.
22+
* @param {string} templateId - Identifier of the template to use for sanitization.
23+
* @param {string} modelResponse - The text response from a model that needs to be sanitized.
24+
* @param {string} userPrompt - The original user prompt that generated the model response.
25+
*/
26+
async function sanitizeModelResponseWithUserPrompt(
27+
projectId,
28+
locationId,
29+
templateId,
30+
modelResponse,
31+
userPrompt
32+
) {
33+
// [START modelarmor_sanitize_model_response_with_user_prompt]
34+
/**
35+
* TODO(developer): Uncomment these variables before running the sample.
36+
*/
37+
// const projectId = process.env.PROJECT_ID || 'your-project-id';
38+
// const locationId = process.env.LOCATION_ID || 'us-central1';
39+
// const templateId = process.env.TEMPLATE_ID || 'template-id';
40+
// const modelResponse = 'unsanitized model output';
41+
// const userPrompt = 'unsafe user prompt';
42+
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
43+
44+
const client = new ModelArmorClient({
45+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
46+
});
47+
48+
const request = {
49+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
50+
modelResponseData: {
51+
text: modelResponse,
52+
},
53+
userPrompt: userPrompt,
54+
};
55+
56+
const [response] = await client.sanitizeModelResponse(request);
57+
console.log(JSON.stringify(response, null, 2));
58+
return response;
59+
// [END modelarmor_sanitize_model_response_with_user_prompt]
60+
}
61+
62+
module.exports = sanitizeModelResponseWithUserPrompt;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Sanitizes a user prompt using Model Armor filters.
19+
*
20+
* @param {string} projectId - Google Cloud project ID where the template exists.
21+
* @param {string} locationId - Google Cloud location (region) of the template.
22+
* @param {string} templateId - Identifier of the template to use for sanitization.
23+
* @param {string} userPrompt - The user's text prompt that needs to be sanitized.
24+
*/
25+
async function sanitizeUserPrompt(
26+
projectId,
27+
locationId,
28+
templateId,
29+
userPrompt
30+
) {
31+
// [START modelarmor_sanitize_user_prompt]
32+
/**
33+
* TODO(developer): Uncomment these variables before running the sample.
34+
*/
35+
// const projectId = process.env.PROJECT_ID || 'your-project-id';
36+
// const locationId = process.env.LOCATION_ID || 'us-central1';
37+
// const templateId = process.env.TEMPLATE_ID || 'template-id';
38+
// const userPrompt = 'unsafe user prompt';
39+
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
40+
41+
const client = new ModelArmorClient({
42+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
43+
});
44+
45+
const request = {
46+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
47+
userPromptData: {
48+
text: userPrompt,
49+
},
50+
};
51+
52+
const [response] = await client.sanitizeUserPrompt(request);
53+
console.log(JSON.stringify(response, null, 2));
54+
return response;
55+
// [END modelarmor_sanitize_user_prompt]
56+
}
57+
58+
module.exports = sanitizeUserPrompt;

model-armor/snippets/screenPdfFile.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Sanitize/Screen PDF file content using the Model Armor API.
19+
*
20+
* @param {string} projectId - Google Cloud project ID.
21+
* @param {string} locationId - Google Cloud location.
22+
* @param {string} templateId - The template ID used for sanitization.
23+
* @param {string} pdfContentFilename - Path to a PDF file.
24+
*/
25+
async function screenPdfFile(
26+
projectId,
27+
locationId,
28+
templateId,
29+
pdfContentFilename
30+
) {
31+
// [START modelarmor_screen_pdf_file]
32+
/**
33+
* TODO(developer): Uncomment these variables before running the sample.
34+
*/
35+
// const projectId = process.env.PROJECT_ID || 'your-project-id';
36+
// const locationId = process.env.LOCATION_ID || 'us-central1';
37+
// const templateId = process.env.TEMPLATE_ID || 'template-id';
38+
// const pdfContentFilename = 'path/to/file.pdf';
39+
40+
// Imports the Model Armor library
41+
const modelarmor = require('@google-cloud/modelarmor');
42+
const {ModelArmorClient} = modelarmor.v1;
43+
const {protos} = modelarmor;
44+
const ByteItemType =
45+
protos.google.cloud.modelarmor.v1.ByteDataItem.ByteItemType;
46+
47+
const fs = require('fs');
48+
49+
const pdfContent = fs.readFileSync(pdfContentFilename);
50+
const pdfContentBase64 = pdfContent.toString('base64');
51+
52+
const client = new ModelArmorClient({
53+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
54+
});
55+
56+
const request = {
57+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
58+
userPromptData: {
59+
byteItem: {
60+
byteDataType: ByteItemType.PDF,
61+
byteData: pdfContentBase64,
62+
},
63+
},
64+
};
65+
66+
const [response] = await client.sanitizeUserPrompt(request);
67+
console.log(JSON.stringify(response, null, 2));
68+
return response;
69+
// [END modelarmor_screen_pdf_file]
70+
}
71+
72+
module.exports = screenPdfFile;

0 commit comments

Comments
 (0)