Skip to content
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
10 changes: 10 additions & 0 deletions modelarmor/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"require": {
"google/cloud-modelarmor": "^0.1.0"
},
"autoload": {
"psr-4": {
"Google\\Cloud\\Samples\\ModelArmor\\": "test"
}
}
}
38 changes: 38 additions & 0 deletions modelarmor/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 Secret Manager 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>
95 changes: 95 additions & 0 deletions modelarmor/src/create_template_with_labels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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) != 6) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID TEMPLATE_ID LABEL_KEY LABEL_VALUE\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId, $labelKey, $labelValue) = $argv;

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

/** 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');
// $labelKey = 'YOUR_LABEL_KEY'; (e.g. 'my-label-key');
// $labelValue = 'YOUR_LABEL_VALUE'; (e.g. 'my-label-value');

// 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);

// Create RAI filters.
$raiFilters = [
(new RaiFilter())
->setFilterType(RaiFilterType::DANGEROUS)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
(new RaiFilter())
->setFilterType(RaiFilterType::HATE_SPEECH)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
(new RaiFilter())
->setFilterType(RaiFilterType::SEXUALLY_EXPLICIT)
->setConfidenceLevel(DetectionConfidenceLevel::LOW_AND_ABOVE),
(new RaiFilter())
->setFilterType(RaiFilterType::HARASSMENT)
->setConfidenceLevel(DetectionConfidenceLevel::MEDIUM_AND_ABOVE),
];

$raiSettings = (new RaiFilterSettings())->setRaiFilters($raiFilters);
$filterConfig = (new FilterConfig())->setRaiSettings($raiSettings);

// Build template with filters and labels.
$template = (new Template())
->setFilterConfig($filterConfig)
->setLabels([$labelKey => $labelValue]);

// Construct the request with template configuration and labels.
$request = (new CreateTemplateRequest())
->setParent($parent)
->setTemplateId($templateId)
->setTemplate($template);

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

printf('Template created: %s' . PHP_EOL, $response->getName());

$labelsArray = [];
foreach ($template->getLabels() as $key => $value) {
$labelsArray[$key] = $value;
}
printf('Labels: %s' . PHP_EOL, json_encode($labelsArray));
// [END modelarmor_create_template_with_labels]
96 changes: 96 additions & 0 deletions modelarmor/src/create_template_with_metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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_metadata]
// Import the ModelArmor client library.
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\RaiFilterType;
use Google\Cloud\ModelArmor\V1\Template;
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\DetectionConfidenceLevel;
use Google\Cloud\ModelArmor\V1\Template\TemplateMetadata;

/** 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');

// 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);

/** Add template metadata to the template.
* For more details on template metadata, please refer to the following doc:
* https://cloud.google.com/security-command-center/docs/reference/model-armor/rest/v1/projects.locations.templates#templatemetadata
*/

$raiFilters = [
(new RaiFilter())
->setFilterType(RaiFilterType::DANGEROUS)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
(new RaiFilter())
->setFilterType(RaiFilterType::HATE_SPEECH)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
(new RaiFilter())
->setFilterType(RaiFilterType::SEXUALLY_EXPLICIT)
->setConfidenceLevel(DetectionConfidenceLevel::LOW_AND_ABOVE),
(new RaiFilter())
->setFilterType(RaiFilterType::HARASSMENT)
->setConfidenceLevel(DetectionConfidenceLevel::MEDIUM_AND_ABOVE),
];

$raiSettings = (new RaiFilterSettings())->setRaiFilters($raiFilters);
$filterConfig = (new FilterConfig())->setRaiSettings($raiSettings);

$templateMetadata = (new TemplateMetadata())
->setIgnorePartialInvocationFailures(true)
->setLogSanitizeOperations(true)
->setCustomPromptSafetyErrorCode(500);

// Build template with filters and Metadata.
$template = (new Template())
->setFilterConfig($filterConfig)
->setTemplateMetadata($templateMetadata);

$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_metadata]
75 changes: 75 additions & 0 deletions modelarmor/test/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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;
}
}
47 changes: 47 additions & 0 deletions modelarmor/test/createTemplateWithLabelsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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;

class createTemplateWithLabelsTest extends BaseTestCase
{
protected static function getTemplatePrefix(): string
{
return 'php-template-labels-';
}

public function testCreateTemplateWithLabels()
{
$labelKey = 'environment';
$labelValue = 'test';

$projectId = self::getProjectId();
$output = $this->runSnippetfile('create_template_with_labels', [
$projectId,
self::$locationId,
self::$templateId,
$labelKey,
$labelValue,
]);

$expectedTemplateString = 'Template created: projects/' . $projectId . '/locations/' . self::$locationId . '/templates/' . self::$templateId;
$this->assertStringContainsString($expectedTemplateString, $output);
$this->assertStringContainsString('Labels: {"environment":"test"}', $output);
}
}
Loading