|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package modelarmor; |
| 18 | + |
| 19 | +// [START modelarmor_create_template_with_advanced_sdp] |
| 20 | + |
| 21 | +import com.google.cloud.modelarmor.v1.CreateTemplateRequest; |
| 22 | +import com.google.cloud.modelarmor.v1.FilterConfig; |
| 23 | +import com.google.cloud.modelarmor.v1.LocationName; |
| 24 | +import com.google.cloud.modelarmor.v1.ModelArmorClient; |
| 25 | +import com.google.cloud.modelarmor.v1.ModelArmorSettings; |
| 26 | +import com.google.cloud.modelarmor.v1.SdpAdvancedConfig; |
| 27 | +import com.google.cloud.modelarmor.v1.SdpFilterSettings; |
| 28 | +import com.google.cloud.modelarmor.v1.Template; |
| 29 | +import com.google.privacy.dlp.v2.DeidentifyTemplateName; |
| 30 | +import com.google.privacy.dlp.v2.InspectTemplateName; |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +public class CreateTemplateWithAdvancedSdp { |
| 34 | + |
| 35 | + public static void main(String[] args) throws IOException { |
| 36 | + // TODO(developer): Replace these variables before running the sample. |
| 37 | + |
| 38 | + // Specify the Google Project ID. |
| 39 | + String projectId = "your-project-id"; |
| 40 | + // Specify the location ID. For example, us-central1. |
| 41 | + String locationId = "your-location-id"; |
| 42 | + // Specify the template ID. |
| 43 | + String templateId = "your-template-id"; |
| 44 | + // Specify the Inspect template ID. |
| 45 | + String inspectTemplateId = "your-inspect-template-id"; |
| 46 | + // Specify the Deidentify template ID. |
| 47 | + String deidentifyTemplateId = "your-deidentify-template-id"; |
| 48 | + |
| 49 | + createTemplateWithAdvancedSdp(projectId, locationId, templateId, inspectTemplateId, |
| 50 | + deidentifyTemplateId); |
| 51 | + } |
| 52 | + |
| 53 | + public static Template createTemplateWithAdvancedSdp(String projectId, String locationId, |
| 54 | + String templateId, String inspectTemplateId, String deidentifyTemplateId) throws IOException { |
| 55 | + |
| 56 | + // Construct the API endpoint URL. |
| 57 | + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); |
| 58 | + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) |
| 59 | + .build(); |
| 60 | + |
| 61 | + // Initialize the client that will be used to send requests. This client |
| 62 | + // only needs to be created once, and can be reused for multiple requests. |
| 63 | + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { |
| 64 | + String parent = LocationName.of(projectId, locationId).toString(); |
| 65 | + |
| 66 | + String inspectTemplateName = InspectTemplateName |
| 67 | + .ofProjectLocationInspectTemplateName(projectId, locationId, inspectTemplateId) |
| 68 | + .toString(); |
| 69 | + |
| 70 | + String deidentifyTemplateName = DeidentifyTemplateName |
| 71 | + .ofProjectLocationDeidentifyTemplateName(projectId, locationId, deidentifyTemplateId) |
| 72 | + .toString(); |
| 73 | + |
| 74 | + // Build the Model Armor template with Advanced SDP Filter. |
| 75 | + |
| 76 | + // Note: If you specify only Inspect template, Model Armor reports the filter matches if |
| 77 | + // sensitive data is detected. If you specify Inspect template and De-identify template, Model |
| 78 | + // Armor returns the de-identified sensitive data and sanitized version of prompts or |
| 79 | + // responses in the deidentifyResult.data.text field of the finding. |
| 80 | + SdpAdvancedConfig advancedSdpConfig = |
| 81 | + SdpAdvancedConfig.newBuilder() |
| 82 | + .setInspectTemplate(inspectTemplateName) |
| 83 | + .setDeidentifyTemplate(deidentifyTemplateName) |
| 84 | + .build(); |
| 85 | + |
| 86 | + SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder() |
| 87 | + .setAdvancedConfig(advancedSdpConfig).build(); |
| 88 | + |
| 89 | + FilterConfig modelArmorFilter = FilterConfig.newBuilder().setSdpSettings(sdpSettings).build(); |
| 90 | + |
| 91 | + Template template = Template.newBuilder().setFilterConfig(modelArmorFilter).build(); |
| 92 | + |
| 93 | + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() |
| 94 | + .setParent(parent) |
| 95 | + .setTemplateId(templateId) |
| 96 | + .setTemplate(template) |
| 97 | + .build(); |
| 98 | + |
| 99 | + Template createdTemplate = client.createTemplate(request); |
| 100 | + System.out.println("Created template with Advanced SDP filter: " + createdTemplate.getName()); |
| 101 | + |
| 102 | + return createdTemplate; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +// [END modelarmor_create_template_with_advanced_sdp] |
0 commit comments