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 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
76 changes: 76 additions & 0 deletions modelarmor/src/main/java/modelarmor/SanitizeModelResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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_sanitize_model_response]

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;
import java.io.IOException;

public class SanitizeModelResponse {

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";
// Specify the model response.
String modelResponse = "Unsanitized model output";

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

public static SanitizeModelResponseResponse sanitizeModelResponse(String projectId,
String locationId, String templateId, String modelResponse) throws IOException {

// 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)) {
// Build the resource name of the template.
String name = TemplateName.of(projectId, locationId, templateId).toString();

// Prepare the request.
SanitizeModelResponseRequest request =
SanitizeModelResponseRequest.newBuilder()
.setName(name)
.setModelResponseData(
DataItem.newBuilder().setText(modelResponse)
.build())
.build();

SanitizeModelResponseResponse response = client.sanitizeModelResponse(request);
System.out.println("Result for the provided model response: "
+ JsonFormat.printer().print(response.getSanitizationResult()));

return response;
}
}
}
// [END modelarmor_sanitize_model_response]
74 changes: 74 additions & 0 deletions modelarmor/src/main/java/modelarmor/SanitizeUserPrompt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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_sanitize_user_prompt]

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;
import java.io.IOException;

public class SanitizeUserPrompt {

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";
// Specify the user prompt.
String userPrompt = "Unsafe user prompt";

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

public static SanitizeUserPromptResponse sanitizeUserPrompt(String projectId, String locationId,
String templateId, String userPrompt) throws IOException {

// 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)) {
// Build the resource name of the template.
String templateName = TemplateName.of(projectId, locationId, templateId).toString();

// Prepare the request.
SanitizeUserPromptRequest request = SanitizeUserPromptRequest.newBuilder()
.setName(templateName)
.setUserPromptData(DataItem.newBuilder().setText(userPrompt).build())
.build();

SanitizeUserPromptResponse response = client.sanitizeUserPrompt(request);
System.out.println("Result for the provided user prompt: "
+ JsonFormat.printer().print(response.getSanitizationResult()));

return response;
}
}
}
// [END modelarmor_sanitize_user_prompt]
93 changes: 93 additions & 0 deletions modelarmor/src/main/java/modelarmor/ScreenPdfFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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_screen_pdf_file]

import com.google.cloud.modelarmor.v1.ByteDataItem;
import com.google.cloud.modelarmor.v1.ByteDataItem.ByteItemType;
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.ByteString;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ScreenPdfFile {

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";
// Specify the PDF file path. Replace with your PDF file path.
String pdfFilePath = "src/main/resources/test_sample.pdf";

screenPdfFile(projectId, locationId, templateId, pdfFilePath);
}

public static SanitizeUserPromptResponse screenPdfFile(String projectId, String locationId,
String templateId, String pdfFilePath) throws IOException {

// 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)) {
// Build the resource name of the template.
String name = TemplateName.of(projectId, locationId, templateId).toString();

// Read the PDF file content and encode it to Base64.
byte[] fileContent = Files.readAllBytes(Paths.get(pdfFilePath));

// Prepare the request.
DataItem userPromptData = DataItem.newBuilder()
.setByteItem(
ByteDataItem.newBuilder()
.setByteDataType(ByteItemType.PDF)
.setByteData(ByteString.copyFrom(fileContent))
.build())
.build();

SanitizeUserPromptRequest request =
SanitizeUserPromptRequest.newBuilder()
.setName(name)
.setUserPromptData(userPromptData)
.build();

// Send the request and get the response.
SanitizeUserPromptResponse response = client.sanitizeUserPrompt(request);

// Print the sanitization result.
System.out.println("Result for the provided PDF file: "
+ JsonFormat.printer().print(response.getSanitizationResult()));

return response;
}
}
}
// [END modelarmor_screen_pdf_file]
Binary file added modelarmor/src/main/resources/test_sample.pdf
Binary file not shown.
Loading