-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
telpirion
merged 7 commits into
GoogleCloudPlatform:main
from
tirthrajsinh-zala-crest:modelarmor-update-template-samples
May 12, 2025
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50e4e9a
Added code samples to update model armor templates
rudrakhsha-crest 16c2f73
address-review-comments
harshnasitcrest e03eedf
sync-requireenvvar-function
harshnasitcrest 3377636
remove-require-env-location-condition
harshnasitcrest 0706e76
address-review-comment
harshnasitcrest 03a6606
Merge branch 'main' into modelarmor-update-template-samples
harshnasitcrest c34aea9
fix-lint-post-merge-conflicts
harshnasitcrest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 = | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)); | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
modelarmor/src/main/java/modelarmor/UpdateTemplateLabels.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
85
modelarmor/src/main/java/modelarmor/UpdateTemplateMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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(); | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Template updatedTemplate = client.updateTemplate(request); | ||
System.out.println( | ||
"Updated template metadata: " + JsonFormat.printer().print(updatedTemplate)); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
modelarmor/src/main/java/modelarmor/UpdateTemplateWithMaskConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.