Skip to content

Commit 71140eb

Browse files
Merge branch 'main' into model-armor-sanitization-snippets
2 parents f29917c + 5fa1ee2 commit 71140eb

File tree

4 files changed

+132
-4
lines changed

4 files changed

+132
-4
lines changed

model-armor/snippets/quickstart.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
* Quickstart example for using Google Cloud Model Armor to
19+
* create a template with RAI filters and sanitize content.
20+
*
21+
* @param {string} projectId - Google Cloud project ID.
22+
* @param {string} locationId - Google Cloud location.
23+
* @param {string} templateId - ID for the template to create.
24+
*/
25+
async function quickstart(
26+
projectId = 'my-project',
27+
locationId = 'us-central1',
28+
templateId = 'my-template'
29+
) {
30+
// [START modelarmor_quickstart]
31+
/**
32+
* TODO(developer): Uncomment these variables before running the sample.
33+
*/
34+
// const projectId = 'my-project';
35+
// const locationId = 'us-central1';
36+
// const templateId = 'my-template';
37+
38+
// Imports the Model Armor library
39+
const modelarmor = require('@google-cloud/modelarmor');
40+
const {ModelArmorClient} = modelarmor.v1;
41+
const {protos} = modelarmor;
42+
43+
const {RaiFilterType} = protos.google.cloud.modelarmor.v1;
44+
const {DetectionConfidenceLevel} = protos.google.cloud.modelarmor.v1;
45+
46+
// Instantiates a client
47+
const client = new ModelArmorClient({
48+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
49+
});
50+
51+
const parent = `projects/${projectId}/locations/${locationId}`;
52+
53+
// Build the Model Armor template with preferred filters
54+
// For more details on filters, refer to:
55+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
56+
const template = {
57+
filterConfig: {
58+
raiSettings: {
59+
raiFilters: [
60+
{
61+
filterType: RaiFilterType.DANGEROUS,
62+
confidenceLevel: DetectionConfidenceLevel.HIGH,
63+
},
64+
{
65+
filterType: RaiFilterType.HARASSMENT,
66+
confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
67+
},
68+
{
69+
filterType: RaiFilterType.HATE_SPEECH,
70+
confidenceLevel: DetectionConfidenceLevel.HIGH,
71+
},
72+
{
73+
filterType: RaiFilterType.SEXUALLY_EXPLICIT,
74+
confidenceLevel: DetectionConfidenceLevel.HIGH,
75+
},
76+
],
77+
},
78+
},
79+
};
80+
81+
const [createdTemplate] = await client.createTemplate({
82+
parent,
83+
templateId,
84+
template,
85+
});
86+
87+
// Sanitize a user prompt using the created template
88+
const userPrompt = 'Unsafe user prompt';
89+
90+
const [userPromptSanitizeResponse] = await client.sanitizeUserPrompt({
91+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
92+
userPromptData: {
93+
text: userPrompt,
94+
},
95+
});
96+
97+
// Sanitize a model response using the created template
98+
const modelResponse = 'Unsanitized model output';
99+
100+
const [modelSanitizeResponse] = await client.sanitizeModelResponse({
101+
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
102+
modelResponseData: {
103+
text: modelResponse,
104+
},
105+
});
106+
107+
return {
108+
templateName: createdTemplate.name,
109+
userPromptSanitizeResponse,
110+
modelSanitizeResponse,
111+
};
112+
// [END modelarmor_quickstart]
113+
}
114+
115+
module.exports = quickstart;

model-armor/test/modelarmor.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,19 @@ describe('Model Armor tests', () => {
303303
await deleteDlpTemplates();
304304
});
305305

306+
// =================== Quickstart Tests ===================
307+
308+
it('should create a template and sanitize content', async () => {
309+
const quickstart = require('../snippets/quickstart');
310+
const testQuickstartTemplateId = `${templateIdPrefix}-quickstart`;
311+
312+
await quickstart(projectId, locationId, testQuickstartTemplateId);
313+
314+
templatesToDelete.push(
315+
`projects/${projectId}/locations/${locationId}/templates/${testQuickstartTemplateId}`
316+
);
317+
});
318+
306319
// =================== RAI Filter Tests ===================
307320

308321
it('should sanitize user prompt with all RAI filter template', async () => {

secret-manager/regional_samples/iamGrantAccessWithRegionalSecret.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'use strict';
1616

1717
async function main(projectId, locationId, secretId, member) {
18-
// [START secretmanager_iam_grant_access_regional_secret]
18+
// [START secretmanager_iam_grant_access_with_regional_secret]
1919
/**
2020
* TODO(developer): Uncomment these variables before running the sample.
2121
*/
@@ -61,7 +61,7 @@ async function main(projectId, locationId, secretId, member) {
6161
}
6262

6363
grantAccessRegionalSecret();
64-
// [END secretmanager_iam_grant_access_regional_secret]
64+
// [END secretmanager_iam_grant_access_with_regional_secret]
6565
}
6666

6767
const args = process.argv.slice(2);

secret-manager/regional_samples/iamRevokeAccessWithRegionalSecret.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'use strict';
1616

1717
async function main(projectId, locationId, secretId, versionId, member) {
18-
// [START secretmanager_iam_revoke_access_regional_secret]
18+
// [START secretmanager_iam_revoke_access_with_regional_secret]
1919
/**
2020
* TODO(developer): Uncomment these variables before running the sample.
2121
*/
@@ -69,7 +69,7 @@ async function main(projectId, locationId, secretId, versionId, member) {
6969
}
7070

7171
grantAccessRegionalSecret();
72-
// [END secretmanager_iam_revoke_access_regional_secret]
72+
// [END secretmanager_iam_revoke_access_with_regional_secret]
7373
}
7474

7575
const args = process.argv.slice(2);

0 commit comments

Comments
 (0)