Skip to content

feat(modelarmor): Added create template samples for modelarmor #10068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
/iam @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
/iap @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
/kms @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
/modelarmor @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/cloud-modelarmor-team
/parametermanager @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/cloud-secrets-team @GoogleCloudPlatform/cloud-parameters-team
/privateca @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
/recaptcha_enterprise @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra
Expand Down
8 changes: 8 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ assign_issues_by:
- "api: bigquerydatatransfer"
to:
- GoogleCloudPlatform/bigquery-data-connectors
- labels:
- "api: modelarmor"
to:
- GoogleCloudPlatform/cloud-modelarmor-team

assign_prs:
- GoogleCloudPlatform/java-samples-reviewers
Expand Down Expand Up @@ -173,3 +177,7 @@ assign_prs_by:
- "api: appengine"
to:
- GoogleCloudPlatform/serverless-runtimes
- labels:
- "api: modelarmor"
to:
- GoogleCloudPlatform/cloud-modelarmor-team
110 changes: 110 additions & 0 deletions modelarmor/src/main/java/modelarmor/CreateTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package modelarmor;

// [START modelarmor_create_template]

import com.google.cloud.modelarmor.v1.CreateTemplateRequest;
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
import com.google.cloud.modelarmor.v1.FilterConfig;
import com.google.cloud.modelarmor.v1.LocationName;
import com.google.cloud.modelarmor.v1.ModelArmorClient;
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
import com.google.cloud.modelarmor.v1.RaiFilterSettings;
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter;
import com.google.cloud.modelarmor.v1.RaiFilterType;
import com.google.cloud.modelarmor.v1.Template;
import java.io.IOException;
import java.util.List;

public class CreateTemplate {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.

// Specify the Google Project ID.
String projectId = "your-project-id";
// Specify the location ID. For example, us-central1.
String locationId = "your-location-id";
// Specify the template ID.
String templateId = "your-template-id";

createTemplate(projectId, locationId, templateId);
}

public static Template createTemplate(String projectId, String locationId, String templateId)
throws IOException {

// Construct the API endpoint URL.
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
.build();

// Initialize the client that will be used to send requests. This client
// only needs to be created once, and can be reused for multiple requests.
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String parent = LocationName.of(projectId, locationId).toString();

// Build the Model Armor template with your preferred filters.
// For more details on filters, please refer to the following doc:
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters

// Configure Responsible AI filter with multiple categories and their confidence
// levels.
RaiFilterSettings raiFilterSettings = RaiFilterSettings.newBuilder()
.addAllRaiFilters(
List.of(
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.DANGEROUS)
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
.build(),
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.HATE_SPEECH)
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
.build(),
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT)
.setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE)
.build(),
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.HARASSMENT)
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
.build()))
.build();

FilterConfig modelArmorFilter = FilterConfig.newBuilder()
.setRaiSettings(raiFilterSettings)
.build();

Template template = Template.newBuilder()
.setFilterConfig(modelArmorFilter)
.build();

CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
.setParent(parent)
.setTemplateId(templateId)
.setTemplate(template)
.build();

Template createdTemplate = client.createTemplate(request);
System.out.println("Created template: " + createdTemplate.getName());

return createdTemplate;
}
}
}
// [END modelarmor_create_template]
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package modelarmor;

// [START modelarmor_create_template_with_basic_sdp]

import com.google.cloud.modelarmor.v1.CreateTemplateRequest;
import com.google.cloud.modelarmor.v1.FilterConfig;
import com.google.cloud.modelarmor.v1.LocationName;
import com.google.cloud.modelarmor.v1.ModelArmorClient;
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
import com.google.cloud.modelarmor.v1.SdpBasicConfig;
import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement;
import com.google.cloud.modelarmor.v1.SdpFilterSettings;
import com.google.cloud.modelarmor.v1.Template;
import java.io.IOException;

public class CreateTemplateWithBasicSdp {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.

// Specify the Google Project ID.
String projectId = "your-project-id";
// Specify the location ID. For example, us-central1.
String locationId = "your-location-id";
// Specify the template ID.
String templateId = "your-template-id";

createTemplateWithBasicSdp(projectId, locationId, templateId);
}

public static Template createTemplateWithBasicSdp(
String projectId, String locationId, String templateId) throws IOException {

// Construct the API endpoint URL.
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
.build();

// Initialize the client that will be used to send requests. This client
// only needs to be created once, and can be reused for multiple requests.
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String parent = LocationName.of(projectId, locationId).toString();

// Build the Model Armor template with your preferred filters.
// For more details on filters, please refer to the following doc:
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters

// Configure Basic SDP Filter.
SdpBasicConfig basicSdpConfig = SdpBasicConfig.newBuilder()
.setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED)
.build();

SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder()
.setBasicConfig(basicSdpConfig)
.build();

FilterConfig modelArmorFilter = FilterConfig.newBuilder()
.setSdpSettings(sdpSettings)
.build();

Template template = Template.newBuilder()
.setFilterConfig(modelArmorFilter)
.build();

CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
.setParent(parent)
.setTemplateId(templateId)
.setTemplate(template)
.build();

Template createdTemplate = client.createTemplate(request);
System.out.println("Created template with basic SDP filter: " + createdTemplate.getName());

return createdTemplate;
}
}
}
// [END modelarmor_create_template_with_basic_sdp]
119 changes: 119 additions & 0 deletions modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package modelarmor;

// [START modelarmor_create_template_with_labels]

import com.google.cloud.modelarmor.v1.CreateTemplateRequest;
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
import com.google.cloud.modelarmor.v1.FilterConfig;
import com.google.cloud.modelarmor.v1.LocationName;
import com.google.cloud.modelarmor.v1.ModelArmorClient;
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
import com.google.cloud.modelarmor.v1.RaiFilterSettings;
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter;
import com.google.cloud.modelarmor.v1.RaiFilterType;
import com.google.cloud.modelarmor.v1.Template;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreateTemplateWithLabels {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.

// Specify the Google Project ID.
String projectId = "your-project-id";
// Specify the location ID. For example, us-central1.
String locationId = "your-location-id";
// Specify the template ID.
String templateId = "your-template-id";

createTemplateWithLabels(projectId, locationId, templateId);
}

public static Template createTemplateWithLabels(
String projectId, String locationId, String templateId) throws IOException {

// Construct the API endpoint URL.
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
.build();

// Initialize the client that will be used to send requests. This client
// only needs to be created once, and can be reused for multiple requests.
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String parent = LocationName.of(projectId, locationId).toString();

// Build the Model Armor template with your preferred filters.
// For more details on filters, please refer to the following doc:
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters

// Configure Responsible AI filter with multiple categories and their confidence
// levels.
RaiFilterSettings raiFilterSettings =
RaiFilterSettings.newBuilder()
.addAllRaiFilters(
List.of(
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.DANGEROUS)
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
.build(),
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.HATE_SPEECH)
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
.build(),
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT)
.setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE)
.build(),
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.HARASSMENT)
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
.build()))
.build();

FilterConfig modelArmorFilter = FilterConfig.newBuilder()
.setRaiSettings(raiFilterSettings)
.build();

// Create Labels.
Map<String, String> labels = new HashMap<>();
labels.put("key1", "value1");
labels.put("key2", "value2");

Template template = Template.newBuilder()
.setFilterConfig(modelArmorFilter)
.putAllLabels(labels)
.build();

CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
.setParent(parent)
.setTemplateId(templateId)
.setTemplate(template)
.build();

Template createdTemplate = client.createTemplate(request);
System.out.println("Created template with labels: " + createdTemplate.getName());

return createdTemplate;
}
}
}
// [END modelarmor_create_template_with_labels]
Loading