Skip to content

Commit fb7a0fa

Browse files
feat(ModelArmor): add samples for Model Armor service (GoogleCloudPlatform#2086)
1 parent 8538076 commit fb7a0fa

29 files changed

+2298
-0
lines changed

.github/blunderbuss.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ assign_issues_by:
2121
- 'api: parametermanager'
2222
to:
2323
- GoogleCloudPlatform/cloud-parameters-team
24+
- labels:
25+
- "api: modelarmor"
26+
to:
27+
- GoogleCloudPlatform/cloud-modelarmor-team
2428

2529
assign_prs_by:
2630
- labels:
@@ -41,3 +45,7 @@ assign_prs_by:
4145
- 'api: parametermanager'
4246
to:
4347
- GoogleCloudPlatform/cloud-parameters-team
48+
- labels:
49+
- "api: modelarmor"
50+
to:
51+
- GoogleCloudPlatform/cloud-modelarmor-team

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
/spanner/ @GoogleCloudPlatform/api-spanner @GoogleCloudPlatform/php-samples-reviewers
2727
/secretmanager/ @GoogleCloudPlatform/php-samples-reviewers @GoogleCloudPlatform/cloud-secrets-team
2828
/parametermanager/ @GoogleCloudPlatform/php-samples-reviewers @GoogleCloudPlatform/cloud-secrets-team @GoogleCloudPlatform/cloud-parameters-team
29+
/modelarmor/ @GoogleCloudPlatform/php-samples-reviewers @GoogleCloudPlatform/cloud-modelarmor-team
2930

3031
# Serverless, Orchestration, DevOps
3132

modelarmor/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"google/cloud-dlp": "^2.4",
4+
"google/cloud-modelarmor": "^0.1.0"
5+
}
6+
}

modelarmor/phpunit.xml.dist

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
18+
<phpunit bootstrap="../testing/bootstrap.php">
19+
<testsuites>
20+
<testsuite name="PHP Model Armor test">
21+
<directory>testdirectory>
22+
testsuite>
23+
testsuites>
24+
<logging>
25+
<log type="coverage-clover" target="build/logs/clover.xml"/>
26+
logging>
27+
<filter>
28+
<whitelist>
29+
<directory suffix=".php">./srcdirectory>
30+
<exclude>
31+
<directory>./vendordirectory>
32+
exclude>
33+
whitelist>
34+
filter>
35+
<php>
36+
<env name="PHPUNIT_TESTS" value="1"/>
37+
php>
38+
phpunit>

modelarmor/src/create_template.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
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]
23+
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
24+
use Google\Cloud\ModelArmor\V1\Template;
25+
use Google\Cloud\ModelArmor\V1\CreateTemplateRequest;
26+
use Google\Cloud\ModelArmor\V1\FilterConfig;
27+
use Google\Cloud\ModelArmor\V1\RaiFilterType;
28+
use Google\Cloud\ModelArmor\V1\RaiFilterSettings;
29+
use Google\Cloud\ModelArmor\V1\RaiFilterSettings\RaiFilter;
30+
use Google\Cloud\ModelArmor\V1\DetectionConfidenceLevel;
31+
32+
/**
33+
* Create a Model Armor template.
34+
*
35+
* @param string $projectId The ID of the project (e.g. 'my-project').
36+
* @param string $locationId The ID of the location (e.g. 'us-central1').
37+
* @param string $templateId The ID of the template (e.g. 'my-template').
38+
*/
39+
function create_template(string $projectId, string $locationId, string $templateId): void
40+
{
41+
$options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];
42+
$client = new ModelArmorClient($options);
43+
$parent = $client->locationName($projectId, $locationId);
44+
45+
/**
46+
* Build the Model Armor template with preferred filters.
47+
* For more details on filters, refer to:
48+
* https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
49+
*/
50+
51+
$raiFilters = [
52+
(new RaiFilter())
53+
->setFilterType(RaiFilterType::DANGEROUS)
54+
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
55+
(new RaiFilter())
56+
->setFilterType(RaiFilterType::HATE_SPEECH)
57+
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
58+
(new RaiFilter())
59+
->setFilterType(RaiFilterType::SEXUALLY_EXPLICIT)
60+
->setConfidenceLevel(DetectionConfidenceLevel::LOW_AND_ABOVE),
61+
(new RaiFilter())
62+
->setFilterType(RaiFilterType::HARASSMENT)
63+
->setConfidenceLevel(DetectionConfidenceLevel::MEDIUM_AND_ABOVE),
64+
];
65+
66+
$raiFilterSetting = (new RaiFilterSettings())->setRaiFilters($raiFilters);
67+
68+
$templateFilterConfig = (new FilterConfig())->setRaiSettings($raiFilterSetting);
69+
70+
$template = (new Template())->setFilterConfig($templateFilterConfig);
71+
72+
$request = (new CreateTemplateRequest)
73+
->setParent($parent)
74+
->setTemplateId($templateId)
75+
->setTemplate($template);
76+
77+
$response = $client->createTemplate($request);
78+
79+
printf('Template created: %s' . PHP_EOL, $response->getName());
80+
}
81+
// [END modelarmor_create_template]
82+
83+
// The following 2 lines are only needed to execute the samples on the CLI.
84+
require_once __DIR__ . '/../../testing/sample_helpers.php';
85+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
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);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
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_basic_sdp]
23+
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
24+
use Google\Cloud\ModelArmor\V1\SdpBasicConfig\SdpBasicConfigEnforcement;
25+
use Google\Cloud\ModelArmor\V1\SdpBasicConfig;
26+
use Google\Cloud\ModelArmor\V1\SdpFilterSettings;
27+
use Google\Cloud\ModelArmor\V1\FilterConfig;
28+
use Google\Cloud\ModelArmor\V1\CreateTemplateRequest;
29+
use Google\Cloud\ModelArmor\V1\Template;
30+
31+
/**
32+
* Create a Model Armor template with Basic SDP Filter.
33+
*
34+
* @param string $projectId The ID of the project (e.g. 'my-project').
35+
* @param string $locationId The ID of the location (e.g. 'us-central1').
36+
* @param string $templateId The ID of the template (e.g. 'my-template').
37+
*/
38+
function create_template_with_basic_sdp(string $projectId, string $locationId, string $templateId): void
39+
{
40+
$options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];
41+
$client = new ModelArmorClient($options);
42+
$parent = $client->locationName($projectId, $locationId);
43+
44+
// Build the Model Armor template with your preferred filters.
45+
// For more details on filters, please refer to the following doc:
46+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
47+
48+
// Configure Basic SDP Filter.
49+
$sdpBasicConfig = (new SdpBasicConfig())->setFilterEnforcement(SdpBasicConfigEnforcement::ENABLED);
50+
$sdpSettings = (new SdpFilterSettings())->setBasicConfig($sdpBasicConfig);
51+
52+
$templateFilterConfig = (new FilterConfig())
53+
->setSdpSettings($sdpSettings);
54+
55+
$template = (new Template())->setFilterConfig($templateFilterConfig);
56+
57+
$request = (new CreateTemplateRequest())
58+
->setParent($parent)
59+
->setTemplateId($templateId)
60+
->setTemplate($template);
61+
62+
$response = $client->createTemplate($request);
63+
64+
printf('Template created: %s' . PHP_EOL, $response->getName());
65+
}
66+
// [END modelarmor_create_template_with_basic_sdp]
67+
68+
// The following 2 lines are only needed to execute the samples on the CLI.
69+
require_once __DIR__ . '/../../testing/sample_helpers.php';
70+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)