diff --git a/modelarmor/composer.json b/modelarmor/composer.json new file mode 100644 index 0000000000..037778bf23 --- /dev/null +++ b/modelarmor/composer.json @@ -0,0 +1,10 @@ +{ + "require": { + "google/cloud-modelarmor": "^0.1.0" + }, + "autoload": { + "psr-4": { + "Google\\Cloud\\Samples\\ModelArmor\\": "test" + } + } +} diff --git a/modelarmor/phpunit.xml.dist b/modelarmor/phpunit.xml.dist new file mode 100644 index 0000000000..8e2959e6a6 --- /dev/null +++ b/modelarmor/phpunit.xml.dist @@ -0,0 +1,38 @@ + + + + + + + test + + + + + + + + ./src + + ./vendor + + + + + + + diff --git a/modelarmor/src/create_template_with_labels.php b/modelarmor/src/create_template_with_labels.php new file mode 100644 index 0000000000..1285e2d288 --- /dev/null +++ b/modelarmor/src/create_template_with_labels.php @@ -0,0 +1,95 @@ + "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] diff --git a/modelarmor/src/create_template_with_metadata.php b/modelarmor/src/create_template_with_metadata.php new file mode 100644 index 0000000000..2df4e85349 --- /dev/null +++ b/modelarmor/src/create_template_with_metadata.php @@ -0,0 +1,96 @@ + "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] diff --git a/modelarmor/test/BaseTestCase.php b/modelarmor/test/BaseTestCase.php new file mode 100644 index 0000000000..2fd406b058 --- /dev/null +++ b/modelarmor/test/BaseTestCase.php @@ -0,0 +1,75 @@ + '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; + } +} diff --git a/modelarmor/test/createTemplateWithLabelsTest.php b/modelarmor/test/createTemplateWithLabelsTest.php new file mode 100644 index 0000000000..12f89c958d --- /dev/null +++ b/modelarmor/test/createTemplateWithLabelsTest.php @@ -0,0 +1,47 @@ +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); + } +} diff --git a/modelarmor/test/createTemplateWithMetadataTest.php b/modelarmor/test/createTemplateWithMetadataTest.php new file mode 100644 index 0000000000..dde45ac3f8 --- /dev/null +++ b/modelarmor/test/createTemplateWithMetadataTest.php @@ -0,0 +1,41 @@ +runSnippetfile('create_template_with_metadata', [ + $projectId, + self::$locationId, + self::$templateId, + ]); + + $expectedTemplateString = 'Template created: projects/' . $projectId . '/locations/' . self::$locationId . '/templates/' . self::$templateId; + $this->assertStringContainsString($expectedTemplateString, $output); + } +}