Skip to content

Commit 4295ff9

Browse files
rudrakhsha-crestharshnasitcresttelpirion
authored
feat(modelarmor): Added snippet for quickstart (#4052)
* Added CRUD code snippets with codeowners file * Solved linting errors * Added header comment * Added quickstart code snippet for model armor * Removed crud related files * Updated prompts in the quickstart template along with test cases * Removed extra functions from quickstart code snippets * Updated according to latest design in the other PRs after addressing review comments * add-new-line-copyright-fix * fix-lints --------- Co-authored-by: Harsh Nasit <[email protected]> Co-authored-by: harshnasitcrest <[email protected]> Co-authored-by: Eric Schmidt <[email protected]>
1 parent 737e0eb commit 4295ff9

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed

.github/blunderbuss.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ assign_issues_by:
4444
- "api: parametermanager"
4545
to:
4646
- GoogleCloudPlatform/cloud-parameters-team
47+
- labels:
48+
- "api: modelarmor"
49+
to:
50+
- GoogleCloudPlatform/cloud-modelarmor-team
4751

4852
assign_prs_by:
4953
- labels:
@@ -77,3 +81,7 @@ assign_prs_by:
7781
- "api: parametermanager"
7882
to:
7983
- GoogleCloudPlatform/cloud-parameters-team
84+
- labels:
85+
- "api: modelarmor"
86+
to:
87+
- GoogleCloudPlatform/cloud-modelarmor-team

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ document-warehouse @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPla
4444
ai-platform @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/text-embedding @GoogleCloudPlatform/cloud-samples-reviewers
4545
asset @GoogleCloudPlatform/cloud-asset-analysis-team @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
4646
dlp @GoogleCloudPlatform/googleapis-dlp @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
47+
model-armor @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-modelarmor-team
4748
security-center @GoogleCloudPlatform/gcp-security-command-center @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
4849
retail @GoogleCloudPlatform/cloud-retail-team @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
4950
media @GoogleCloudPlatform/cloud-media-team @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers

model-armor/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "nodejs-model-armor-samples",
3+
"private": true,
4+
"license": "Apache-2.0",
5+
"files": [
6+
"*.js"
7+
],
8+
"author": "Google LLC",
9+
"repository": "googleapis/nodejs-model-armor",
10+
"engines": {
11+
"node": ">=16.0.0"
12+
},
13+
"scripts": {
14+
"test": "c8 mocha -p -j 2 --recursive test/ --timeout=60000"
15+
},
16+
"dependencies": {
17+
"@google-cloud/modelarmor": "^0.1.0"
18+
},
19+
"devDependencies": {
20+
"c8": "^10.0.0",
21+
"chai": "^4.5.0",
22+
"mocha": "^10.0.0",
23+
"uuid": "^10.0.0"
24+
}
25+
}
26+

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/.eslintrc.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
---
16+
env:
17+
mocha: true

model-armor/test/modelarmor.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
const {v4: uuidv4} = require('uuid');
18+
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
19+
20+
let projectId;
21+
const locationId = process.env.GCLOUD_LOCATION || 'us-central1';
22+
const options = {
23+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
24+
};
25+
26+
const client = new ModelArmorClient(options);
27+
const templateIdPrefix = `test-template-${uuidv4().substring(0, 8)}`;
28+
29+
describe('Model Armor tests', () => {
30+
const templatesToDelete = [];
31+
32+
before(async () => {
33+
projectId = await client.getProjectId();
34+
});
35+
36+
after(async () => {
37+
// Clean up all templates
38+
for (const templateName of templatesToDelete) {
39+
try {
40+
await client.deleteTemplate({name: templateName});
41+
console.log(`Cleaned up template: ${templateName}`);
42+
} catch (error) {
43+
console.error(`Failed to delete template ${templateName}:`, error);
44+
}
45+
}
46+
});
47+
48+
// =================== Quickstart Tests ===================
49+
50+
it('should create a template and sanitize content', async () => {
51+
const quickstart = require('../snippets/quickstart');
52+
const testQuickstartTemplateId = `${templateIdPrefix}-quickstart`;
53+
54+
await quickstart(projectId, locationId, testQuickstartTemplateId);
55+
56+
templatesToDelete.push(
57+
`projects/${projectId}/locations/${locationId}/templates/${testQuickstartTemplateId}`
58+
);
59+
});
60+
});

0 commit comments

Comments
 (0)