Skip to content

deprecated #2095

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

Closed
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-modelarmor": "^0.1.0",
"google/cloud-dlp": "^2.4"
},
"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>
54 changes: 54 additions & 0 deletions modelarmor/src/sanitize_model_response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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) != 5) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID TEMPLATE_ID MODEL_RESPONSE\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId, $modelResponse) = $argv;

// [START modelarmor_sanitize_model_response]
// Import the ModelArmor client library.
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\SanitizeModelResponseRequest;
use Google\Cloud\ModelArmor\V1\DataItem;

/** 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';
// $modelResponse = 'YOUR_MODEL_RESPONSE'; // e.g. 'my-model-response';

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

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

$modelResponseRequest = (new SanitizeModelResponseRequest())
->setName("projects/$projectId/locations/$locationId/templates/$templateId")
->setModelResponseData((new DataItem())->setText($modelResponse));

$response = $client->sanitizeModelResponse($modelResponseRequest);

printf('Result for Model Response Sanitization: %s' . PHP_EOL, $response->serializeToJsonString());
// [END modelarmor_sanitize_model_response]
56 changes: 56 additions & 0 deletions modelarmor/src/sanitize_model_response_with_user_prompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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 MODEL_RESPONSE USER_PROMPT\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId, $modelResponse, $userPrompt) = $argv;

// [START modelarmor_sanitize_model_response_with_user_prompt]
// Import the ModelArmor client library.
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\SanitizeModelResponseRequest;
use Google\Cloud\ModelArmor\V1\DataItem;

/** 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';
// $modelResponse = 'YOUR_MODEL_RESPONSE'; // e.g. 'my-model-response';
// $userPrompt = 'YOUR_USER_PROMPT'; // e.g. 'my-user-prompt';

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

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

$modelResponseRequest = (new SanitizeModelResponseRequest())
->setName("projects/$projectId/locations/$locationId/templates/$templateId")
->setModelResponseData((new DataItem())->setText($modelResponse))
->setUserPrompt($userPrompt);

$response = $client->sanitizeModelResponse($modelResponseRequest);

printf('Result for Model Response Sanitization with User Prompt: %s' . PHP_EOL, $response->serializeToJsonString());
// [END modelarmor_sanitize_model_response_with_user_prompt]
54 changes: 54 additions & 0 deletions modelarmor/src/sanitize_user_prompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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) != 5) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID TEMPLATE_ID USER_PROMPT\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId, $userPrompt) = $argv;

// [START modelarmor_sanitize_user_prompt]
// Import the ModelArmor client library.
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\SanitizeUserPromptRequest;
use Google\Cloud\ModelArmor\V1\DataItem;

/** 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';
// $userPrompt = 'YOUR_USER_PROMPT'; // e.g. 'my-user-prompt';

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

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

$userPromptRequest = (new SanitizeUserPromptRequest())
->setName("projects/$projectId/locations/$locationId/templates/$templateId")
->setUserPromptData((new DataItem())->setText($userPrompt));

$response = $client->sanitizeUserPrompt($userPromptRequest);

printf('Result for Sanitize User Prompt: %s' . PHP_EOL, $response->serializeToJsonString());
// [END modelarmor_sanitize_user_prompt]
63 changes: 63 additions & 0 deletions modelarmor/src/screen_pdf_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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) != 5) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID TEMPLATE_ID PDF_FILE_NAME\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId, $filePath) = $argv;

// [START modelarmor_screen_pdf_file]
// Import the ModelArmor client library.
use Google\Cloud\ModelArmor\V1\CLient\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\SanitizeUserPromptRequest;
use Google\Cloud\ModelArmor\V1\ByteDataItem;
use Google\Cloud\ModelArmor\V1\ByteDataItem\ByteItemType;
use Google\Cloud\ModelArmor\V1\DataItem;

/** 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';
// $userPrompt = 'YOUR_USER_PROMPT'; // e.g. 'my-user-prompt';
// $filePath = 'YOUR_PDF_FILE_NAME'; // e.g. ''path/to/file.pdf';

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

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

// Read the file content and encode it in base64.
$pdfContent = file_get_contents($filePath);
$pdfContentBase64 = base64_encode($pdfContent);

$userPromptRequest = (new SanitizeUserPromptRequest())
->setName("projects/$projectId/locations/$locationId/templates/$templateId")
->setUserPromptData((new DataItem())
->setByteItem(new ByteDataItem()->setByteData($pdfContentBase64)
->setByteDataType(ByteItemType::PDF)));

$response = $client->sanitizeUserPrompt($userPromptRequest);

printf('Result for Screen PDF File: %s' . PHP_EOL, $response->serializeToJsonString());
// [END modelarmor_screen_pdf_file]
Loading