diff --git a/modelarmor/pom.xml b/modelarmor/pom.xml new file mode 100644 index 00000000000..b343f166bf1 --- /dev/null +++ b/modelarmor/pom.xml @@ -0,0 +1,83 @@ + + + + 4.0.0 + com.example.modelarmor + modelarmor-samples + jar + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + UTF-8 + 11 + 11 + + + + + + com.google.cloud + libraries-bom + 26.59.0 + pom + import + + + + + + + com.google.cloud + google-cloud-modelarmor + + + + com.google.cloud + google-cloud-dlp + + + + com.google.protobuf + protobuf-java-util + + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.4.0 + test + + + + diff --git a/modelarmor/src/main/java/modelarmor/Quickstart.java b/modelarmor/src/main/java/modelarmor/Quickstart.java new file mode 100644 index 00000000000..93cbcc0e2bb --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/Quickstart.java @@ -0,0 +1,145 @@ +/* + * 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_quickstart] + +import com.google.cloud.modelarmor.v1.CreateTemplateRequest; +import com.google.cloud.modelarmor.v1.DataItem; +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.SanitizeModelResponseRequest; +import com.google.cloud.modelarmor.v1.SanitizeModelResponseResponse; +import com.google.cloud.modelarmor.v1.SanitizeUserPromptRequest; +import com.google.cloud.modelarmor.v1.SanitizeUserPromptResponse; +import com.google.cloud.modelarmor.v1.Template; +import com.google.protobuf.util.JsonFormat; +import java.io.IOException; +import java.util.List; + +public class Quickstart { + + public 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"; + + // Run quickstart method. + quickstart(projectId, locationId, templateId); + } + + // This is an example to demonstrate how to use Model Armor to screen + // user prompts and model responses using a Model Armor template. + public static void quickstart(String projectId, String locationId, String templateId) + throws IOException { + + // Endpoint to call the Model Armor server. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); + ModelArmorSettings.Builder builder = ModelArmorSettings.newBuilder(); + ModelArmorSettings modelArmorSettings = builder.setEndpoint(apiEndpoint).build(); + + // Initialize the client that will be used to send requests. This client + // only needs to be created once, and can be reused for multiple requests. + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + + // Build the parent name from the project and location. + String parent = LocationName.of(projectId, locationId).toString(); + // Build the Model Armor template with your preferred filters. + // For more details on filters, please refer to the following doc: + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + + // Configure Responsible AI filter with multiple categories and their + // confidence levels. + RaiFilterSettings raiFilterSettings = + RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = + FilterConfig.newBuilder().setRaiSettings(raiFilterSettings).build(); + + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .build(); + + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); + + Template createdTemplate = client.createTemplate(request); + System.out.println("Created template: " + createdTemplate.getName()); + + // Screen a user prompt using the created template. + String userPrompt = "Unsafe user prompt"; + SanitizeUserPromptRequest userPromptRequest = + SanitizeUserPromptRequest.newBuilder() + .setName(createdTemplate.getName()) + .setUserPromptData(DataItem.newBuilder().setText(userPrompt).build()) + .build(); + + SanitizeUserPromptResponse userPromptResponse = client.sanitizeUserPrompt(userPromptRequest); + System.out.println( + "Result for the provided user prompt: " + + JsonFormat.printer().print(userPromptResponse.getSanitizationResult())); + + // Screen a model response using the created template. + String modelResponse = "Unsanitized model output"; + SanitizeModelResponseRequest modelResponseRequest = + SanitizeModelResponseRequest.newBuilder() + .setName(createdTemplate.getName()) + .setModelResponseData(DataItem.newBuilder().setText(modelResponse).build()) + .build(); + + SanitizeModelResponseResponse modelResponseResult = + client.sanitizeModelResponse(modelResponseRequest); + System.out.println( + "Result for the provided model response: " + + JsonFormat.printer().print(modelResponseResult.getSanitizationResult())); + } + } +} +// [END modelarmor_quickstart] diff --git a/modelarmor/src/test/java/modelarmor/QuickstartIT.java b/modelarmor/src/test/java/modelarmor/QuickstartIT.java new file mode 100644 index 00000000000..27019c0d75d --- /dev/null +++ b/modelarmor/src/test/java/modelarmor/QuickstartIT.java @@ -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 static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.modelarmor.v1.DeleteTemplateRequest; +import com.google.cloud.modelarmor.v1.ModelArmorClient; +import com.google.cloud.modelarmor.v1.ModelArmorSettings; +import com.google.cloud.modelarmor.v1.TemplateName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class QuickstartIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv() + .getOrDefault("GOOGLE_CLOUD_PROJECT_LOCATION", "us-central1"); + private static final String TEMPLATE_ID = "java-quickstart-" + UUID.randomUUID().toString(); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull("Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @AfterClass + public static void afterAll() throws IOException { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + // Delete the template created by quickstart. + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", LOCATION_ID); + + ModelArmorSettings.Builder builder = ModelArmorSettings.newBuilder(); + ModelArmorSettings modelArmorSettings = builder.setEndpoint(apiEndpoint).build(); + + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { + String templateName = TemplateName.of(PROJECT_ID, LOCATION_ID, TEMPLATE_ID).toString(); + client.deleteTemplate(DeleteTemplateRequest.newBuilder().setName(templateName).build()); + } + } + + @Test + public void quickstart_test() throws IOException { + PrintStream originalOut = System.out; + ByteArrayOutputStream redirected = new ByteArrayOutputStream(); + + System.setOut(new PrintStream(redirected)); + + try { + Quickstart.quickstart(PROJECT_ID, LOCATION_ID, TEMPLATE_ID); + assertThat(redirected.toString()).contains("Result for the provided user prompt:"); + assertThat(redirected.toString()).contains("Result for the provided model response:"); + } finally { + System.setOut(originalOut); + } + } +}