Skip to content

Commit d851777

Browse files
address-review-comments
1 parent 7dff76b commit d851777

File tree

6 files changed

+189
-236
lines changed

6 files changed

+189
-236
lines changed

modelarmor/src/main/java/modelarmor/CreateTemplate.java

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616

1717
package modelarmor;
1818

@@ -28,19 +28,12 @@
2828
import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter;
2929
import com.google.cloud.modelarmor.v1.RaiFilterType;
3030
import com.google.cloud.modelarmor.v1.Template;
31-
import com.google.protobuf.util.JsonFormat;
31+
import java.io.IOException;
3232
import java.util.List;
3333

34-
/** This class contains a main method that creates a template using the Model Armor API. */
3534
public class CreateTemplate {
3635

37-
/**
38-
* Main method that calls the createTemplate method to create a template.
39-
*
40-
* @param args command line arguments (not used)
41-
* @throws Exception if an error occurs during template creation
42-
*/
43-
public static void main(String[] args) throws Exception {
36+
public static void main(String[] args) throws IOException {
4437
// TODO(developer): Replace these variables before running the sample.
4538
String projectId = "your-project-id";
4639
String locationId = "your-location-id";
@@ -49,58 +42,62 @@ public static void main(String[] args) throws Exception {
4942
createTemplate(projectId, locationId, templateId);
5043
}
5144

52-
/**
53-
* Creates a template using the Model Armor API.
54-
*
55-
* @param projectId the ID of the project
56-
* @param locationId the ID of the location
57-
* @param templateId the ID of the template
58-
* @return the created template
59-
* @throws Exception if an error occurs during template creation
60-
*/
6145
public static Template createTemplate(String projectId, String locationId, String templateId)
62-
throws Exception {
63-
// Construct the API endpoint URL
46+
throws IOException {
47+
// Construct the API endpoint URL.
6448
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
49+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
50+
.build();
6551

66-
// Create a Model Armor settings object with the API endpoint
67-
ModelArmorSettings modelArmorSettings =
68-
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
69-
52+
// Initialize the client that will be used to send requests. This client
53+
// only needs to be created once, and can be reused for multiple requests.
7054
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
71-
// Construct the parent resource name
7255
String parent = LocationName.of(projectId, locationId).toString();
7356

74-
// Create a template object with a filter config
75-
Template template =
76-
Template.newBuilder()
77-
.setFilterConfig(
78-
FilterConfig.newBuilder()
79-
.setRaiSettings(
80-
RaiFilterSettings.newBuilder()
81-
.addAllRaiFilters(
82-
List.of(
83-
RaiFilter.newBuilder()
84-
.setFilterType(RaiFilterType.DANGEROUS)
85-
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
86-
.build()))
87-
.build())
88-
.build())
89-
.build();
57+
// Build the Model Armor template with your preferred filters.
58+
// For more details on filters, please refer to the following doc:
59+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
9060

91-
// Create a create template request object
92-
CreateTemplateRequest request =
93-
CreateTemplateRequest.newBuilder()
94-
.setParent(parent)
95-
.setTemplateId(templateId)
96-
.setTemplate(template)
61+
// Configure Responsible AI filter with multiple categories and their confidence
62+
// levels.
63+
RaiFilterSettings raiFilterSettings =
64+
RaiFilterSettings.newBuilder()
65+
.addAllRaiFilters(
66+
List.of(
67+
RaiFilter.newBuilder()
68+
.setFilterType(RaiFilterType.DANGEROUS)
69+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
70+
.build(),
71+
RaiFilter.newBuilder()
72+
.setFilterType(RaiFilterType.HATE_SPEECH)
73+
.setConfidenceLevel(DetectionConfidenceLevel.HIGH)
74+
.build(),
75+
RaiFilter.newBuilder()
76+
.setFilterType(RaiFilterType.SEXUALLY_EXPLICIT)
77+
.setConfidenceLevel(DetectionConfidenceLevel.LOW_AND_ABOVE)
78+
.build(),
79+
RaiFilter.newBuilder()
80+
.setFilterType(RaiFilterType.HARASSMENT)
81+
.setConfidenceLevel(DetectionConfidenceLevel.MEDIUM_AND_ABOVE)
82+
.build()))
9783
.build();
9884

99-
// Create the template using the Model Armor client
100-
Template createdTemplate = client.createTemplate(request);
85+
FilterConfig modelArmorFilter = FilterConfig.newBuilder()
86+
.setRaiSettings(raiFilterSettings)
87+
.build();
10188

102-
// Print the created template
103-
System.out.println("Created template: " + JsonFormat.printer().print(createdTemplate));
89+
Template template = Template.newBuilder()
90+
.setFilterConfig(modelArmorFilter)
91+
.build();
92+
93+
CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
94+
.setParent(parent)
95+
.setTemplateId(templateId)
96+
.setTemplate(template)
97+
.build();
98+
99+
Template createdTemplate = client.createTemplate(request);
100+
System.out.println("Created template: " + createdTemplate.getName());
104101

105102
return createdTemplate;
106103
}

modelarmor/src/main/java/modelarmor/DeleteTemplate.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616

1717
package modelarmor;
1818

@@ -21,17 +21,11 @@
2121
import com.google.cloud.modelarmor.v1.ModelArmorClient;
2222
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
2323
import com.google.cloud.modelarmor.v1.TemplateName;
24+
import java.io.IOException;
2425

25-
/** This class contains a main method that deletes a template using the Model Armor API. */
2626
public class DeleteTemplate {
2727

28-
/**
29-
* Main method that calls the deleteTemplate method to delete a template.
30-
*
31-
* @param args command line arguments (not used)
32-
* @throws Exception if an error occurs during template deletion
33-
*/
34-
public static void main(String[] args) throws Exception {
28+
public static void main(String[] args) throws IOException {
3529
// TODO(developer): Replace these variables before running the sample.
3630
String projectId = "your-project-id";
3731
String locationId = "your-location-id";
@@ -40,34 +34,24 @@ public static void main(String[] args) throws Exception {
4034
deleteTemplate(projectId, locationId, templateId);
4135
}
4236

43-
/**
44-
* Deletes a template using the Model Armor API.
45-
*
46-
* @param projectId the ID of the project
47-
* @param locationId the ID of the location
48-
* @param templateId the ID of the template
49-
* @throws Exception if an error occurs during template deletion
50-
*/
5137
public static void deleteTemplate(String projectId, String locationId, String templateId)
52-
throws Exception {
53-
// Construct the API endpoint URL
38+
throws IOException {
39+
// Construct the API endpoint URL.
5440
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
41+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
42+
.build();
5543

56-
// Create a Model Armor settings object with the API endpoint
57-
ModelArmorSettings modelArmorSettings =
58-
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
59-
44+
// Initialize the client that will be used to send requests. This client
45+
// only needs to be created once, and can be reused for multiple requests.
6046
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
61-
// Construct the template name
6247
String name = TemplateName.of(projectId, locationId, templateId).toString();
6348

64-
// Delete the template using the Model Armor client
49+
// Note: Ensure that the template you are deleting isn't used by any models.
6550
client.deleteTemplate(name);
66-
67-
// Print a success message
6851
System.out.println("Deleted template: " + name);
6952
}
7053
}
7154
}
7255

7356
// [END modelarmor_delete_template]
57+

modelarmor/src/main/java/modelarmor/GetTemplate.java

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,21 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616

1717
package modelarmor;
1818

19-
/**
20-
* This class demonstrates how to retrieve a template using the Model Armor API.
21-
*
22-
* @author [Your Name]
23-
*/
19+
// [START modelarmor_get_template]
20+
2421
import com.google.cloud.modelarmor.v1.ModelArmorClient;
2522
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
2623
import com.google.cloud.modelarmor.v1.Template;
2724
import com.google.cloud.modelarmor.v1.TemplateName;
28-
import com.google.protobuf.util.JsonFormat;
25+
import java.io.IOException;
2926

30-
// [START modelarmor_get_template]
31-
32-
/** This class contains a main method that retrieves a template using the Model Armor API. */
3327
public class GetTemplate {
3428

35-
/**
36-
* Main method that calls the getTemplate method to retrieve a template.
37-
*
38-
* @param args command line arguments (not used)
39-
* @throws Exception if an error occurs during template retrieval
40-
*/
41-
public static void main(String[] args) throws Exception {
29+
public static void main(String[] args) throws IOException {
4230
// TODO(developer): Replace these variables before running the sample.
4331
String projectId = "your-project-id";
4432
String locationId = "your-location-id";
@@ -47,32 +35,28 @@ public static void main(String[] args) throws Exception {
4735
getTemplate(projectId, locationId, templateId);
4836
}
4937

50-
/**
51-
* Retrieves a template using the Model Armor API.
52-
*
53-
* @param projectId the ID of the project
54-
* @param locationId the ID of the location
55-
* @param templateId the ID of the template
56-
* @throws Exception if an error occurs during template retrieval
57-
*/
58-
public static void getTemplate(String projectId, String locationId, String templateId)
59-
throws Exception {
60-
// Construct the API endpoint URL
38+
public static Template getTemplate(String projectId, String locationId, String templateId)
39+
throws IOException {
40+
// Construct the API endpoint URL.
6141
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
6242

63-
// Create a Model Armor settings object with the API endpoint
64-
ModelArmorSettings modelArmorSettings =
65-
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
43+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
44+
.build();
6645

46+
// Initialize the client that will be used to send requests. This client
47+
// only needs to be created once, and can be reused for multiple requests.
6748
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
68-
// Construct the template name
49+
// Build the template name.
6950
String name = TemplateName.of(projectId, locationId, templateId).toString();
7051

71-
// Retrieve the template using the Model Armor client
52+
// Get the template.
7253
Template template = client.getTemplate(name);
7354

74-
// Print the retrieved template
75-
System.out.println("Retrieved template: " + JsonFormat.printer().print(template));
55+
// Find more details about Template object here:
56+
// https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#Template
57+
System.out.printf("Retrieved template: %s\n", template.getName());
58+
59+
return template;
7660
}
7761
}
7862
}

modelarmor/src/main/java/modelarmor/ListTemplates.java

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,55 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616

1717
package modelarmor;
1818

19-
/**
20-
* This class demonstrates how to list templates using the Model Armor API.
21-
*
22-
* @author [Your Name]
23-
*/
24-
2519
// [START modelarmor_list_templates]
2620

2721
import com.google.cloud.modelarmor.v1.ListTemplatesRequest;
2822
import com.google.cloud.modelarmor.v1.LocationName;
2923
import com.google.cloud.modelarmor.v1.ModelArmorClient;
24+
import com.google.cloud.modelarmor.v1.ModelArmorClient.ListTemplatesPagedResponse;
3025
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
31-
import com.google.cloud.modelarmor.v1.Template;
32-
import com.google.protobuf.util.JsonFormat;
26+
import java.io.IOException;
3327

34-
/** This class contains a main method that lists templates using the Model Armor API. */
3528
public class ListTemplates {
3629

37-
/**
38-
* Main method that calls the listTemplates method to list templates.
39-
*
40-
* @param args command line arguments (not used)
41-
* @throws Exception if an error occurs during template listing
42-
*/
43-
public static void main(String[] args) throws Exception {
30+
public static void main(String[] args) throws IOException {
4431
// TODO(developer): Replace these variables before running the sample.
4532
String projectId = "your-project-id";
4633
String locationId = "your-location-id";
4734

4835
listTemplates(projectId, locationId);
4936
}
5037

51-
/**
52-
* Lists templates using the Model Armor API.
53-
*
54-
* @param projectId the ID of the project
55-
* @param locationId the ID of the location
56-
* @throws Exception if an error occurs during template listing
57-
*/
58-
public static void listTemplates(String projectId, String locationId) throws Exception {
59-
// Construct the API endpoint URL
38+
public static ListTemplatesPagedResponse listTemplates(String projectId, String locationId)
39+
throws IOException {
40+
// Construct the API endpoint URL.
6041
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
6142

62-
// Create a Model Armor settings object with the API endpoint
63-
ModelArmorSettings modelArmorSettings =
64-
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
43+
ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
44+
.build();
6545

46+
// Initialize the client that will be used to send requests. This client
47+
// only needs to be created once, and can be reused for multiple requests.
6648
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
67-
// Construct the parent resource name
49+
// Build the parent name.
6850
String parent = LocationName.of(projectId, locationId).toString();
6951

70-
// Create a list templates request object
71-
ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).build();
52+
ListTemplatesRequest request =
53+
ListTemplatesRequest.newBuilder()
54+
.setParent(parent)
55+
.build();
56+
57+
// List all templates.
58+
ListTemplatesPagedResponse pagedResponse = client.listTemplates(request);
59+
pagedResponse.iterateAll().forEach(template -> {
60+
System.out.printf("Template %s\n", template.getName());
61+
});
7262

73-
// List templates using the Model Armor client
74-
for (Template template : client.listTemplates(request).iterateAll()) {
75-
// Print each retrieved template
76-
System.out.println("Retrieved Templates: " + JsonFormat.printer().print(template));
77-
}
63+
return pagedResponse;
7864
}
7965
}
8066
}

0 commit comments

Comments
 (0)