Skip to content

feat(modelarmor): Added code samples to update model armor templates #10069

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 1 commit
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
81 changes: 81 additions & 0 deletions modelarmor/src/main/java/modelarmor/UpdateTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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;

import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
import com.google.cloud.modelarmor.v1.FilterConfig;
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 com.google.cloud.modelarmor.v1.TemplateName;
import com.google.cloud.modelarmor.v1.UpdateTemplateRequest;
import com.google.protobuf.FieldMask;
import com.google.protobuf.util.JsonFormat;
import java.util.List;

public class UpdateTemplate {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String locationId = "your-location-id";
String templateId = "your-template-id";

updateTemplate(projectId, locationId, templateId);
}

public static void updateTemplate(String projectId, String locationId, String templateId)
throws Exception {
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
ModelArmorSettings modelArmorSettings =
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();

try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();

Template template =
Template.newBuilder()
.setName(name)
.setFilterConfig(
FilterConfig.newBuilder()
.setRaiSettings(
RaiFilterSettings.newBuilder()
.addAllRaiFilters(
List.of(
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.HARASSMENT)
.setConfidenceLevel(
DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
.build()))
.build())
.build())
.build();

UpdateTemplateRequest request =
UpdateTemplateRequest.newBuilder()
.setTemplate(template)
.setUpdateMask(FieldMask.newBuilder().addPaths("filter_config").build())
.build();

Template updatedTemplate = client.updateTemplate(request);
System.out.println("Updated template: " + JsonFormat.printer().print(updatedTemplate));
}
}
}
65 changes: 65 additions & 0 deletions modelarmor/src/main/java/modelarmor/UpdateTemplateLabels.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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;

import com.google.cloud.modelarmor.v1.ModelArmorClient;
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
import com.google.cloud.modelarmor.v1.Template;
import com.google.cloud.modelarmor.v1.TemplateName;
import com.google.cloud.modelarmor.v1.UpdateTemplateRequest;
import com.google.protobuf.FieldMask;
import com.google.protobuf.util.JsonFormat;
import java.util.HashMap;
import java.util.Map;

public class UpdateTemplateLabels {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String locationId = "your-location-id";
String templateId = "your-template-id";

updateTemplateLabels(projectId, locationId, templateId);
}

public static void updateTemplateLabels(String projectId, String locationId, String templateId)
throws Exception {
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
ModelArmorSettings modelArmorSettings =
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();

try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();

Map<String, String> labels = new HashMap<>();
labels.put("key3", "value3");
labels.put("key4", "value4");

Template template = Template.newBuilder().setName(name).putAllLabels(labels).build();

UpdateTemplateRequest request =
UpdateTemplateRequest.newBuilder()
.setTemplate(template)
.setUpdateMask(FieldMask.newBuilder().addPaths("labels").build())
.build();

Template updatedTemplate = client.updateTemplate(request);
System.out.println("Updated template labels: " + JsonFormat.printer().print(updatedTemplate));
}
}
}
85 changes: 85 additions & 0 deletions modelarmor/src/main/java/modelarmor/UpdateTemplateMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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;

import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
import com.google.cloud.modelarmor.v1.FilterConfig;
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 com.google.cloud.modelarmor.v1.TemplateName;
import com.google.cloud.modelarmor.v1.UpdateTemplateRequest;
import com.google.protobuf.util.JsonFormat;
import java.util.List;

public class UpdateTemplateMetadata {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String locationId = "your-location-id";
String templateId = "your-template-id";

updateTemplateMetadata(projectId, locationId, templateId);
}

public static void updateTemplateMetadata(String projectId, String locationId, String templateId)
throws Exception {
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
ModelArmorSettings modelArmorSettings =
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();

try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();

Template template =
Template.newBuilder()
.setName(name)
// Ensure the rest of the template is correctly populated as needed.
.setTemplateMetadata(
Template.TemplateMetadata.newBuilder()
.setIgnorePartialInvocationFailures(true)
.setLogSanitizeOperations(true))
.setFilterConfig(
FilterConfig.newBuilder()
.setRaiSettings(
RaiFilterSettings.newBuilder()
.addAllRaiFilters(
List.of(
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.DANGEROUS)
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
.build()))
.build())
.build())
.build();

UpdateTemplateRequest request =
UpdateTemplateRequest.newBuilder()
.setTemplate(template)
// Removed the setUpdateMask line to attempt a full update.
.build();

Template updatedTemplate = client.updateTemplate(request);
System.out.println(
"Updated template metadata: " + JsonFormat.printer().print(updatedTemplate));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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;

import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
import com.google.cloud.modelarmor.v1.FilterConfig;
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 com.google.cloud.modelarmor.v1.TemplateName;
import com.google.cloud.modelarmor.v1.UpdateTemplateRequest;
import com.google.protobuf.FieldMask;
import com.google.protobuf.util.JsonFormat;
import java.util.List;

public class UpdateTemplateWithMaskConfiguration {

public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String locationId = "your-location-id";
String templateId = "your-template-id";

updateTemplateWithMaskConfiguration(projectId, locationId, templateId);
}

public static void updateTemplateWithMaskConfiguration(
String projectId, String locationId, String templateId) throws Exception {
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
ModelArmorSettings modelArmorSettings =
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();

try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
String name = TemplateName.of(projectId, locationId, templateId).toString();

Template template =
Template.newBuilder()
.setName(name)
.setFilterConfig(
FilterConfig.newBuilder()
.setRaiSettings(
RaiFilterSettings.newBuilder()
.addAllRaiFilters(
List.of(
RaiFilter.newBuilder()
.setFilterType(RaiFilterType.HARASSMENT)
.setConfidenceLevel(
DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
.build()))
.build())
.build())
.build();

UpdateTemplateRequest request =
UpdateTemplateRequest.newBuilder()
.setTemplate(template)
.setUpdateMask(
FieldMask.newBuilder().addPaths("filter_config.rai_settings.rai_filters").build())
.build();

Template updatedTemplate = client.updateTemplate(request);
System.out.println(
"Updated template with mask configuration: "
+ JsonFormat.printer().print(updatedTemplate));
}
}
}
Loading