Skip to content

Commit 874955b

Browse files
Added CRUD code snippets with codeowners file
1 parent 1099bd7 commit 874955b

18 files changed

+1468
-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+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
* Creates a Model Armor template with Responsible AI (RAI) filters.
19+
*
20+
* This function creates a template that can be used for sanitizing user prompts and model responses.
21+
*
22+
* @param {string} projectId - Google Cloud project ID where the template will be created.
23+
* @param {string} locationId - Google Cloud location (region) for the template, e.g., 'us-central1'.
24+
* @param {string} templateId - Unique identifier for the new template.
25+
*/
26+
async function main(projectId, locationId, templateId) {
27+
// [START modelarmor_create_template]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.
30+
*/
31+
// const projectId = 'your-project-id';
32+
// const locationId = 'us-central1';
33+
// const templateId = 'your-template-id';
34+
35+
const parent = `projects/${projectId}/locations/${locationId}`;
36+
37+
// Imports the Model Armor library
38+
const modelarmor = require('@google-cloud/modelarmor');
39+
const {ModelArmorClient} = modelarmor.v1;
40+
const {protos} = modelarmor;
41+
42+
// Instantiates a client
43+
const client = new ModelArmorClient({
44+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
45+
});
46+
47+
async function createTemplate() {
48+
/** Build the Model Armor template with your preferred filters.
49+
For more details on filters, please refer to the following doc:
50+
https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
51+
*/
52+
const templateConfig = {
53+
filterConfig: {
54+
raiSettings: {
55+
raiFilters: [
56+
{
57+
filterType:
58+
protos.google.cloud.modelarmor.v1.RaiFilterType.HATE_SPEECH,
59+
confidenceLevel:
60+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH,
61+
},
62+
{
63+
filterType:
64+
protos.google.cloud.modelarmor.v1.RaiFilterType
65+
.SEXUALLY_EXPLICIT,
66+
confidenceLevel:
67+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel
68+
.MEDIUM_AND_ABOVE,
69+
},
70+
],
71+
},
72+
},
73+
};
74+
75+
// Construct request
76+
const request = {
77+
parent,
78+
templateId,
79+
template: templateConfig,
80+
};
81+
82+
// Create the template
83+
const [response] = await client.createTemplate(request);
84+
console.log(`Created template: ${response.name}`);
85+
}
86+
87+
createTemplate();
88+
// [END modelarmor_create_template]
89+
}
90+
91+
const args = process.argv.slice(2);
92+
main(...args).catch(console.error);
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
* Creates a new model armor template with advanced SDP settings enabled.
19+
*
20+
* @param {string} projectId - Google Cloud project ID where the template will be created.
21+
* @param {string} locationId - Google Cloud location where the template will be created.
22+
* @param {string} templateId - ID for the template to create.
23+
* @param {string} inspectTemplate - Optional. Sensitive Data Protection inspect template resource name.
24+
If only inspect template is provided (de-identify template
25+
not provided), then Sensitive Data Protection InspectContent
26+
action is performed during Sanitization. All Sensitive Data
27+
Protection findings identified during inspection will be
28+
returned as SdpFinding in SdpInsepctionResult e.g.
29+
`organizations/{organization}/inspectTemplates/{inspect_template}`,
30+
`projects/{project}/inspectTemplates/{inspect_template}`
31+
`organizations/{organization}/locations/{location}/inspectTemplates/{inspect_template}`
32+
`projects/{project}/locations/{location}/inspectTemplates/{inspect_template}`
33+
* @param {string} deidentifyTemplate - Optional. Optional Sensitive Data Protection Deidentify template resource name.
34+
If provided then DeidentifyContent action is performed
35+
during Sanitization using this template and inspect
36+
template. The De-identified data will be returned in
37+
SdpDeidentifyResult. Note that all info-types present in the
38+
deidentify template must be present in inspect template.
39+
e.g.
40+
`organizations/{organization}/deidentifyTemplates/{deidentify_template}`,
41+
`projects/{project}/deidentifyTemplates/{deidentify_template}`
42+
`organizations/{organization}/locations/{location}/deidentifyTemplates/{deidentify_template}`
43+
`projects/{project}/locations/{location}/deidentifyTemplates/{deidentify_template}`
44+
*/
45+
async function main(
46+
projectId,
47+
locationId,
48+
templateId,
49+
inspectTemplate,
50+
deidentifyTemplate
51+
) {
52+
// [START modelarmor_create_template_with_advanced_sdp]
53+
/**
54+
* TODO(developer): Uncomment these variables before running the sample.
55+
*/
56+
// const projectId = 'your-project-id';
57+
// const locationId = 'us-central1';
58+
// const templateId = 'template-id';
59+
// const inspectTemplate = `projects/${projectId}/locations/${locationId}/inspectTemplates/inspect-template-id`;
60+
// const deidentifyTemplate = `projects/${projectId}/locations/${locationId}/deidentifyTemplates/deidentify-template-id`;
61+
62+
const parent = `projects/${projectId}/locations/${locationId}`;
63+
64+
// Imports the Model Armor library
65+
const modelarmor = require('@google-cloud/modelarmor');
66+
const {ModelArmorClient} = modelarmor.v1;
67+
const {protos} = modelarmor;
68+
69+
const RaiFilterType = protos.google.cloud.modelarmor.v1.RaiFilterType;
70+
const DetectionConfidenceLevel =
71+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
72+
73+
// Instantiates a client
74+
const client = new ModelArmorClient({
75+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
76+
});
77+
78+
async function createTemplateWithAdvancedSdp() {
79+
// Configuration for the template with advanced SDP settings
80+
const templateConfig = {
81+
filterConfig: {
82+
raiSettings: {
83+
raiFilters: [
84+
{
85+
filterType: RaiFilterType.DANGEROUS,
86+
confidenceLevel: DetectionConfidenceLevel.HIGH,
87+
},
88+
{
89+
filterType: RaiFilterType.HARASSMENT,
90+
confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
91+
},
92+
{
93+
filterType: RaiFilterType.HATE_SPEECH,
94+
confidenceLevel: DetectionConfidenceLevel.HIGH,
95+
},
96+
{
97+
filterType: RaiFilterType.SEXUALLY_EXPLICIT,
98+
confidenceLevel: DetectionConfidenceLevel.HIGH,
99+
},
100+
],
101+
},
102+
sdpSettings: {
103+
advancedConfig: {
104+
inspectTemplate: inspectTemplate,
105+
deidentifyTemplate: deidentifyTemplate,
106+
},
107+
},
108+
},
109+
};
110+
111+
// Construct request
112+
const request = {
113+
parent,
114+
templateId,
115+
template: templateConfig,
116+
};
117+
118+
// Create the template
119+
const [response] = await client.createTemplate(request);
120+
console.log(`Created template: ${response.name}`);
121+
}
122+
123+
createTemplateWithAdvancedSdp();
124+
// [END modelarmor_create_template_with_advanced_sdp]
125+
}
126+
127+
// Check if this script is being run directly
128+
const args = process.argv.slice(2);
129+
main(...args).catch(console.error);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
* Creates a new model armor template with basic SDP settings enabled.
19+
*
20+
* @param {string} projectId - Google Cloud project ID where the template will be created.
21+
* @param {string} locationId - Google Cloud location where the template will be created.
22+
* @param {string} templateId - ID for the template to create.
23+
*/
24+
async function main(projectId, locationId, templateId) {
25+
// [START modelarmor_create_template_with_basic_sdp]
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.
28+
*/
29+
// const projectId = 'your-project-id';
30+
// const locationId = 'us-central1';
31+
// const templateId = 'template-id';
32+
33+
const parent = `projects/${projectId}/locations/${locationId}`;
34+
35+
// Imports the Model Armor library
36+
const modelarmor = require('@google-cloud/modelarmor');
37+
const {ModelArmorClient} = modelarmor.v1;
38+
const {protos} = modelarmor;
39+
40+
const RaiFilterType = protos.google.cloud.modelarmor.v1.RaiFilterType;
41+
const DetectionConfidenceLevel =
42+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
43+
const SdpBasicConfigEnforcement =
44+
protos.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement;
45+
46+
// Instantiates a client
47+
const client = new ModelArmorClient({
48+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
49+
});
50+
51+
async function createTemplateWithBasicSdp() {
52+
// Configuration for the template with basic SDP settings
53+
const templateConfig = {
54+
filterConfig: {
55+
raiSettings: {
56+
raiFilters: [
57+
{
58+
filterType: RaiFilterType.DANGEROUS,
59+
confidenceLevel: DetectionConfidenceLevel.HIGH,
60+
},
61+
{
62+
filterType: RaiFilterType.HARASSMENT,
63+
confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
64+
},
65+
{
66+
filterType: RaiFilterType.HATE_SPEECH,
67+
confidenceLevel: DetectionConfidenceLevel.HIGH,
68+
},
69+
{
70+
filterType: RaiFilterType.SEXUALLY_EXPLICIT,
71+
confidenceLevel: DetectionConfidenceLevel.HIGH,
72+
},
73+
],
74+
},
75+
sdpSettings: {
76+
basicConfig: {
77+
filterEnforcement: SdpBasicConfigEnforcement.ENABLED,
78+
},
79+
},
80+
},
81+
};
82+
83+
// Construct request
84+
const request = {
85+
parent,
86+
templateId,
87+
template: templateConfig,
88+
};
89+
90+
const [response] = await client.createTemplate(request);
91+
console.log(`Created template: ${response.name}`);
92+
}
93+
94+
return createTemplateWithBasicSdp();
95+
// [END modelarmor_create_template_with_basic_sdp]
96+
}
97+
98+
const args = process.argv.slice(2);
99+
main(...args).catch(console.error);

0 commit comments

Comments
 (0)