8000 feat(modelarmor): Added create template samples for modelarmor (#10068) · rebeccaellis/java-docs-samples@e21d99e · GitHub
[go: up one dir, main page]

Skip to content

Commit e21d99e

Browse files
feat(modelarmor): Added create template samples for modelarmor (GoogleCloudPlatform#10068)
* Added create template samples for java * Added delete template code snippet * address-review-comments * fix-lint * fix-lint * remove-unused-annotation * sync code comments and update requireEnv function across snippets * added-codeowners-and-blunderbuss * remove-require-env-location-condition * address-review-comment * fixed-lint-post-merge-conflicts * fix-lints-post-merge-conflicts * fix-tests-post-resolving-merge-conflicts --------- Co-authored-by: Harsh Nasit <harsh.nasit@crestdata.ai> Co-authored-by: harshnasitcrest <131268456+harshnasitcrest@users.noreply.github.com>
1 parent d239656 commit e21d99e

File tree

8 files changed

+588
-22
lines changed

8 files changed

+588
-22
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
/iam @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
3838
/iap @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
3939
/kms @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
40+
/modelarmor @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/cloud-modelarmor-team
4041
/parametermanager @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/cloud-secrets-team @GoogleCloudPlatform/cloud-parameters-team
4142
/privateca @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
4243
/recaptcha_enterprise @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra

.github/blunderbuss.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ assign_issues_by:
9696
- "api: bigquerydatatransfer"
9797
to:
9898
- GoogleCloudPlatform/bigquery-data-connectors
99+
- labels:
100+
- "api: modelarmor"
101+
to:
102+
- GoogleCloudPlatform/cloud-modelarmor-team
99103

100104
assign_prs:
101105
- GoogleCloudPlatform/java-samples-reviewers
@@ -173,3 +177,7 @@ assign_prs_by:
173177
- "api: appengine"
174178
to:
175179
- GoogleCloudPlatform/serverless-runtimes
180+
- labels:
181+
- "api: modelarmor"
182+
to:
183+
- GoogleCloudPlatform/cloud-modelarmor-team
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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]
20+
21+
import com.google.cloud.modelarmor.v1.CreateTemplateRequest;
22+
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
23+
import com.google.cloud.modelarmor.v1.FilterConfig;
24+
import com.google.cloud.modelarmor.v1.LocationName;
25+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
26+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
27+
import com.google.cloud.modelarmor.v1.RaiFilterSettings;
28+
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter;
29+
import com.google.cloud.modelarmor.v1.RaiFilterType;
30+
import com.google.cloud.modelarmor.v1.Template;
31+
import java.io.IOException;
32+
import java.util.List;
33+
34+
public class CreateTemplate {
35+
36+
public static void main(String[] args) throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
39+
// Specify the Google Project ID.
40+
String projectId = "your-project-id";
41+
// Specify the location ID. For example, us-central1.
42+
String locationId = "your-location-id";
43+
// Specify the template ID.
44+
String templateId = "your-template-id";
45+
46+
createTemplate(projectId, locationId, templateId);
47+
}
48+
49+
public static Template createTemplate(String projectId, String locationId, String templateId)
50+
throws IOException {
51+
52+
// Construct the API endpoint URL.
53+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
54+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
55+
.build();
56+
57+
// Initialize the client that will be used to send requests. This client
58+
// only needs to be created once, and can be reused for multiple requests.
59+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
60+
String parent = LocationName.of(projectId, locationId).toString();
61+
62+
// Build the Model Armor template with your preferred filters.
63+
// For more details on filters, please refer to the following doc:
64+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
65+
66+
// Configure Responsible AI filter with multiple categories and their confidence
67+
// levels.
68+
RaiFilterSettings raiFilterSettings = RaiFilterSettings.newBuilder()
69+
.addAllRaiFilters(
70+
List.of(
71+
RaiFilter.newBuilder()
72+
.setFilterType(RaiFilterType.DANGEROUS)
73+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
74+
.build(),
75+
RaiFilter.newBuilder()
76+
.setFilterType(RaiFilterType.HATE_SPEECH)
77+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
78+
.build(),
79+
RaiFilter.newBuilder()
80+
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT)
81+
.setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE)
82+
.build(),
83+
RaiFilter.newBuilder()
84+
.setFilterType(RaiFilterType.HARASSMENT)
85+
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
86+
.build()))
87+
.build();
88+
89+
FilterConfig modelArmorFilter = FilterConfig.newBuilder()
90+
.setRaiSettings(raiFilterSettings)
91+
.build();
92+
93+
Template template = Template.newBuilder()
94+
.setFilterConfig(modelArmorFilter)
95+
.build();
96+
97+
CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
98+
.setParent(parent)
99+
.setTemplateId(templateId)
100+
.setTemplate(template)
101+
.build();
102+
103+
Template createdTemplate = client.createTemplate(request);
104+
System.out.println("Created template: " + createdTemplate.getName());
105+
106+
return createdTemplate;
107+
}
108+
}
109+
}
110+
// [END modelarmor_create_template]
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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_basic_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.SdpBasicConfig;
27+
import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement;
28+
import com.google.cloud.modelarmor.v1.SdpFilterSettings;
29+
import com.google.cloud.modelarmor.v1.Template;
30+
import java.io.IOException;
31+
32+
public class CreateTemplateWithBasicSdp {
33+
34+
public static void main(String[] args) throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
37+
// Specify the Google Project ID.
38+
String projectId = "your-project-id";
39+
// Specify the location ID. For example, us-central1.
40+
String locationId = "your-location-id";
41+
// Specify the template ID.
42+
String templateId = "your-template-id";
43+
44+
createTemplateWithBasicSdp(projectId, locationId, templateId);
45+
}
46+
47+
public static Template createTemplateWithBasicSdp(
48+
String projectId, String locationId, String templateId) throws IOException {
49+
50+
// Construct the API endpoint URL.
51+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
52+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
53+
.build();
54+
55+
// Initialize the client that will be used to send requests. This client
56+
// only needs to be created once, and can be reused for multiple requests.
57+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
58+
String parent = LocationName.of(projectId, locationId).toString();
59+
60+
// Build the Model Armor template with your preferred filters.
61+
// For more details on filters, please refer to the following doc:
62+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
63+
64+
// Configure Basic SDP Filter.
65+
SdpBasicConfig basicSdpConfig = SdpBasicConfig.newBuilder()
66+
.setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED)
67+
.build();
68+
69+
SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder()
70+
.setBasicConfig(basicSdpConfig)
71+
.build();
72+
73+
FilterConfig modelArmorFilter = FilterConfig.newBuilder()
74+
.setSdpSettings(sdpSettings)
75+
.build();
76+
77+
Template template = Template.newBuilder()
78+
.setFilterConfig(modelArmorFilter)
79+
.build();
80+
81+
CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
82+
.setParent(parent)
83+
.setTemplateId(templateId)
84+
.setTemplate(template)
85+
.build();
86+
87+
Template createdTemplate = client.createTemplate(request);
88+
System.out.println("Created template with basic SDP filter: " + createdTemplate.getName());
89+
90+
return createdTemplate;
91+
}
92+
}
93+
}
94+
// [END modelarmor_create_template_with_basic_sdp]
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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_labels]
20+
21+
import com.google.cloud.modelarmor.v1.CreateTemplateRequest;
22+
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
23+
import com.google.cloud.modelarmor.v1.FilterConfig;
24+
import com.google.cloud.modelarmor.v1.LocationName;
25+
import com.google.cloud.modelarmor.v1.ModelArmorClient;
26+
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
27+
import com.google.cloud.modelarmor.v1.RaiFilterSettings;
28+
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter;
29+
import com.google.cloud.modelarmor.v1.RaiFilterType;
30+
import com.google.cloud.modelarmor.v1.Template;
31+
import java.io.IOException;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
public class CreateTemplateWithLabels {
37+
38+
public static void main(String[] args) throws IOException {
39+
// TODO(developer): Replace these variables before running the sample.
40+
41+
// Specify the Google Project ID.
42+
String projectId = "your-project-id";
43+
// Specify the location ID. For example, us-central1.
44+
String locationId = "your-location-id";
45+
// Specify the template ID.
46+
String templateId = "your-template-id";
47+
48+
createTemplateWithLabels(projectId, locationId, templateId);
49+
}
50+
51+
public static Template createTemplateWithLabels(
52+
String projectId, String locationId, String templateId) throws IOException {
53+
54+
// Construct the API endpoint URL.
55+
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
56+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
57+
.build();
58+
59+
// Initialize the client that will be used to send requests. This client
60+
// only needs to be created once, and can be reused for multiple requests.
61+
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
62+
String parent = LocationName.of(projectId, locationId).toString();
63+
64+
// Build the Model Armor template with your preferred filters.
65+
// For more details on filters, please refer to the following doc:
66+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
67+
68+
// Configure Responsible AI filter with multiple categories and their confidence
69+
// levels.
70+
RaiFilterSettings raiFilterSettings =
71+
RaiFilterSettings.newBuilder()
72+
.addAllRaiFilters(
73+
List.of(
74+
RaiFilter.newBuilder()
75+
.setFilterType(RaiFilterType.DANGEROUS)
76+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
77+
.build(),
78+
RaiFilter.newBuilder()
79+
.setFilterType(RaiFilterType.HATE_SPEECH)
80+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
81+
.build(),
82+
RaiFilter.newBuilder()
83+
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT)
84+
.setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE)
85+
.build(),
86+
RaiFilter.newBuilder()
87+
.setFilterType(RaiFilterType.HARASSMENT)
88+
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
89+
.build()))
90+
.build();
91+
92+
FilterConfig modelArmorFilter = FilterConfig.newBuilder()
93+
.setRaiSettings(raiFilterSettings)
94+
.build();
95+
96+
// Create Labels.
97+
Map<String, String> labels = new HashMap<>();
98+
labels.put("key1", "value1");
99+
labels.put("key2", "value2");
100+
101+
Template template = Template.newBuilder()
102+
.setFilterConfig(modelArmorFilter)
103+
.putAllLabels(labels)
104+
.build();
105+
106+
CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
107+
.setParent(parent)
108+
.setTemplateId(templateId)
109+
.setTemplate(template)
110+
.build();
111+
112+
Template createdTemplate = client.createTemplate(request);
113+
System.out.println("Created template with labels: " + createdTemplate.getName());
114+
115+
return createdTemplate;
116+
}
117+
}
118+
}
119+
// [END modelarmor_create_template_with_labels]

0 commit comments

Comments
 (0)
0