From 974db03a560b0ab8715375147c32c71341dd6f86 Mon Sep 17 00:00:00 2001 From: rudrakhsha Date: Fri, 11 Apr 2025 12:30:42 +0000 Subject: [PATCH 01/13] Added create template samples for java --- modelarmor/README.md | 29 +++++ modelarmor/pom.xml | 112 ++++++++++++++++++ .../main/java/modelarmor/CreateTemplate.java | 80 +++++++++++++ .../CreateTemplateWithBasicSdp.java | 81 +++++++++++++ .../modelarmor/CreateTemplateWithLabels.java | 87 ++++++++++++++ .../CreateTemplateWithMetadata.java | 85 +++++++++++++ .../src/test/java/modelarmor/SnippetsIT.java | 108 +++++++++++++++++ 7 files changed, 582 insertions(+) create mode 100644 modelarmor/README.md create mode 100644 modelarmor/pom.xml create mode 100644 modelarmor/src/main/java/modelarmor/CreateTemplate.java create mode 100644 modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java create mode 100644 modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java create mode 100644 modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java create mode 100644 modelarmor/src/test/java/modelarmor/SnippetsIT.java diff --git a/modelarmor/README.md b/modelarmor/README.md new file mode 100644 index 00000000000..0c8113a9377 --- /dev/null +++ b/modelarmor/README.md @@ -0,0 +1,29 @@ +# Google Model Armor + + +Open in Cloud Shell + +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) + +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= +``` \ No newline at end of file diff --git a/modelarmor/pom.xml b/modelarmor/pom.xml new file mode 100644 index 00000000000..a23e8770bfa --- /dev/null +++ b/modelarmor/pom.xml @@ -0,0 +1,112 @@ + + + + 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 + + + + org.projectlombok + lombok + 1.18.30 + provided + + + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.4.0 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.12.1 + + 11 + 11 + + + org.projectlombok + lombok + 1.18.30 + + + + + + + + diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java new file mode 100644 index 00000000000..3d3e0b44c3d --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.java @@ -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; + } + } +} diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java new file mode 100644 index 00000000000..1649773a082 --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java @@ -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)); + } + } +} diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java new file mode 100644 index 00000000000..6b40947e9db --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.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 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 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)); + } + } +} diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java new file mode 100644 index 00000000000..52de3843a1f --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java @@ -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.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 CreateTemplateWithMetadata { + + 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"; + + createTemplateWithMetadata(projectId, locationId, templateId); + } + + public static void createTemplateWithMetadata( + 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() + .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(); + + CreateTemplateRequest request = + CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); + + Template createdTemplate = client.createTemplate(request); + System.out.println( + "Created template with metadata: " + JsonFormat.printer().print(createdTemplate)); + } + } +} diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java new file mode 100644 index 00000000000..72e9019f23d --- /dev/null +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -0,0 +1,108 @@ +/* + * 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 com.google.cloud.modelarmor.v1.Template; +import com.google.common.base.Strings; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link Snippets}. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") +public class SnippetsIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION = "us-central1"; + private static final String MA_REGIONAL_ENDPOINT = + String.format("modelarmor.%s.rep.googleapis.com:443", LOCATION); + private static final String DLP_REGIONAL_ENDPOINT = + String.format("dlp.%s.rep.googleapis.com:443", LOCATION); + private static final String INSPECT_TEMPLATE_ID = + "model-armour-inspect-template-" + UUID.randomUUID().toString(); + private static final String DEIDENTIFY_TEMPLATE_ID = + "model-armour-deidentify-template-" + UUID.randomUUID().toString(); + private static Template TEST_MODELARMOR_TEMPLATE; + private static Template TEST_MODELARMOR_TEMPLATE_NAME; + private static String TEMPLATE_ID; + + private ByteArrayOutputStream stdOut; + + @BeforeClass + public static void beforeAll() throws Exception { + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION)); + } + + @AfterClass + public static void afterAll() throws Exception { + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); + } + + @Before + public void beforeEach() { + stdOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(stdOut)); + + TEMPLATE_ID = "test-model-armor-" + UUID.randomUUID().toString(); + } + + @After + public void afterEach() throws Exception { + stdOut = null; + System.setOut(null); + } + + @Test + public void testCreateModelArmorTemplate() throws Exception { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + assertThat(stdOut.toString()).contains("Created template"); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + } + + @Test + public void testCreateModelArmorTemplateWithBasicSDP() throws Exception { + CreateTemplateWithBasicSdp.createTemplateWithBasicSdp(PROJECT_ID, LOCATION, TEMPLATE_ID); + assertThat(stdOut.toString()).contains("Created template with basic sdp: "); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + } + + @Test + public void testCreateModelArmorTemplateWithLabels() throws Exception { + CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, LOCATION, TEMPLATE_ID); + assertThat(stdOut.toString()).contains("Created template with labels"); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + } + + @Test + public void testCreateModelArmorTemplateWithMetadata() throws Exception { + CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, LOCATION, TEMPLATE_ID); + assertThat(stdOut.toString()).contains("Created template with metadata"); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + } +} From 2bdf84a52809bf70183a5c897c40aead030a21a0 Mon Sep 17 00:00:00 2001 From: rudrakhsha Date: Fri, 11 Apr 2025 13:15:38 +0000 Subject: [PATCH 02/13] Added delete template code snippet --- .../main/java/modelarmor/DeleteTemplate.java | 45 +++++++++++++++++++ .../src/test/java/modelarmor/SnippetsIT.java | 7 +++ 2 files changed, 52 insertions(+) create mode 100644 modelarmor/src/main/java/modelarmor/DeleteTemplate.java diff --git a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java new file mode 100644 index 00000000000..7e2f2f49529 --- /dev/null +++ b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java @@ -0,0 +1,45 @@ +/* + * 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); + } + } +} diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 72e9019f23d..22f4c09f9ba 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -105,4 +105,11 @@ public void testCreateModelArmorTemplateWithMetadata() throws Exception { assertThat(stdOut.toString()).contains("Created template with metadata"); DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); } + + @Test + public void testDeleteModelArmorTemplate() throws Exception { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + assertThat(stdOut.toString()).contains("Deleted template"); + } } From a96a594f6412111e0c39e129908f6eb6dd242b36 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Tue, 15 Apr 2025 01:05:08 +0530 Subject: [PATCH 03/13] address-review-comments --- modelarmor/README.md | 29 ----- modelarmor/pom.xml | 29 ----- .../main/java/modelarmor/CreateTemplate.java | 80 ++++++++----- .../CreateTemplateWithBasicSdp.java | 77 +++++++------ .../modelarmor/CreateTemplateWithLabels.java | 91 +++++++++------ .../CreateTemplateWithMetadata.java | 99 ++++++++++------ .../main/java/modelarmor/DeleteTemplate.java | 15 ++- .../src/test/java/modelarmor/SnippetsIT.java | 108 +++++++++++------- 8 files changed, 289 insertions(+), 239 deletions(-) delete mode 100644 modelarmor/README.md diff --git a/modelarmor/README.md b/modelarmor/README.md deleted file mode 100644 index 0c8113a9377..00000000000 --- a/modelarmor/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# Google Model Armor - - -Open in Cloud Shell - -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) - -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= -``` \ No newline at end of file diff --git a/modelarmor/pom.xml b/modelarmor/pom.xml index a23e8770bfa..57d56f73f0f 100644 --- a/modelarmor/pom.xml +++ b/modelarmor/pom.xml @@ -66,13 +66,6 @@ protobuf-java-util - - org.projectlombok - lombok - 1.18.30 - provided - - junit @@ -87,26 +80,4 @@ test - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.12.1 - - 11 - 11 - - - org.projectlombok - lombok - 1.18.30 - - - - - - - diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index 3d3e0b44c3d..2cc4848aadc 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplate.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.java @@ -16,6 +16,11 @@ package modelarmor; +import java.io.IOException; +import java.util.List; + +// [START modelarmor_create_template] + import com.google.cloud.modelarmor.v1.CreateTemplateRequest; import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; import com.google.cloud.modelarmor.v1.FilterConfig; @@ -26,12 +31,10 @@ 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 { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -41,40 +44,59 @@ public static void main(String[] args) throws Exception { } public static Template createTemplate(String projectId, String locationId, String templateId) - throws Exception { + throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = - ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + 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(); + // 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.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_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: " + JsonFormat.printer().print(createdTemplate)); + System.out.println("Created template: " + createdTemplate.getName()); + return createdTemplate; } } } +// [END modelarmor_create_template] diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java index 1649773a082..566e650caa9 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java @@ -16,23 +16,23 @@ package modelarmor; +import java.io.IOException; + +// [START modelarmor_create_template_with_basic_sdp] + 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.SdpBasicConfig; +import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement; +import com.google.cloud.modelarmor.v1.SdpFilterSettings; 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 { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -41,41 +41,46 @@ public static void main(String[] args) throws Exception { createTemplateWithBasicSdp(projectId, locationId, templateId); } - public static void createTemplateWithBasicSdp( - String projectId, String locationId, String templateId) throws Exception { + public static Template createTemplateWithBasicSdp( + String projectId, String locationId, String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = - ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + 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(); + // 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 Basic SDP Filter. + SdpBasicConfig basicSdpConfig = SdpBasicConfig.newBuilder() + .setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED) + .build(); + + SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder() + .setBasicConfig(basicSdpConfig) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setSdpSettings(sdpSettings) + .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 with basic sdp: " + JsonFormat.printer().print(createdTemplate)); + System.out.println("Created template with basic SDP filter: " + createdTemplate.getName()); + + return createdTemplate; } } } +// [END modelarmor_create_template_with_basic_sdp] diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java index 6b40947e9db..4ed1462b45e 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java @@ -16,6 +16,13 @@ package modelarmor; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +// [START modelarmor_create_template_with_labels] + import com.google.cloud.modelarmor.v1.CreateTemplateRequest; import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; import com.google.cloud.modelarmor.v1.FilterConfig; @@ -26,14 +33,10 @@ 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 { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -42,46 +45,66 @@ public static void main(String[] args) throws Exception { createTemplateWithLabels(projectId, locationId, templateId); } - public static void createTemplateWithLabels( - String projectId, String locationId, String templateId) throws Exception { + public static Template createTemplateWithLabels( + String projectId, String locationId, String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = - ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { 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.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) + .build(); + + // Create Labels. Map 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 template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .putAllLabels(labels) + .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)); + System.out.println("Created template with labels: " + createdTemplate.getName()); + + return createdTemplate; } } } +// [END modelarmor_create_template_with_labels] diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java index 52de3843a1f..273a1b89e86 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java @@ -16,6 +16,11 @@ package modelarmor; +import java.io.IOException; +import java.util.List; + +// [START modelarmor_create_template_with_metadata] + import com.google.cloud.modelarmor.v1.CreateTemplateRequest; import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; import com.google.cloud.modelarmor.v1.FilterConfig; @@ -26,13 +31,11 @@ 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; +import com.google.cloud.modelarmor.v1.Template.TemplateMetadata; public class CreateTemplateWithMetadata { - public static void main(String[] args) throws Exception { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -41,45 +44,69 @@ public static void main(String[] args) throws Exception { createTemplateWithMetadata(projectId, locationId, templateId); } - public static void createTemplateWithMetadata( - String projectId, String locationId, String templateId) throws Exception { + public static Template createTemplateWithMetadata( + String projectId, String locationId, String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = - ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { String parent = LocationName.of(projectId, locationId).toString(); - Template template = - Template.newBuilder() - .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(); - - CreateTemplateRequest request = - CreateTemplateRequest.newBuilder() - .setParent(parent) - .setTemplateId(templateId) - .setTemplate(template) - .build(); + // 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.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) + .build(); + + // For more details about metadata, refer to the following documentation: + // https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#templatemetadata + TemplateMetadata templateMetadata = TemplateMetadata.newBuilder() + .setIgnorePartialInvocationFailures(true) + .setLogSanitizeOperations(true) + .setCustomPromptSafetyErrorCode(500) + .build(); + + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .setTemplateMetadata(templateMetadata) + .build(); + + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); Template createdTemplate = client.createTemplate(request); - System.out.println( - "Created template with metadata: " + JsonFormat.printer().print(createdTemplate)); + System.out.println("Created template with metadata: " + createdTemplate.getName()); + + return createdTemplate; } } } +// [END modelarmor_create_template_with_metadata] diff --git a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java index 7e2f2f49529..a86bbf4f4ba 100644 --- a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java +++ b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java @@ -16,13 +16,17 @@ package modelarmor; +// [START modelarmor_delete_template] + +import java.io.IOException; + 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 { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String locationId = "your-location-id"; @@ -32,14 +36,17 @@ public static void main(String[] args) throws Exception { } public static void deleteTemplate(String projectId, String locationId, String templateId) - throws Exception { + throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = - ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { String name = TemplateName.of(projectId, locationId, templateId).toString(); + + // Note: Ensure that the template you are deleting isn't used by any models. client.deleteTemplate(name); System.out.println("Deleted template: " + name); } } } +// [END modelarmor_delete_template] diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 22f4c09f9ba..4515bbc1ce3 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -17,12 +17,13 @@ package modelarmor; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertEquals; -import com.google.cloud.modelarmor.v1.Template; -import com.google.common.base.Strings; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; -import java.util.UUID; +import java.util.Random; + import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -32,36 +33,33 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement; +import com.google.cloud.modelarmor.v1.Template; +import com.google.cloud.modelarmor.v1.TemplateName; +import com.google.common.base.Strings; + /** Integration (system) tests for {@link Snippets}. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:AbbreviationAsWordInName") public class SnippetsIT { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String LOCATION = "us-central1"; - private static final String MA_REGIONAL_ENDPOINT = - String.format("modelarmor.%s.rep.googleapis.com:443", LOCATION); - private static final String DLP_REGIONAL_ENDPOINT = - String.format("dlp.%s.rep.googleapis.com:443", LOCATION); - private static final String INSPECT_TEMPLATE_ID = - "model-armour-inspect-template-" + UUID.randomUUID().toString(); - private static final String DEIDENTIFY_TEMPLATE_ID = - "model-armour-deidentify-template-" + UUID.randomUUID().toString(); - private static Template TEST_MODELARMOR_TEMPLATE; - private static Template TEST_MODELARMOR_TEMPLATE_NAME; - private static String TEMPLATE_ID; - + private static final String LOCATION_ID = System.getenv().getOrDefault("GOOGLE_CLOUD_PROJECT_LOCATION", + "us-central1"); + private static String TEST_TEMPLATE_ID; + private static String TEST_TEMPLATE_NAME; private ByteArrayOutputStream stdOut; @BeforeClass - public static void beforeAll() throws Exception { + public static void beforeAll() throws IOException { Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION)); + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION_ID)); } @AfterClass - public static void afterAll() throws Exception { + public static void afterAll() throws IOException { Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); + Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION_ID)); } @Before @@ -69,47 +67,73 @@ public void beforeEach() { stdOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(stdOut)); - TEMPLATE_ID = "test-model-armor-" + UUID.randomUUID().toString(); + TEST_TEMPLATE_ID = randomId(); + TEST_TEMPLATE_NAME = TemplateName.of(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID).toString(); } @After - public void afterEach() throws Exception { + public void afterEach() throws IOException { + + try { + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + } catch (NotFoundException e) { + // Ignore not found error - template already deleted. + } + stdOut = null; System.setOut(null); } + private static String randomId() { + Random random = new Random(); + return "java-ma-" + random.nextLong(); + } + @Test - public void testCreateModelArmorTemplate() throws Exception { - CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Created template"); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + public void testCreateModelArmorTemplate() throws IOException { + Template createdTemplate = CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + + assertThat(stdOut.toString()).contains("Created template:"); + assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); } @Test - public void testCreateModelArmorTemplateWithBasicSDP() throws Exception { - CreateTemplateWithBasicSdp.createTemplateWithBasicSdp(PROJECT_ID, LOCATION, TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Created template with basic sdp: "); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + public void testCreateModelArmorTemplateWithBasicSDP() throws IOException { + Template createdTemplate = CreateTemplateWithBasicSdp.createTemplateWithBasicSdp(PROJECT_ID, LOCATION_ID, + TEST_TEMPLATE_ID); + + assertThat(stdOut.toString()).contains("Created template with basic SDP filter:"); + assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); + assertEquals(SdpBasicConfigEnforcement.ENABLED, + createdTemplate.getFilterConfig().getSdpSettings().getBasicConfig().getFilterEnforcement()); } @Test - public void testCreateModelArmorTemplateWithLabels() throws Exception { - CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, LOCATION, TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Created template with labels"); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + public void testCreateModelArmorTemplateWithLabels() throws IOException { + Template createdTemplate = CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, LOCATION_ID, + TEST_TEMPLATE_ID); + + assertThat(stdOut.toString()).contains("Created template with labels:"); + assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); } @Test - public void testCreateModelArmorTemplateWithMetadata() throws Exception { - CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, LOCATION, TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Created template with metadata"); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); + public void testCreateModelArmorTemplateWithMetadata() throws IOException { + Template createdTemplate = CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, LOCATION_ID, + TEST_TEMPLATE_ID); + + assertThat(stdOut.toString()).contains("Created template with metadata:"); + assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); + assertEquals(true, createdTemplate.getTemplateMetadata().getIgnorePartialInvocationFailures()); + assertEquals(true, createdTemplate.getTemplateMetadata().getLogSanitizeOperations()); + assertEquals(500, createdTemplate.getTemplateMetadata().getCustomPromptSafetyErrorCode()); } @Test - public void testDeleteModelArmorTemplate() throws Exception { - CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Deleted template"); + public void testDeleteModelArmorTemplate() throws IOException { + CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + + assertThat(stdOut.toString()).contains("Deleted template:"); } } From 856db9073937c2d5ba90525f54748fe664d1e769 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Tue, 15 Apr 2025 01:56:59 +0530 Subject: [PATCH 04/13] fix-lint --- .../main/java/modelarmor/CreateTemplate.java | 49 ++++++--------- .../CreateTemplateWithBasicSdp.java | 31 ++++------ .../modelarmor/CreateTemplateWithLabels.java | 59 +++++++----------- .../CreateTemplateWithMetadata.java | 61 +++++++------------ .../main/java/modelarmor/DeleteTemplate.java | 6 +- .../src/test/java/modelarmor/SnippetsIT.java | 31 +++++----- 6 files changed, 90 insertions(+), 147 deletions(-) diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index 2cc4848aadc..5d1857b4e7d 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplate.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.java @@ -16,9 +16,6 @@ package modelarmor; -import java.io.IOException; -import java.util.List; - // [START modelarmor_create_template] import com.google.cloud.modelarmor.v1.CreateTemplateRequest; @@ -31,6 +28,8 @@ import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; import com.google.cloud.modelarmor.v1.RaiFilterType; import com.google.cloud.modelarmor.v1.Template; +import java.io.IOException; +import java.util.List; public class CreateTemplate { @@ -46,7 +45,8 @@ public static void main(String[] args) throws IOException { public static Template createTemplate(String projectId, String locationId, String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { String parent = LocationName.of(projectId, locationId).toString(); @@ -58,39 +58,24 @@ public static Template createTemplate(String projectId, String locationId, Strin // 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.HIGH) - .build(), - RaiFilter.newBuilder() - .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) - .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) - .build(), - RaiFilter.newBuilder() - .setFilterType(RaiFilterType.HARASSMENT) - .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) - .build())) + .addAllRaiFilters(List.of( + RaiFilter.newBuilder().setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE).build())) .build(); - FilterConfig modelArmorFilter = FilterConfig.newBuilder() - .setRaiSettings(raiFilterSettings) + FilterConfig modelArmorFilter = FilterConfig.newBuilder().setRaiSettings(raiFilterSettings) .build(); - Template template = Template.newBuilder() - .setFilterConfig(modelArmorFilter) - .build(); + Template template = Template.newBuilder().setFilterConfig(modelArmorFilter).build(); - CreateTemplateRequest request = CreateTemplateRequest.newBuilder() - .setParent(parent) - .setTemplateId(templateId) - .setTemplate(template) - .build(); + CreateTemplateRequest request = CreateTemplateRequest.newBuilder().setParent(parent) + .setTemplateId(templateId).setTemplate(template).build(); Template createdTemplate = client.createTemplate(request); System.out.println("Created template: " + createdTemplate.getName()); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java index 566e650caa9..81e106d590c 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java @@ -16,8 +16,6 @@ package modelarmor; -import java.io.IOException; - // [START modelarmor_create_template_with_basic_sdp] import com.google.cloud.modelarmor.v1.CreateTemplateRequest; @@ -29,6 +27,7 @@ import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement; import com.google.cloud.modelarmor.v1.SdpFilterSettings; import com.google.cloud.modelarmor.v1.Template; +import java.io.IOException; public class CreateTemplateWithBasicSdp { @@ -41,10 +40,11 @@ public static void main(String[] args) throws IOException { createTemplateWithBasicSdp(projectId, locationId, templateId); } - public static Template createTemplateWithBasicSdp( - String projectId, String locationId, String templateId) throws IOException { + public static Template createTemplateWithBasicSdp(String projectId, String locationId, + String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { String parent = LocationName.of(projectId, locationId).toString(); @@ -55,26 +55,17 @@ public static Template createTemplateWithBasicSdp( // Configure Basic SDP Filter. SdpBasicConfig basicSdpConfig = SdpBasicConfig.newBuilder() - .setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED) - .build(); + .setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED).build(); - SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder() - .setBasicConfig(basicSdpConfig) + SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder().setBasicConfig(basicSdpConfig) .build(); - FilterConfig modelArmorFilter = FilterConfig.newBuilder() - .setSdpSettings(sdpSettings) - .build(); + FilterConfig modelArmorFilter = FilterConfig.newBuilder().setSdpSettings(sdpSettings).build(); - Template template = Template.newBuilder() - .setFilterConfig(modelArmorFilter) - .build(); + Template template = Template.newBuilder().setFilterConfig(modelArmorFilter).build(); - CreateTemplateRequest request = CreateTemplateRequest.newBuilder() - .setParent(parent) - .setTemplateId(templateId) - .setTemplate(template) - .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 filter: " + createdTemplate.getName()); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java index 4ed1462b45e..d454c2a0cf5 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java @@ -16,11 +16,6 @@ package modelarmor; -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - // [START modelarmor_create_template_with_labels] import com.google.cloud.modelarmor.v1.CreateTemplateRequest; @@ -33,6 +28,10 @@ import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; import com.google.cloud.modelarmor.v1.RaiFilterType; import com.google.cloud.modelarmor.v1.Template; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class CreateTemplateWithLabels { @@ -45,10 +44,11 @@ public static void main(String[] args) throws IOException { createTemplateWithLabels(projectId, locationId, templateId); } - public static Template createTemplateWithLabels( - String projectId, String locationId, String templateId) throws IOException { + public static Template createTemplateWithLabels(String projectId, String locationId, + String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { String parent = LocationName.of(projectId, locationId).toString(); @@ -60,28 +60,18 @@ public static Template createTemplateWithLabels( // 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.HIGH) - .build(), - RaiFilter.newBuilder() - .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) - .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) - .build(), - RaiFilter.newBuilder() - .setFilterType(RaiFilterType.HARASSMENT) - .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) - .build())) + .addAllRaiFilters(List.of( + RaiFilter.newBuilder().setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE).build())) .build(); - FilterConfig modelArmorFilter = FilterConfig.newBuilder() - .setRaiSettings(raiFilterSettings) + FilterConfig modelArmorFilter = FilterConfig.newBuilder().setRaiSettings(raiFilterSettings) .build(); // Create Labels. @@ -89,16 +79,11 @@ public static Template createTemplateWithLabels( labels.put("key1", "value1"); labels.put("key2", "value2"); - Template template = Template.newBuilder() - .setFilterConfig(modelArmorFilter) - .putAllLabels(labels) - .build(); + Template template = Template.newBuilder().setFilterConfig(modelArmorFilter) + .putAllLabels(labels).build(); - CreateTemplateRequest request = CreateTemplateRequest.newBuilder() - .setParent(parent) - .setTemplateId(templateId) - .setTemplate(template) - .build(); + CreateTemplateRequest request = CreateTemplateRequest.newBuilder().setParent(parent) + .setTemplateId(templateId).setTemplate(template).build(); Template createdTemplate = client.createTemplate(request); System.out.println("Created template with labels: " + createdTemplate.getName()); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java index 273a1b89e86..f47633597ff 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java @@ -16,9 +16,6 @@ package modelarmor; -import java.io.IOException; -import java.util.List; - // [START modelarmor_create_template_with_metadata] import com.google.cloud.modelarmor.v1.CreateTemplateRequest; @@ -32,6 +29,8 @@ import com.google.cloud.modelarmor.v1.RaiFilterType; import com.google.cloud.modelarmor.v1.Template; import com.google.cloud.modelarmor.v1.Template.TemplateMetadata; +import java.io.IOException; +import java.util.List; public class CreateTemplateWithMetadata { @@ -44,10 +43,11 @@ public static void main(String[] args) throws IOException { createTemplateWithMetadata(projectId, locationId, templateId); } - public static Template createTemplateWithMetadata( - String projectId, String locationId, String templateId) throws IOException { + public static Template createTemplateWithMetadata(String projectId, String locationId, + String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { String parent = LocationName.of(projectId, locationId).toString(); @@ -59,48 +59,31 @@ public static Template createTemplateWithMetadata( // 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.HIGH) - .build(), - RaiFilter.newBuilder() - .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) - .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) - .build(), - RaiFilter.newBuilder() - .setFilterType(RaiFilterType.HARASSMENT) - .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) - .build())) + .addAllRaiFilters(List.of( + RaiFilter.newBuilder().setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE).build(), + RaiFilter.newBuilder().setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE).build())) .build(); - FilterConfig modelArmorFilter = FilterConfig.newBuilder() - .setRaiSettings(raiFilterSettings) + FilterConfig modelArmorFilter = FilterConfig.newBuilder().setRaiSettings(raiFilterSettings) .build(); // For more details about metadata, refer to the following documentation: // https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#templatemetadata TemplateMetadata templateMetadata = TemplateMetadata.newBuilder() - .setIgnorePartialInvocationFailures(true) - .setLogSanitizeOperations(true) - .setCustomPromptSafetyErrorCode(500) - .build(); + .setIgnorePartialInvocationFailures(true).setLogSanitizeOperations(true) + .setCustomPromptSafetyErrorCode(500).build(); - Template template = Template.newBuilder() - .setFilterConfig(modelArmorFilter) - .setTemplateMetadata(templateMetadata) - .build(); + Template template = Template.newBuilder().setFilterConfig(modelArmorFilter) + .setTemplateMetadata(templateMetadata).build(); - CreateTemplateRequest request = CreateTemplateRequest.newBuilder() - .setParent(parent) - .setTemplateId(templateId) - .setTemplate(template) - .build(); + CreateTemplateRequest request = CreateTemplateRequest.newBuilder().setParent(parent) + .setTemplateId(templateId).setTemplate(template).build(); Template createdTemplate = client.createTemplate(request); System.out.println("Created template with metadata: " + createdTemplate.getName()); diff --git a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java index a86bbf4f4ba..803a8376833 100644 --- a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java +++ b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java @@ -18,11 +18,10 @@ // [START modelarmor_delete_template] -import java.io.IOException; - import com.google.cloud.modelarmor.v1.ModelArmorClient; import com.google.cloud.modelarmor.v1.ModelArmorSettings; import com.google.cloud.modelarmor.v1.TemplateName; +import java.io.IOException; public class DeleteTemplate { @@ -38,7 +37,8 @@ public static void main(String[] args) throws IOException { public static void deleteTemplate(String projectId, String locationId, String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); - ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); + ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) + .build(); try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { String name = TemplateName.of(projectId, locationId, templateId).toString(); diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 4515bbc1ce3..e42d1228d60 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -19,11 +19,15 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement; +import com.google.cloud.modelarmor.v1.Template; +import com.google.cloud.modelarmor.v1.TemplateName; +import com.google.common.base.Strings; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Random; - import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -33,19 +37,13 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import com.google.api.gax.rpc.NotFoundException; -import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement; -import com.google.cloud.modelarmor.v1.Template; -import com.google.cloud.modelarmor.v1.TemplateName; -import com.google.common.base.Strings; - /** Integration (system) tests for {@link Snippets}. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:AbbreviationAsWordInName") public class SnippetsIT { 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 LOCATION_ID = System.getenv() + .getOrDefault("GOOGLE_CLOUD_PROJECT_LOCATION", "us-central1"); private static String TEST_TEMPLATE_ID; private static String TEST_TEMPLATE_NAME; private ByteArrayOutputStream stdOut; @@ -91,7 +89,8 @@ private static String randomId() { @Test public void testCreateModelArmorTemplate() throws IOException { - Template createdTemplate = CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); + Template createdTemplate = CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, + TEST_TEMPLATE_ID); assertThat(stdOut.toString()).contains("Created template:"); assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); @@ -99,8 +98,8 @@ public void testCreateModelArmorTemplate() throws IOException { @Test public void testCreateModelArmorTemplateWithBasicSDP() throws IOException { - Template createdTemplate = CreateTemplateWithBasicSdp.createTemplateWithBasicSdp(PROJECT_ID, LOCATION_ID, - TEST_TEMPLATE_ID); + Template createdTemplate = CreateTemplateWithBasicSdp.createTemplateWithBasicSdp(PROJECT_ID, + LOCATION_ID, TEST_TEMPLATE_ID); assertThat(stdOut.toString()).contains("Created template with basic SDP filter:"); assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); @@ -110,8 +109,8 @@ public void testCreateModelArmorTemplateWithBasicSDP() throws IOException { @Test public void testCreateModelArmorTemplateWithLabels() throws IOException { - Template createdTemplate = CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, LOCATION_ID, - TEST_TEMPLATE_ID); + Template createdTemplate = CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, + LOCATION_ID, TEST_TEMPLATE_ID); assertThat(stdOut.toString()).contains("Created template with labels:"); assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); @@ -119,8 +118,8 @@ public void testCreateModelArmorTemplateWithLabels() throws IOException { @Test public void testCreateModelArmorTemplateWithMetadata() throws IOException { - Template createdTemplate = CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, LOCATION_ID, - TEST_TEMPLATE_ID); + Template createdTemplate = CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, + LOCATION_ID, TEST_TEMPLATE_ID); assertThat(stdOut.toString()).contains("Created template with metadata:"); assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); From da0224c4314b5f605c6b9771eba07c3e6e084d8a Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Tue, 15 Apr 2025 02:04:59 +0530 Subject: [PATCH 05/13] fix-lint --- .../main/java/modelarmor/CreateTemplate.java | 41 +++++++++---- .../CreateTemplateWithBasicSdp.java | 25 +++++--- .../modelarmor/CreateTemplateWithLabels.java | 54 +++++++++++------ .../CreateTemplateWithMetadata.java | 60 ++++++++++++------- 4 files changed, 119 insertions(+), 61 deletions(-) diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index 5d1857b4e7d..5cf72602e7c 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplate.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.java @@ -58,24 +58,39 @@ public static Template createTemplate(String projectId, String locationId, Strin // 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.HIGH).build(), - RaiFilter.newBuilder().setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) - .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE).build(), - RaiFilter.newBuilder().setFilterType(RaiFilterType.HARASSMENT) - .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE).build())) + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) .build(); - FilterConfig modelArmorFilter = FilterConfig.newBuilder().setRaiSettings(raiFilterSettings) + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) .build(); - Template template = Template.newBuilder().setFilterConfig(modelArmorFilter).build(); + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .build(); - CreateTemplateRequest request = CreateTemplateRequest.newBuilder().setParent(parent) - .setTemplateId(templateId).setTemplate(template).build(); + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); Template createdTemplate = client.createTemplate(request); System.out.println("Created template: " + createdTemplate.getName()); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java index 81e106d590c..726e138dad3 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java @@ -40,8 +40,8 @@ public static void main(String[] args) throws IOException { createTemplateWithBasicSdp(projectId, locationId, templateId); } - public static Template createTemplateWithBasicSdp(String projectId, String locationId, - String templateId) throws IOException { + public static Template createTemplateWithBasicSdp( + String projectId, String locationId, String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) .build(); @@ -55,17 +55,26 @@ public static Template createTemplateWithBasicSdp(String projectId, String locat // Configure Basic SDP Filter. SdpBasicConfig basicSdpConfig = SdpBasicConfig.newBuilder() - .setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED).build(); + .setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED) + .build(); - SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder().setBasicConfig(basicSdpConfig) + SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder() + .setBasicConfig(basicSdpConfig) .build(); - FilterConfig modelArmorFilter = FilterConfig.newBuilder().setSdpSettings(sdpSettings).build(); + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setSdpSettings(sdpSettings) + .build(); - Template template = Template.newBuilder().setFilterConfig(modelArmorFilter).build(); + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .build(); - CreateTemplateRequest request = CreateTemplateRequest.newBuilder().setParent(parent) - .setTemplateId(templateId).setTemplate(template).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 filter: " + createdTemplate.getName()); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java index d454c2a0cf5..ddc2101c09c 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java @@ -44,8 +44,8 @@ public static void main(String[] args) throws IOException { createTemplateWithLabels(projectId, locationId, templateId); } - public static Template createTemplateWithLabels(String projectId, String locationId, - String templateId) throws IOException { + public static Template createTemplateWithLabels( + String projectId, String locationId, String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) .build(); @@ -59,19 +59,30 @@ public static Template createTemplateWithLabels(String projectId, String locatio // 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.HIGH).build(), - RaiFilter.newBuilder().setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) - .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE).build(), - RaiFilter.newBuilder().setFilterType(RaiFilterType.HARASSMENT) - .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE).build())) - .build(); - - FilterConfig modelArmorFilter = FilterConfig.newBuilder().setRaiSettings(raiFilterSettings) + RaiFilterSettings raiFilterSettings = + RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) .build(); // Create Labels. @@ -79,11 +90,16 @@ public static Template createTemplateWithLabels(String projectId, String locatio labels.put("key1", "value1"); labels.put("key2", "value2"); - Template template = Template.newBuilder().setFilterConfig(modelArmorFilter) - .putAllLabels(labels).build(); + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .putAllLabels(labels) + .build(); - CreateTemplateRequest request = CreateTemplateRequest.newBuilder().setParent(parent) - .setTemplateId(templateId).setTemplate(template).build(); + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); Template createdTemplate = client.createTemplate(request); System.out.println("Created template with labels: " + createdTemplate.getName()); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java index f47633597ff..a5e1a50fec2 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java @@ -43,8 +43,8 @@ public static void main(String[] args) throws IOException { createTemplateWithMetadata(projectId, locationId, templateId); } - public static Template createTemplateWithMetadata(String projectId, String locationId, - String templateId) throws IOException { + public static Template createTemplateWithMetadata( + String projectId, String locationId, String templateId) throws IOException { String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint) .build(); @@ -58,32 +58,50 @@ public static Template createTemplateWithMetadata(String projectId, String locat // 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.HIGH).build(), - RaiFilter.newBuilder().setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) - .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE).build(), - RaiFilter.newBuilder().setFilterType(RaiFilterType.HARASSMENT) - .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE).build())) - .build(); - - FilterConfig modelArmorFilter = FilterConfig.newBuilder().setRaiSettings(raiFilterSettings) + RaiFilterSettings raiFilterSettings = + RaiFilterSettings.newBuilder() + .addAllRaiFilters( + List.of( + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.DANGEROUS) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HATE_SPEECH) + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.SEXUALLY_EXPLICIT) + .setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE) + .build(), + RaiFilter.newBuilder() + .setFilterType(RaiFilterType.HARASSMENT) + .setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE) + .build())) + .build(); + + FilterConfig modelArmorFilter = FilterConfig.newBuilder() + .setRaiSettings(raiFilterSettings) .build(); // For more details about metadata, refer to the following documentation: // https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#templatemetadata TemplateMetadata templateMetadata = TemplateMetadata.newBuilder() - .setIgnorePartialInvocationFailures(true).setLogSanitizeOperations(true) - .setCustomPromptSafetyErrorCode(500).build(); + .setIgnorePartialInvocationFailures(true) + .setLogSanitizeOperations(true) + .setCustomPromptSafetyErrorCode(500) + .build(); - Template template = Template.newBuilder().setFilterConfig(modelArmorFilter) - .setTemplateMetadata(templateMetadata).build(); + Template template = Template.newBuilder() + .setFilterConfig(modelArmorFilter) + .setTemplateMetadata(templateMetadata) + .build(); - CreateTemplateRequest request = CreateTemplateRequest.newBuilder().setParent(parent) - .setTemplateId(templateId).setTemplate(template).build(); + CreateTemplateRequest request = CreateTemplateRequest.newBuilder() + .setParent(parent) + .setTemplateId(templateId) + .setTemplate(template) + .build(); Template createdTemplate = client.createTemplate(request); System.out.println("Created template with metadata: " + createdTemplate.getName()); From d46f26e6a455f6d6117ed7ad68a2e23e2cf27206 Mon Sep 17 00:00:00 2001 From: harshnasitcrest <131268456+harshnasitcrest@users.noreply.github.com> Date: Tue, 15 Apr 2025 22:23:14 +0530 Subject: [PATCH 06/13] remove-unused-annotation --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index e42d1228d60..0bf35957cfa 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -39,7 +39,6 @@ /** Integration (system) tests for {@link Snippets}. */ @RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:AbbreviationAsWordInName") public class SnippetsIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String LOCATION_ID = System.getenv() From d5817bbbe6c2e15d55fceb75908e4b1e8c60f0b4 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Wed, 16 Apr 2025 18:57:23 +0530 Subject: [PATCH 07/13] sync code comments and update requireEnv function across snippets --- .../main/java/modelarmor/CreateTemplate.java | 8 +++++++ .../CreateTemplateWithBasicSdp.java | 8 +++++++ .../modelarmor/CreateTemplateWithLabels.java | 8 +++++++ .../CreateTemplateWithMetadata.java | 8 +++++++ .../main/java/modelarmor/DeleteTemplate.java | 8 +++++++ .../src/test/java/modelarmor/SnippetsIT.java | 22 ++++++++++++------- 6 files changed, 54 insertions(+), 8 deletions(-) diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplate.java b/modelarmor/src/main/java/modelarmor/CreateTemplate.java index 5cf72602e7c..dea12f19a8b 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplate.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplate.java @@ -35,8 +35,12 @@ public class CreateTemplate { 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"; createTemplate(projectId, locationId, templateId); @@ -44,10 +48,14 @@ public static void main(String[] args) throws IOException { public static Template createTemplate(String projectId, String locationId, String templateId) throws IOException { + + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().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)) { String parent = LocationName.of(projectId, locationId).toString(); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java index 726e138dad3..a88ab47b59a 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithBasicSdp.java @@ -33,8 +33,12 @@ public class CreateTemplateWithBasicSdp { 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"; createTemplateWithBasicSdp(projectId, locationId, templateId); @@ -42,10 +46,14 @@ public static void main(String[] args) throws IOException { public static Template createTemplateWithBasicSdp( String projectId, String locationId, String templateId) throws IOException { + + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().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)) { String parent = LocationName.of(projectId, locationId).toString(); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java index ddc2101c09c..0faf2c7a42f 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithLabels.java @@ -37,8 +37,12 @@ public class CreateTemplateWithLabels { 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"; createTemplateWithLabels(projectId, locationId, templateId); @@ -46,10 +50,14 @@ public static void main(String[] args) throws IOException { public static Template createTemplateWithLabels( String projectId, String locationId, String templateId) throws IOException { + + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().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)) { String parent = LocationName.of(projectId, locationId).toString(); diff --git a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java index a5e1a50fec2..5abf2bdb4cf 100644 --- a/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java +++ b/modelarmor/src/main/java/modelarmor/CreateTemplateWithMetadata.java @@ -36,8 +36,12 @@ public class CreateTemplateWithMetadata { 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"; createTemplateWithMetadata(projectId, locationId, templateId); @@ -45,10 +49,14 @@ public static void main(String[] args) throws IOException { public static Template createTemplateWithMetadata( String projectId, String locationId, String templateId) throws IOException { + + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().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)) { String parent = LocationName.of(projectId, locationId).toString(); diff --git a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java index 803a8376833..20cdb670cb0 100644 --- a/modelarmor/src/main/java/modelarmor/DeleteTemplate.java +++ b/modelarmor/src/main/java/modelarmor/DeleteTemplate.java @@ -27,8 +27,12 @@ public class DeleteTemplate { 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"; deleteTemplate(projectId, locationId, templateId); @@ -36,10 +40,14 @@ public static void main(String[] args) throws IOException { public static void deleteTemplate(String projectId, String locationId, String templateId) throws IOException { + + // Construct the API endpoint URL. String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().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)) { String name = TemplateName.of(projectId, locationId, templateId).toString(); diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index e42d1228d60..5a54b04ebb9 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -18,19 +18,18 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import com.google.api.gax.rpc.NotFoundException; import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement; import com.google.cloud.modelarmor.v1.Template; import com.google.cloud.modelarmor.v1.TemplateName; -import com.google.common.base.Strings; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Random; import org.junit.After; import org.junit.AfterClass; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -39,7 +38,6 @@ /** Integration (system) tests for {@link Snippets}. */ @RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:AbbreviationAsWordInName") public class SnippetsIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String LOCATION_ID = System.getenv() @@ -48,16 +46,24 @@ public class SnippetsIT { private static String TEST_TEMPLATE_NAME; private ByteArrayOutputStream stdOut; + // Check if the required environment variables are set. + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull("Environment variable " + varName + " is required to run these tests.", + System.getenv(varName)); + return value; + } + @BeforeClass - public static void beforeAll() throws IOException { - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION_ID)); + public static void beforeAll() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("GOOGLE_CLOUD_PROJECT_LOCATION"); } @AfterClass public static void afterAll() throws IOException { - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID)); - Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT_LOCATION", Strings.isNullOrEmpty(LOCATION_ID)); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("GOOGLE_CLOUD_PROJECT_LOCATION"); } @Before From 11634c28b179319fe2242e9c14a770b5bcedfce1 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Wed, 16 Apr 2025 19:23:26 +0530 Subject: [PATCH 08/13] added-codeowners-and-blunderbuss --- .github/CODEOWNERS | 1 + .github/blunderbuss.yml | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 08490e87aaf..09b11f04a7c 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -37,6 +37,7 @@ /iam @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra /iap @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra /kms @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra +/modelarmor @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/cloud-modelarmor-team /parametermanager @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/cloud-secrets-team @GoogleCloudPlatform/cloud-parameters-team /privateca @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra /recaptcha_enterprise @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/dee-infra diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index b46b2c816f3..06cb5cde1d0 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -96,6 +96,10 @@ assign_issues_by: - "api: bigquerydatatransfer" to: - GoogleCloudPlatform/bigquery-data-connectors +- labels: + - "api: modelarmor" + to: + - GoogleCloudPlatform/cloud-modelarmor-team assign_prs: - GoogleCloudPlatform/java-samples-reviewers @@ -173,3 +177,7 @@ assign_prs_by: - "api: appengine" to: - GoogleCloudPlatform/serverless-runtimes +- labels: + - "api: modelarmor" + to: + - GoogleCloudPlatform/cloud-modelarmor-team From 8ade246ad27c92105e7d3e804a01f830ce7abc45 Mon Sep 17 00:00:00 2001 From: harshnasitcrest <131268456+harshnasitcrest@users.noreply.github.com> Date: Thu, 17 Apr 2025 16:02:52 +0530 Subject: [PATCH 09/13] remove-require-env-location-condition --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 5a54b04ebb9..6759a6b671b 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -57,13 +57,11 @@ private static String requireEnvVar(String varName) { @BeforeClass public static void beforeAll() { requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("GOOGLE_CLOUD_PROJECT_LOCATION"); } @AfterClass public static void afterAll() throws IOException { requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("GOOGLE_CLOUD_PROJECT_LOCATION"); } @Before From dbc1b5efbb5739820e27957e72500e2d845a9b21 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Fri, 18 Apr 2025 13:05:55 +0530 Subject: [PATCH 10/13] address-review-comment --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index 6759a6b671b..f5bdcc1bb1e 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -96,7 +96,6 @@ public void testCreateModelArmorTemplate() throws IOException { Template createdTemplate = CreateTemplate.createTemplate(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Created template:"); assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); } @@ -105,7 +104,6 @@ public void testCreateModelArmorTemplateWithBasicSDP() throws IOException { Template createdTemplate = CreateTemplateWithBasicSdp.createTemplateWithBasicSdp(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Created template with basic SDP filter:"); assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); assertEquals(SdpBasicConfigEnforcement.ENABLED, createdTemplate.getFilterConfig().getSdpSettings().getBasicConfig().getFilterEnforcement()); @@ -116,7 +114,6 @@ public void testCreateModelArmorTemplateWithLabels() throws IOException { Template createdTemplate = CreateTemplateWithLabels.createTemplateWithLabels(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Created template with labels:"); assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); } @@ -125,7 +122,6 @@ public void testCreateModelArmorTemplateWithMetadata() throws IOException { Template createdTemplate = CreateTemplateWithMetadata.createTemplateWithMetadata(PROJECT_ID, LOCATION_ID, TEST_TEMPLATE_ID); - assertThat(stdOut.toString()).contains("Created template with metadata:"); assertEquals(createdTemplate.getName(), TEST_TEMPLATE_NAME); assertEquals(true, createdTemplate.getTemplateMetadata().getIgnorePartialInvocationFailures()); assertEquals(true, createdTemplate.getTemplateMetadata().getLogSanitizeOperations()); From 6ff142e1dd2bb67be52a0282deec09faaa29031a Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Wed, 7 May 2025 16:54:24 +0530 Subject: [PATCH 11/13] fixed-lint-post-merge-conflicts --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index b8bed5eb4bc..fa2f725cfe7 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -77,7 +77,8 @@ public class SnippetsIT { // Check if the required environment variables are set. private static String requireEnvVar(String varName) { String value = System.getenv(varName); - assertNotNull("Environment variable " + varName + " is required to run these tests.", + assertNotNull( + "Environment variable " + varName + " is required to run these tests.", System.getenv(varName)); return value; } From 5a0c3bd3c5702f31225374ca6728571335ede4e3 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Wed, 7 May 2025 16:58:07 +0530 Subject: [PATCH 12/13] fix-lints-post-merge-conflicts --- .../src/test/java/modelarmor/SnippetsIT.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index fa2f725cfe7..e2845cc26e0 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -94,8 +94,10 @@ public static void beforeAll() throws IOException { TEST_INSPECT_TEMPLATE_NAME = InspectTemplateName .ofProjectLocationInspectTemplateName(PROJECT_ID, LOCATION_ID, TEST_INSPECT_TEMPLATE_ID) .toString(); - TEST_DEIDENTIFY_TEMPLATE_NAME = DeidentifyTemplateName.ofProjectLocationDeidentifyTemplateName( - PROJECT_ID, LOCATION_ID, TEST_DEIDENTIFY_TEMPLATE_ID).toString(); + TEST_DEIDENTIFY_TEMPLATE_NAME = DeidentifyTemplateName + .ofProjectLocationDeidentifyTemplateName( + PROJECT_ID, LOCATION_ID, TEST_DEIDENTIFY_TEMPLATE_ID) + .toString(); createInspectTemplate(TEST_INSPECT_TEMPLATE_ID); createDeidentifyTemplate(TEST_DEIDENTIFY_TEMPLATE_ID); @@ -249,12 +251,12 @@ private static DeidentifyTemplate createDeidentifyTemplate(String templateId) th .setDeidentifyConfig(redactConfig) .build(); - CreateDeidentifyTemplateRequest createDeidentifyTemplateRequest = CreateDeidentifyTemplateRequest - .newBuilder() - .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) - .setTemplateId(templateId) - .setDeidentifyTemplate(deidentifyTemplate) - .build(); + CreateDeidentifyTemplateRequest createDeidentifyTemplateRequest = + CreateDeidentifyTemplateRequest.newBuilder() + .setParent(LocationName.of(PROJECT_ID, LOCATION_ID).toString()) + .setTemplateId(templateId) + .setDeidentifyTemplate(deidentifyTemplate) + .build(); return dlpServiceClient.createDeidentifyTemplate(createDeidentifyTemplateRequest); } From cbdfc1ce167926e6de10529228b903f2d443e835 Mon Sep 17 00:00:00 2001 From: Harsh Nasit Date: Wed, 7 May 2025 17:19:45 +0530 Subject: [PATCH 13/13] fix-tests-post-resolving-merge-conflicts --- modelarmor/src/test/java/modelarmor/SnippetsIT.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/modelarmor/src/test/java/modelarmor/SnippetsIT.java b/modelarmor/src/test/java/modelarmor/SnippetsIT.java index e2845cc26e0..3ba8ca22fe4 100644 --- a/modelarmor/src/test/java/modelarmor/SnippetsIT.java +++ b/modelarmor/src/test/java/modelarmor/SnippetsIT.java @@ -107,11 +107,6 @@ public static void beforeAll() throws IOException { public static void afterAll() throws IOException { requireEnvVar("GOOGLE_CLOUD_PROJECT"); - try { - deleteModelArmorTemplate(TEST_TEMPLATE_ID); - } catch (NotFoundException e) { - // Ignore not found error - template already deleted. - } deleteSdpTemplates(); } @@ -123,6 +118,12 @@ public void beforeEach() { @After public void afterEach() throws IOException { + try { + deleteModelArmorTemplate(TEST_TEMPLATE_ID); + } catch (NotFoundException e) { + // Ignore not found error - template already deleted. + } + stdOut = null; System.setOut(null); }