Skip to content

Commit 8a6e111

Browse files
rudrakhsha-crestglasntharshnasitcrest
authored
feat(modelarmor): Added snippets for creating, listing, updating and deleting templates (#4050)
* Added CRUD code snippets with codeowners file * Solved linting errors * Added header comment * Removed hardcoded value from test * Updated code snippets and tests as per comments given in other PRs * add-endofline-fix-copyright-year * update-template-metadata-consistent-with-python-snippet * resolve-merge-conflicts * fix-tests --------- Co-authored-by: Katie McLaughlin <[email protected]> Co-authored-by: Harsh Nasit <[email protected]> Co-authored-by: harshnasitcrest <[email protected]>
1 parent 62ddb7d commit 8a6e111

14 files changed

+1254
-16
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 createTemplate(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+
/** Build the Model Armor template with your preferred filters.
48+
For more details on filters, please refer to the following doc:
49+
https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
50+
*/
51+
const templateConfig = {
52+
filterConfig: {
53+
raiSettings: {
54+
raiFilters: [
55+
{
56+
filterType:
57+
protos.google.cloud.modelarmor.v1.RaiFilterType.HATE_SPEECH,
58+
confidenceLevel:
59+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH,
60+
},
61+
{
62+
filterType:
63+
protos.google.cloud.modelarmor.v1.RaiFilterType.SEXUALLY_EXPLICIT,
64+
confidenceLevel:
65+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel
66+
.MEDIUM_AND_ABOVE,
67+
},
68+
],
69+
},
70+
},
71+
};
72+
73+
// Construct request
74+
const request = {
75+
parent,
76+
templateId,
77+
template: templateConfig,
78+
};
79+
80+
// Create the template
81+
const [response] = await client.createTemplate(request);
82+
return response;
83+
// [END modelarmor_create_template]
84+
}
85+
86+
module.exports = createTemplate;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 createTemplateWithAdvancedSdp(
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+
// Configuration for the template with advanced SDP settings
79+
const templateConfig = {
80+
filterConfig: {
81+
raiSettings: {
82+
raiFilters: [
83+
{
84+
filterType: RaiFilterType.DANGEROUS,
85+
confidenceLevel: DetectionConfidenceLevel.HIGH,
86+
},
87+
{
88+
filterType: RaiFilterType.HARASSMENT,
89+
confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
90+
},
91+
{
92+
filterType: RaiFilterType.HATE_SPEECH,
93+
confidenceLevel: DetectionConfidenceLevel.HIGH,
94+
},
95+
{
96+
filterType: RaiFilterType.SEXUALLY_EXPLICIT,
97+
confidenceLevel: DetectionConfidenceLevel.HIGH,
98+
},
99+
],
100+
},
101+
sdpSettings: {
102+
advancedConfig: {
103+
inspectTemplate: inspectTemplate,
104+
deidentifyTemplate: deidentifyTemplate,
105+
},
106+
},
107+
},
108+
};
109+
110+
// Construct request
111+
const request = {
112+
parent,
113+
templateId,
114+
template: templateConfig,
115+
};
116+
117+
// Create the template
118+
const [response] = await client.createTemplate(request);
119+
return response;
120+
// [END modelarmor_create_template_with_advanced_sdp]
121+
}
122+
123+
module.exports = createTemplateWithAdvancedSdp;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 createTemplateWithBasicSdp(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+
// Configuration for the template with basic SDP settings
52+
const templateConfig = {
53+
filterConfig: {
54+
raiSettings: {
55+
raiFilters: [
56+
{
57+
filterType: RaiFilterType.DANGEROUS,
58+
confidenceLevel: DetectionConfidenceLevel.HIGH,
59+
},
60+
{
61+
filterType: RaiFilterType.HARASSMENT,
62+
confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
63+
},
64+
{
65+
filterType: RaiFilterType.HATE_SPEECH,
66+
confidenceLevel: DetectionConfidenceLevel.HIGH,
67+
},
68+
{
69+
filterType: RaiFilterType.SEXUALLY_EXPLICIT,
70+
confidenceLevel: DetectionConfidenceLevel.HIGH,
71+
},
72+
],
73+
},
74+
sdpSettings: {
75+
basicConfig: {
76+
filterEnforcement: SdpBasicConfigEnforcement.ENABLED,
77+
},
78+
},
79+
},
80+
};
81+
82+
// Construct request
83+
const request = {
84+
parent,
85+
templateId,
86+
template: templateConfig,
87+
};
88+
89+
const [response] = await client.createTemplate(request);
90+
return response;
91+
// [END modelarmor_create_template_with_basic_sdp]
92+
}
93+
94+
module.exports = createTemplateWithBasicSdp;
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 and custom labels.
19+
*
20+
* @param {string} projectId - Google Cloud project ID where the template will be created.
21+
* @param {string} locationId - Google Cloud location (region) for the template, e.g., 'us-central1'.
22+
* @param {string} templateId - Unique identifier for the new template.
23+
* @param {string} labelKey - The key for the label to add to the template.
24+
* @param {string} labelValue - The value for the label.
25+
*/
26+
async function createTemplateWithLabels(
27+
projectId,
28+
locationId,
29+
templateId,
30+
labelKey,
31+
labelValue
32+
) {
33+
// [START modelarmor_create_template_with_labels]
34+
/**
35+
* TODO(developer): Uncomment these variables before running the sample.
36+
*/
37+
// const projectId = 'your-project-id';
38+
// const locationId = 'us-central1';
39+
// const templateId = 'your-template-id';
40+
// const labelKey = 'environment';
41+
// const labelValue = 'production';
42+
43+
const parent = `projects/${projectId}/locations/${locationId}`;
44+
45+
// Imports the Model Armor library
46+
const modelarmor = require('@google-cloud/modelarmor');
47+
const {ModelArmorClient} = modelarmor.v1;
48+
const {protos} = modelarmor;
49+
50+
// Instantiates a client
51+
const client = new ModelArmorClient({
52+
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
53+
});
54+
55+
// Construct the request with template configuration and labels
56+
const request = {
57+
parent,
58+
templateId,
59+
template: {
60+
filterConfig: {
61+
raiSettings: {
62+
raiFilters: [
63+
{
64+
filterType:
65+
protos.google.cloud.modelarmor.v1.RaiFilterType.HATE_SPEECH,
66+
confidenceLevel:
67+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH,
68+
},
69+
{
70+
filterType:
71+
protos.google.cloud.modelarmor.v1.RaiFilterType
72+
.SEXUALLY_EXPLICIT,
73+
confidenceLevel:
74+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel
75+
.MEDIUM_AND_ABOVE,
76+
},
77+
],
78+
},
79+
},
80+
labels: {
81+
[labelKey]: labelValue,
82+
},
83+
},
84+
};
85+
86+
// Create the template
87+
const [response] = await client.createTemplate(request);
88+
return response;
89+
// [END modelarmor_create_template_with_labels]
90+
}
91+
92+
module.exports = createTemplateWithLabels;

0 commit comments

Comments
 (0)