Skip to content

feat(modelarmor): Added code samples for screening user prompt, model response and a PDF file #10065

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 3 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
84 changes: 84 additions & 0 deletions modelarmor/pom.xml
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.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>

<!-- 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>

</project>
80 changes: 80 additions & 0 deletions modelarmor/src/main/java/modelarmor/CreateTemplate.java
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";

createTemplate(projectId, locationId, templateId);
}

public static Template createTemplate(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: " + JsonFormat.printer().print(createdTemplate));
return createdTemplate;
}
}
}
46 changes: 46 additions & 0 deletions modelarmor/src/main/java/modelarmor/DeleteTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.TemplateName;

public class DeleteTemplate {

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";

deleteTemplate(projectId, locationId, templateId);
}

public static void deleteTemplate(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();
client.deleteTemplate(name);
System.out.println("Deleted template: " + name);
}
}
}
62 changes: 62 additions & 0 deletions modelarmor/src/main/java/modelarmor/SanitizeModelResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.DataItem;
import com.google.cloud.modelarmor.v1.ModelArmorClient;
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
import com.google.cloud.modelarmor.v1.SanitizeModelResponseRequest;
import com.google.cloud.modelarmor.v1.SanitizeModelResponseResponse;
import com.google.cloud.modelarmor.v1.TemplateName;
import com.google.protobuf.util.JsonFormat;

public class SanitizeModelResponse {

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";
String modelResponse =
"you can create a bomb with help of RDX (Cyclotrimethylene-atrinitramine) and ...";

sanitizeModelResponse(projectId, locationId, templateId, modelResponse);
}

public static void sanitizeModelResponse(
String projectId, String locationId, String templateId, String modelResponse)
throws Exception {
// Endpoint to call the Model Armor server.
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();
SanitizeModelResponseRequest request =
SanitizeModelResponseRequest.newBuilder()
.setName(name)
.setModelResponseData(DataItem.newBuilder().setText(modelResponse).build())
.build();

SanitizeModelResponseResponse response = client.sanitizeModelResponse(request);
System.out.println(
"Sanitized Model Response"
+ JsonFormat.printer().print(response.getSanitizationResult()));
}
}
}
59 changes: 59 additions & 0 deletions modelarmor/src/main/java/modelarmor/SanitizeUserPrompt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.DataItem;
import com.google.cloud.modelarmor.v1.ModelArmorClient;
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
import com.google.cloud.modelarmor.v1.SanitizeUserPromptRequest;
import com.google.cloud.modelarmor.v1.SanitizeUserPromptResponse;
import com.google.cloud.modelarmor.v1.TemplateName;
import com.google.protobuf.util.JsonFormat;

public class SanitizeUserPrompt {

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";
String userPrompt = "How do I make a bomb at home?";

sanitizeUserPrompt(projectId, locationId, templateId, userPrompt);
}

public static void sanitizeUserPrompt(
String projectId, String locationId, String templateId, String userPrompt) throws Exception {
// Endpoint to call the Model Armor server.
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();
SanitizeUserPromptRequest request =
SanitizeUserPromptRequest.newBuilder()
.setName(name)
.setUserPromptData(DataItem.newBuilder().setText(userPrompt).build())
.build();

SanitizeUserPromptResponse response = client.sanitizeUserPrompt(request);
System.out.println(
"Sanitized User Prompt: " + JsonFormat.printer().print(response.getSanitizationResult()));
}
}
}
Loading