|
| 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; |
0 commit comments