-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
telpirion
merged 15 commits into
GoogleCloudPlatform:main
from
tirthrajsinh-zala-crest:modelarmor-create-template-samples
May 7, 2025
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
974db03
Added create template samples for java
rudrakhsha-crest 2bdf84a
Added delete template code snippet
rudrakhsha-crest a96a594
address-review-comments
harshnasitcrest 856db90
fix-lint
harshnasitcrest da0224c
fix-lint
harshnasitcrest d46f26e
remove-unused-annotation
harshnasitcrest d5817bb
sync code comments and update requireEnv function across snippets
harshnasitcrest 412a057
Merge branch 'modelarmor-create-template-samples' of https://github.c…
harshnasitcrest 11634c2
added-codeowners-and-blunderbuss
harshnasitcrest 8ade246
remove-require-env-location-condition
harshnasitcrest dbc1b5e
address-review-comment
harshnasitcrest 6f3ebd3
Merge branch 'main' into modelarmor-create-template-samples
harshnasitcrest 6ff142e
fixed-lint-post-merge-conflicts
harshnasitcrest 5a0c3bd
fix-lints-post-merge-conflicts
harshnasitcrest cbdfc1c
fix-tests-post-resolving-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,29 @@ | ||
# Google Model Armor | ||
|
||
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=modelarmor/README.md"> | ||
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a> | ||
|
||
Google Model Armor is a fully managed Google Cloud service that enhances the security and safety of AI applications by screening LLM prompts and responses for various security and safety risks. [More Details](https://cloud.google.com/security-command-center/docs/model-armor-overview) | ||
|
||
These sample Java code snippets demonstrate how to access the Model Armor API using the Google Java API Client Libraries. | ||
|
||
## Prerequisites | ||
|
||
### Enable the API | ||
|
||
The following page details the permissions required for Model Armor and provides instructions for enabling and disabling Model Armor: | ||
[Enable Model Armor API](https://cloud.google.com/security-command-center/docs/get-started-model-armor#enable-model-armor) | ||
|
||
### Grant Permissions | ||
You must ensure that the [user account or service account](https://cloud.google.com/iam/docs/service-accounts#differences_between_a_service_account_and_a_user_account) you used to authorize your gcloud session has the proper permissions to edit Secret Manager resources for your project. In the Cloud Console under IAM, add the following roles to the project whose service account you're using to test: | ||
* Model Armor Admin (roles/modelarmor.admin) | ||
* Floor Settings Admin (modelarmor.floorSettingsAdmin) | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
More information can be found in the [Model Armor Docs](https://cloud.google.com/security-command-center/docs/get-started-model-armor#required_permissions) | ||
|
||
### Set Environment Variables | ||
|
||
You must set your project ID to run the tests: | ||
```shell | ||
export GOOGLE_CLOUD_PROJECT=<your-project-id-here> | ||
``` |
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,112 @@ | ||
<!-- | ||
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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example.modelarmor</groupId> | ||
<artifactId>modelarmor-samples</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.2.0</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>libraries-bom</artifactId> | ||
<version>26.59.0</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-modelarmor</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-dlp</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.protobuf</groupId> | ||
<artifactId>protobuf-java-util</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.30</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>1.4.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.12.1</version> | ||
<configuration> | ||
<source>11</source> <!-- depending on your project --> | ||
<target>11</target> <!-- depending on your project --> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.30</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
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,80 @@ | ||
/* | ||
* 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.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 com.google.protobuf.util.JsonFormat; | ||
import java.util.List; | ||
|
||
public class CreateTemplate { | ||
|
||
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
|
||
|
||
createTemplate(projectId, locationId, templateId); | ||
} | ||
|
||
public static Template createTemplate(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 parent = LocationName.of(projectId, locationId).toString(); | ||
|
||
Template template = | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Template.newBuilder() | ||
.setFilterConfig( | ||
FilterConfig.newBuilder() | ||
.setRaiSettings( | ||
RaiFilterSettings.newBuilder() | ||
.addAllRaiFilters( | ||
List.of( | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.DANGEROUS) | ||
.setConfidenceLevel(DetectionConfidenceLevel.HIGH) | ||
.build())) | ||
.build()) | ||
.build()) | ||
.build(); | ||
|
||
CreateTemplateRequest request = | ||
CreateTemplateRequest.newBuilder() | ||
.setParent(parent) | ||
.setTemplateId(templateId) | ||
.setTemplate(template) | ||
.build(); | ||
|
||
Template createdTemplate = client.createTemplate(request); | ||
System.out.println("Created template: " + JsonFormat.printer().print(createdTemplate)); | ||
rudrakhsha-crest marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return createdTemplate; | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.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,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.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 com.google.protobuf.util.JsonFormat; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class CreateTemplateWithBasicSdp { | ||
|
||
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"; | ||
|
||
createTemplateWithBasicSdp(projectId, locationId, templateId); | ||
} | ||
|
||
public static void createTemplateWithBasicSdp( | ||
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 parent = LocationName.of(projectId, locationId).toString(); | ||
|
||
Template template = | ||
Template.newBuilder() | ||
.setFilterConfig( | ||
FilterConfig.newBuilder() | ||
.setRaiSettings( | ||
RaiFilterSettings.newBuilder() | ||
.addAllRaiFilters( | ||
List.of( | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.DANGEROUS) | ||
.setConfidenceLevel(DetectionConfidenceLevel.HIGH) | ||
.build())) | ||
.build()) | ||
.build()) | ||
.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: " + JsonFormat.printer().print(createdTemplate)); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.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,87 @@ | ||
/* | ||
* 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.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 com.google.protobuf.util.JsonFormat; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class CreateTemplateWithLabels { | ||
|
||
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"; | ||
|
||
createTemplateWithLabels(projectId, locationId, templateId); | ||
} | ||
|
||
public static void createTemplateWithLabels( | ||
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 parent = LocationName.of(projectId, locationId).toString(); | ||
|
||
Map<String, String> labels = new HashMap<>(); | ||
labels.put("key1", "value1"); | ||
labels.put("key2", "value2"); | ||
|
||
Template template = | ||
Template.newBuilder() | ||
.putAllLabels(labels) | ||
.setFilterConfig( | ||
FilterConfig.newBuilder() | ||
.setRaiSettings( | ||
RaiFilterSettings.newBuilder() | ||
.addAllRaiFilters( | ||
List.of( | ||
RaiFilter.newBuilder() | ||
.setFilterType(RaiFilterType.DANGEROUS) | ||
.setConfidenceLevel(DetectionConfidenceLevel.HIGH) | ||
.build())) | ||
.build()) | ||
.build()) | ||
.build(); | ||
|
||
CreateTemplateRequest request = | ||
CreateTemplateRequest.newBuilder() | ||
.setParent(parent) | ||
.setTemplateId(templateId) | ||
.setTemplate(template) | ||
.build(); | ||
|
||
Template createdTemplate = client.createTemplate(request); | ||
System.out.println( | ||
"Created template with labels: " + JsonFormat.printer().print(createdTemplate)); | ||
} | ||
} | ||
} |
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.