Skip to content

feat(modelarmor): Added snippets for create template with basic and advanced SDP #2088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions modelarmor/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"require": {
"google/cloud-dlp": "^2.4",
"google/cloud-modelarmor": "^0.1.0"
},
"autoload": {
"psr-4": {
"Google\\Cloud\\Samples\\ModelArmor\\": "test"
}
}
}
35 changes: 35 additions & 0 deletions modelarmor/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<phpunit bootstrap="../testing/bootstrap.php">
<testsuites>
<testsuite name="PHP Model Armor test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
</php>
</phpunit>
84 changes: 84 additions & 0 deletions modelarmor/src/create_template_with_advanced_sdp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/*
* 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.
*/

declare(strict_types=1);

namespace Google\Cloud\Samples\ModelArmor;

require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) < 4 || count($argv) > 6) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID TEMPLATE_ID [INSPECT_TEMPLATE] [DEIDENTIFY_TEMPLATE]\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId) = $argv;
$inspectTemplate = $argv[4] ?? '';
$deidentifyTemplate = $argv[5] ?? '';

// [START modelarmor_create_template_with_advanced_sdp]
// Import the Model Armor client library.
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\SdpAdvancedConfig;
use Google\Cloud\ModelArmor\V1\Template;
use Google\Cloud\ModelArmor\V1\FilterConfig;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings\RaiFilter;
use Google\Cloud\ModelArmor\V1\CreateTemplateRequest;
use Google\Cloud\ModelArmor\V1\RaiFilterType;
use Google\Cloud\ModelArmor\V1\DetectionConfidenceLevel;
use Google\Cloud\ModelArmor\V1\SdpFilterSettings;

/** Uncomment and populate these variables in your code. */
// $projectId = "YOUR_GOOGLE_CLOUD_PROJECT"; // e.g. 'my-project';
// $locationId = 'YOUR_LOCATION_ID'; // e.g. 'my-location';
// $templateId = 'YOUR_TEMPLATE_ID'; // e.g. 'my-template';
// $inspectTemplate = 'YOUR_INSPECT_TEMPLATE'; // e.g. 'organizations/{organization}/inspectTemplates/{inspect_template}'
// $deidentifyTemplate = 'YOUR_DEIDENTIFY_TEMPLATE'; // e.g. 'organizations/{organization}/deidentifyTemplates/{deidentify_template}'

// Specify regional endpoint.
$options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];

$client = new ModelArmorClient($options);

// Build the resource name of the parent location.
$parent = $client->locationName($projectId, $locationId);

// Build the Model Armor template with Advanced SDP Filter.

// Note: If you specify only Inspect template, Model Armor reports the filter matches if
// sensitive data is detected. If you specify Inspect template and De-identify template, Model
// Armor returns the de-identified sensitive data and sanitized version of prompts or
// responses in the deidentifyResult.data.text field of the finding.
$sdpAdvancedConfig = (new SdpAdvancedConfig())
->setInspectTemplate($inspectTemplate)
->setDeidentifyTemplate($deidentifyTemplate);

$sdpSettings = (new SdpFilterSettings())->setAdvancedConfig($sdpAdvancedConfig);

$templateFilterConfig = (new FilterConfig())
->setSdpSettings($sdpSettings);

$template = (new Template())->setFilterConfig($templateFilterConfig);

$request = (new CreateTemplateRequest())
->setParent($parent)
->setTemplateId($templateId)
->setTemplate($template);

$response = $client->createTemplate($request);

printf('Template created: %s' . PHP_EOL, $response->getName());
// [END modelarmor_create_template_with_advanced_sdp]
78 changes: 78 additions & 0 deletions modelarmor/src/create_template_with_basic_sdp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/*
* 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.
*/

declare(strict_types=1);

namespace Google\Cloud\Samples\ModelArmor;

require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) != 4) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID TEMPLATE_ID\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId) = $argv;

// [START modelarmor_create_template_with_basic_sdp]
// Import the Model Armor client library.
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\RaiFilterType;
use Google\Cloud\ModelArmor\V1\DetectionConfidenceLevel;
use Google\Cloud\ModelArmor\V1\SdpBasicConfig\SdpBasicConfigEnforcement;
use Google\Cloud\ModelArmor\V1\SdpBasicConfig;
use Google\Cloud\ModelArmor\V1\SdpFilterSettings;
use Google\Cloud\ModelArmor\V1\FilterConfig;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings;
use Google\Cloud\ModelArmor\V1\CreateTemplateRequest;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings\RaiFilter;
use Google\Cloud\ModelArmor\V1\Template;

/** Uncomment and populate these variables in your code. */
// $projectId = "YOUR_GOOGLE_CLOUD_PROJECT"; // e.g. 'my-project';
// $locationId = 'YOUR_LOCATION_ID'; // e.g. 'us-central1';
// $templateId = 'YOUR_TEMPLATE_ID'; // e.g. 'my-template';

// Specify regional endpoint.
$options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];

// Instantiates a client.
$client = new ModelArmorClient($options);

// Build the resource name of the parent location.
$parent = $client->locationName($projectId, $locationId);

// 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 = (new SdpBasicConfig())->setFilterEnforcement(SdpBasicConfigEnforcement::ENABLED);
$sdpSettings = (new SdpFilterSettings())->setBasicConfig($sdpBasicConfig);

$templateFilterConfig = (new FilterConfig())
->setSdpSettings($sdpSettings);

$template = (new Template())->setFilterConfig($templateFilterConfig);

$request = (new CreateTemplateRequest())
->setParent($parent)
->setTemplateId($templateId)
->setTemplate($template);

$response = $client->createTemplate($request);

printf('Template created: %s' . PHP_EOL, $response->getName());
// [END modelarmor_create_template_with_basic_sdp]
73 changes: 73 additions & 0 deletions modelarmor/test/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/*
* 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.
*/

declare(strict_types=1);

namespace Google\Cloud\Samples\ModelArmor;

require_once __DIR__ . '/../vendor/autoload.php';

use Google\ApiCore\ApiException as GaxApiException;
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\DeleteTemplateRequest;
use Google\Cloud\TestUtils\TestTrait;
use PHPUnit\Framework\TestCase;

abstract class BaseTestCase extends TestCase
{
use TestTrait;

protected static $client;
protected static $templateId;
protected static $locationId = 'us-central1';

public static function setUpBeforeClass(): void
{
$options = ['apiEndpoint' => 'modelarmor.' . self::$locationId . '.rep.googleapis.com'];
self::$client = new ModelArmorClient($options);
self::$templateId = static::getTemplatePrefix() . uniqid();
}

public static function tearDownAfterClass(): void
{
$templateName = self::$client->templateName(self::$projectId, self::$locationId, self::$templateId);
try {
static::customTeardown();
$request = (new DeleteTemplateRequest())->setName($templateName);
self::$client->deleteTemplate($request);
} catch (GaxApiException $e) {
if ($e->getStatus() != 'NOT_FOUND') {
throw $e;
}
}
self::$client->close();
}

abstract protected static function getTemplatePrefix(): string;
protected static function customTeardown(): void {}

protected function runSnippetfile(string $snippetName, array $params = []): string
{
$output = $this->runSnippet($snippetName, $params);
return $output;
}

protected static function getProjectId()
{
return self::$projectId;
}
}
Loading