|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package modelarmor; |
| 18 | + |
| 19 | +import com.google.cloud.modelarmor.v1.CreateTemplateRequest; |
| 20 | +import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel; |
| 21 | +import com.google.cloud.modelarmor.v1.FilterConfig; |
| 22 | +import com.google.cloud.modelarmor.v1.LocationName; |
| 23 | +import com.google.cloud.modelarmor.v1.ModelArmorClient; |
| 24 | +import com.google.cloud.modelarmor.v1.ModelArmorSettings; |
| 25 | +import com.google.cloud.modelarmor.v1.RaiFilterSettings; |
| 26 | +import com.google.cloud.modelarmor.v1.RaiFilterSettings.RaiFilter; |
| 27 | +import com.google.cloud.modelarmor.v1.RaiFilterType; |
| 28 | +import com.google.cloud.modelarmor.v1.Template; |
| 29 | +import com.google.protobuf.util.JsonFormat; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class CreateTemplate { |
| 33 | + |
| 34 | + public static void main(String[] args) throws Exception { |
| 35 | + // TODO(developer): Replace these variables before running the sample. |
| 36 | + String projectId = "your-project-id"; |
| 37 | + String locationId = "your-location-id"; |
| 38 | + String templateId = "your-template-id"; |
| 39 | + |
| 40 | + createTemplate(projectId, locationId, templateId); |
| 41 | + } |
| 42 | + |
| 43 | + public static Template createTemplate(String projectId, String locationId, String templateId) |
| 44 | + throws Exception { |
| 45 | + String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId); |
| 46 | + ModelArmorSettings modelArmorSettings = |
| 47 | + ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build(); |
| 48 | + |
| 49 | + try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) { |
| 50 | + String parent = LocationName.of(projectId, locationId).toString(); |
| 51 | + |
| 52 | + Template template = |
| 53 | + Template.newBuilder() |
| 54 | + .setFilterConfig( |
| 55 | + FilterConfig.newBuilder() |
| 56 | + .setRaiSettings( |
| 57 | + RaiFilterSettings.newBuilder() |
| 58 | + .addAllRaiFilters( |
| 59 | + List.of( |
| 60 | + RaiFilter.newBuilder() |
| 61 | + .setFilterType(RaiFilterType.DANGEROUS) |
| 62 | + .setConfidenceLevel(DetectionConfidenceLevel.HIGH) |
| 63 | + .build())) |
| 64 | + .build()) |
| 65 | + .build()) |
| 66 | + .build(); |
| 67 | + |
| 68 | + CreateTemplateRequest request = |
| 69 | + CreateTemplateRequest.newBuilder() |
| 70 | + .setParent(parent) |
| 71 | + .setTemplateId(templateId) |
| 72 | + .setTemplate(template) |
| 73 | + .build(); |
| 74 | + |
| 75 | + Template createdTemplate = client.createTemplate(request); |
| 76 | + System.out.println("Created template: " + JsonFormat.printer().print(createdTemplate)); |
| 77 | + return createdTemplate; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments