Skip to content

Commit 757b290

Browse files
add-create-template-with-advanced-sdp-snippet (#10077)
1 parent 6b3375e commit 757b290

File tree

3 files changed

+417
-0
lines changed

3 files changed

+417
-0
lines changed

modelarmor/pom.xml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<groupId>com.example.modelarmor</groupId>
22+
<artifactId>modelarmor-samples</artifactId>
23+
<packaging>jar</packaging>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<groupId>com.google.cloud.samples</groupId>
31+
<artifactId>shared-configuration</artifactId>
32+
<version>1.2.0</version>
33+
</parent>
34+
35+
<properties>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
<maven.compiler.target>11</maven.compiler.target>
38+
<maven.compiler.source>11</maven.compiler.source>
39+
</properties>
40+
41+
<dependencyManagement>
42+
<dependencies>
43+
<dependency>
44+
<groupId>com.google.cloud</groupId>
45+
<artifactId>libraries-bom</artifactId>
46+
<version>26.59.0</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
</dependencies>
51+
</dependencyManagement>
52+
53+
<dependencies>
54+
<dependency>
55+
<groupId>com.google.cloud</groupId>
56+
<artifactId>google-cloud-modelarmor</artifactId>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>com.google.cloud</groupId>
61+
<artifactId>google-cloud-dlp</artifactId>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>com.google.protobuf</groupId>
66+
<artifactId>protobuf-java-util</artifactId>
67+
</dependency>
68+
69+
<!-- test dependencies -->
70+
<dependency>
71+
<groupId>junit</groupId>
72+
<artifactId>junit</artifactId>
73+
<version>4.13.2</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.google.truth</groupId>
78+
<artifactId>truth</artifactId>
79+
<version>1.4.0</version>
80+
<scope>test</scope>
81+
</dependency>
82+
</dependencies>
83+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)