Skip to content

Commit 7890c3a

Browse files
Updated code snippets and tests as per comments given in other PRs
1 parent c4e2167 commit 7890c3a

14 files changed

Lines changed: 440 additions & 440 deletions

model-armor/snippets/createTemplate.js

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @param {string} locationId - Google Cloud location (region) for the template, e.g., 'us-central1'.
2424
* @param {string} templateId - Unique identifier for the new template.
2525
*/
26-
async function main(projectId, locationId, templateId) {
26+
async function createTemplate(projectId, locationId, templateId) {
2727
// [START modelarmor_create_template]
2828
/**
2929
* TODO(developer): Uncomment these variables before running the sample.
@@ -44,49 +44,43 @@ async function main(projectId, locationId, templateId) {
4444
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
4545
});
4646

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-
},
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+
],
7269
},
73-
};
74-
75-
// Construct request
76-
const request = {
77-
parent,
78-
templateId,
79-
template: templateConfig,
80-
};
70+
},
71+
};
8172

82-
// Create the template
83-
const [response] = await client.createTemplate(request);
84-
console.log(`Created template: ${response.name}`);
85-
}
73+
// Construct request
74+
const request = {
75+
parent,
76+
templateId,
77+
template: templateConfig,
78+
};
8679

87-
createTemplate();
80+
// Create the template
81+
const [response] = await client.createTemplate(request);
82+
return response;
8883
// [END modelarmor_create_template]
8984
}
9085

91-
const args = process.argv.slice(2);
92-
main(...args).catch(console.error);
86+
module.exports = createTemplate;

model-armor/snippets/createTemplateWithAdvancedSdp.js

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
`organizations/{organization}/locations/{location}/deidentifyTemplates/{deidentify_template}`
4343
`projects/{project}/locations/{location}/deidentifyTemplates/{deidentify_template}`
4444
*/
45-
async function main(
45+
async function createTemplateWithAdvancedSdp(
4646
projectId,
4747
locationId,
4848
templateId,
@@ -75,55 +75,49 @@ async function main(
7575
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
7676
});
7777

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,
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,
10694
},
95+
{
96+
filterType: RaiFilterType.SEXUALLY_EXPLICIT,
97+
confidenceLevel: DetectionConfidenceLevel.HIGH,
98+
},
99+
],
100+
},
101+
sdpSettings: {
102+
advancedConfig: {
103+
inspectTemplate: inspectTemplate,
104+
deidentifyTemplate: deidentifyTemplate,
107105
},
108106
},
109-
};
110-
111-
// Construct request
112-
const request = {
113-
parent,
114-
templateId,
115-
template: templateConfig,
116-
};
107+
},
108+
};
117109

118-
// Create the template
119-
const [response] = await client.createTemplate(request);
120-
console.log(`Created template: ${response.name}`);
121-
}
110+
// Construct request
111+
const request = {
112+
parent,
113+
templateId,
114+
template: templateConfig,
115+
};
122116

123-
createTemplateWithAdvancedSdp();
117+
// Create the template
118+
const [response] = await client.createTemplate(request);
119+
return response;
124120
// [END modelarmor_create_template_with_advanced_sdp]
125121
}
126122

127-
// Check if this script is being run directly
128-
const args = process.argv.slice(2);
129-
main(...args).catch(console.error);
123+
module.exports = createTemplateWithAdvancedSdp;

model-armor/snippets/createTemplateWithBasicSdp.js

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @param {string} locationId - Google Cloud location where the template will be created.
2222
* @param {string} templateId - ID for the template to create.
2323
*/
24-
async function main(projectId, locationId, templateId) {
24+
async function createTemplateWithBasicSdp(projectId, locationId, templateId) {
2525
// [START modelarmor_create_template_with_basic_sdp]
2626
/**
2727
* TODO(developer): Uncomment these variables before running the sample.
@@ -48,52 +48,47 @@ async function main(projectId, locationId, templateId) {
4848
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
4949
});
5050

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,
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,
7867
},
68+
{
69+
filterType: RaiFilterType.SEXUALLY_EXPLICIT,
70+
confidenceLevel: DetectionConfidenceLevel.HIGH,
71+
},
72+
],
73+
},
74+
sdpSettings: {
75+
basicConfig: {
76+
filterEnforcement: SdpBasicConfigEnforcement.ENABLED,
7977
},
8078
},
81-
};
82-
83-
// Construct request
84-
const request = {
85-
parent,
86-
templateId,
87-
template: templateConfig,
88-
};
79+
},
80+
};
8981

90-
const [response] = await client.createTemplate(request);
91-
console.log(`Created template: ${response.name}`);
92-
}
82+
// Construct request
83+
const request = {
84+
parent,
85+
templateId,
86+
template: templateConfig,
87+
};
9388

94-
return createTemplateWithBasicSdp();
89+
const [response] = await client.createTemplate(request);
90+
return response;
9591
// [END modelarmor_create_template_with_basic_sdp]
9692
}
9793

98-
const args = process.argv.slice(2);
99-
main(...args).catch(console.error);
94+
module.exports = createTemplateWithBasicSdp;

model-armor/snippets/createTemplateWithLabels.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
* @param {string} labelKey - The key for the label to add to the template.
2424
* @param {string} labelValue - The value for the label.
2525
*/
26-
async function main(projectId, locationId, templateId, labelKey, labelValue) {
26+
async function createTemplateWithLabels(
27+
projectId,
28+
locationId,
29+
templateId,
30+
labelKey,
31+
labelValue
32+
) {
2733
// [START modelarmor_create_template_with_labels]
2834
/**
2935
* TODO(developer): Uncomment these variables before running the sample.
@@ -46,47 +52,41 @@ async function main(projectId, locationId, templateId, labelKey, labelValue) {
4652
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
4753
});
4854

49-
async function createTemplateWithLabels() {
50-
// Construct the request with template configuration and labels
51-
const request = {
52-
parent,
53-
templateId,
54-
template: {
55-
filterConfig: {
56-
raiSettings: {
57-
raiFilters: [
58-
{
59-
filterType:
60-
protos.google.cloud.modelarmor.v1.RaiFilterType.HATE_SPEECH,
61-
confidenceLevel:
62-
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel
63-
.HIGH,
64-
},
65-
{
66-
filterType:
67-
protos.google.cloud.modelarmor.v1.RaiFilterType
68-
.SEXUALLY_EXPLICIT,
69-
confidenceLevel:
70-
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel
71-
.MEDIUM_AND_ABOVE,
72-
},
73-
],
74-
},
75-
},
76-
labels: {
77-
[labelKey]: labelValue,
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+
],
7878
},
7979
},
80-
};
81-
82-
// Create the template
83-
const [response] = await client.createTemplate(request);
84-
console.log(`Created template: ${response.name}`);
85-
}
80+
labels: {
81+
[labelKey]: labelValue,
82+
},
83+
},
84+
};
8685

87-
createTemplateWithLabels();
86+
// Create the template
87+
const [response] = await client.createTemplate(request);
88+
return response;
8889
// [END modelarmor_create_template_with_labels]
8990
}
9091

91-
const args = process.argv.slice(2);
92-
main(...args).catch(console.error);
92+
module.exports = createTemplateWithLabels;

0 commit comments

Comments
 (0)