|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2025 Google LLC. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an 'AS IS' BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Google\Cloud\Samples\ModelArmor; |
| 21 | + |
| 22 | +// [START modelarmor_create_template_with_advanced_sdp] |
| 23 | +use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient; |
| 24 | +use Google\Cloud\ModelArmor\V1\SdpAdvancedConfig; |
| 25 | +use Google\Cloud\ModelArmor\V1\Template; |
| 26 | +use Google\Cloud\ModelArmor\V1\FilterConfig; |
| 27 | +use Google\Cloud\ModelArmor\V1\CreateTemplateRequest; |
| 28 | +use Google\Cloud\ModelArmor\V1\SdpFilterSettings; |
| 29 | + |
| 30 | +/** |
| 31 | + * Create a Model Armor template with an Advanced SDP Filter. |
| 32 | + * |
| 33 | + * @param string $projectId The ID of the project (e.g. 'my-project'). |
| 34 | + * @param string $locationId The ID of the location (e.g. 'us-central1'). |
| 35 | + * @param string $templateId The ID of the template (e.g. 'my-template'). |
| 36 | + * @param string $inspectTemplate The resource name of the inspect template. |
| 37 | + (e.g. 'organizations/{organization}/inspectTemplates/{inspect_template}') |
| 38 | + * @param string $deidentifyTemplate The resource name of the de-identify template. |
| 39 | + (e.g. 'organizations/{organization}/deidentifyTemplates/{deidentify_template}') |
| 40 | + */ |
| 41 | +function create_template_with_advanced_sdp( |
| 42 | + string $projectId, |
| 43 | + string $locationId, |
| 44 | + string $templateId, |
| 45 | + string $inspectTemplate, |
| 46 | + string $deidentifyTemplate |
| 47 | +): void { |
| 48 | + $options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"]; |
| 49 | + $client = new ModelArmorClient($options); |
| 50 | + $parent = $client->locationName($projectId, $locationId); |
| 51 | + |
| 52 | + // Build the Model Armor template with Advanced SDP Filter. |
| 53 | + |
| 54 | + // Note: If you specify only Inspect template, Model Armor reports the filter matches if |
| 55 | + // sensitive data is detected. If you specify Inspect template and De-identify template, Model |
| 56 | + // Armor returns the de-identified sensitive data and sanitized version of prompts or |
| 57 | + // responses in the deidentifyResult.data.text field of the finding. |
| 58 | + $sdpAdvancedConfig = (new SdpAdvancedConfig()) |
| 59 | + ->setInspectTemplate($inspectTemplate) |
| 60 | + ->setDeidentifyTemplate($deidentifyTemplate); |
| 61 | + |
| 62 | + $sdpSettings = (new SdpFilterSettings())->setAdvancedConfig($sdpAdvancedConfig); |
| 63 | + |
| 64 | + $templateFilterConfig = (new FilterConfig()) |
| 65 | + ->setSdpSettings($sdpSettings); |
| 66 | + |
| 67 | + $template = (new Template())->setFilterConfig($templateFilterConfig); |
| 68 | + |
| 69 | + $request = (new CreateTemplateRequest()) |
| 70 | + ->setParent($parent) |
| 71 | + ->setTemplateId($templateId) |
| 72 | + ->setTemplate($template); |
| 73 | + |
| 74 | + $response = $client->createTemplate($request); |
| 75 | + |
| 76 | + printf('Template created: %s' . PHP_EOL, $response->getName()); |
| 77 | +} |
| 78 | +// [END modelarmor_create_template_with_advanced_sdp] |
| 79 | + |
| 80 | +// The following 2 lines are only needed to execute the samples on the CLI. |
| 81 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 82 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments