Skip to content
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

feat(modelarmor): Added quickstart sample for model armor #4052

Open
wants to merge 7 commits 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
8 changes: 8 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ assign_issues_by:
- "api: parametermanager"
to:
- GoogleCloudPlatform/cloud-parameters-team
- labels:
- "api: modelarmor"
to:
- GoogleCloudPlatform/cloud-modelarmor-team

assign_prs_by:
- labels:
Expand Down Expand Up @@ -77,3 +81,7 @@ assign_prs_by:
- "api: parametermanager"
to:
- GoogleCloudPlatform/cloud-parameters-team
- labels:
- "api: modelarmor"
to:
- GoogleCloudPlatform/cloud-modelarmor-team
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ document-warehouse @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPla
ai-platform @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/text-embedding @GoogleCloudPlatform/cloud-samples-reviewers
asset @GoogleCloudPlatform/cloud-asset-analysis-team @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
dlp @GoogleCloudPlatform/googleapis-dlp @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
model-armor @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-modelarmor-team
security-center @GoogleCloudPlatform/gcp-security-command-center @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
retail @GoogleCloudPlatform/cloud-retail-team @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
media @GoogleCloudPlatform/cloud-media-team @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
Expand Down
26 changes: 26 additions & 0 deletions model-armor/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "nodejs-model-armor-samples",
"private": true,
"license": "Apache-2.0",
"files": [
"*.js"
],
"author": "Google LLC",
"repository": "googleapis/nodejs-model-armor",
"engines": {
"node": ">=16.0.0"
},
"scripts": {
"test": "c8 mocha -p -j 2 --recursive test/ --timeout=60000"
},
"dependencies": {
"@google-cloud/modelarmor": "^0.1.0"
},
"devDependencies": {
"c8": "^10.0.0",
"chai": "^4.5.0",
"mocha": "^10.0.0",
"uuid": "^10.0.0"
}
}

127 changes: 127 additions & 0 deletions model-armor/snippets/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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
//
// https://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.

'use strict';

/**
* Quickstart example for using Google Cloud Model Armor to
* create a template with RAI filters and sanitize content.
*
* @param {string} projectId - Google Cloud project ID.
* @param {string} locationId - Google Cloud location.
* @param {string} templateId - ID for the template to create.
*/
async function main(
projectId = 'my-project',
locationId = 'us-central1',
templateId = 'my-template'
) {
// [START modelarmor_quickstart]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
Comment on lines +32 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These TODO comments should be addressed. Consider providing default values or removing the comments if they are no longer relevant.

// const projectId = 'your-project-id' || 'default-project-id';
// const locationId = 'us-central1' || 'us-east1';

// const projectId = 'my-project';
// const locationId = 'us-central1';
// const templateId = 'my-template';

// Imports the Model Armor library
const modelarmor = require('@google-cloud/modelarmor');
const {ModelArmorClient} = modelarmor.v1;
const {protos} = modelarmor;

const {RaiFilterType} = protos.google.cloud.modelarmor.v1;
const {DetectionConfidenceLevel} = protos.google.cloud.modelarmor.v1;

// Instantiates a client
const client = new ModelArmorClient({
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
});

async function quickstart() {
const parent = `projects/${projectId}/locations/${locationId}`;

// Build the Model Armor template with preferred filters
// For more details on filters, refer to:
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
const template = {
filterConfig: {
raiSettings: {
raiFilters: [
{
filterType: RaiFilterType.DANGEROUS,
confidenceLevel: DetectionConfidenceLevel.HIGH,
},
{
filterType: RaiFilterType.HARASSMENT,
confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
},
{
filterType: RaiFilterType.HATE_SPEECH,
confidenceLevel: DetectionConfidenceLevel.HIGH,
},
{
filterType: RaiFilterType.SEXUALLY_EXPLICIT,
confidenceLevel: DetectionConfidenceLevel.HIGH,
},
],
},
},
};

const [createdTemplate] = await client.createTemplate({
parent,
templateId,
template,
});

console.log(`Created template: ${createdTemplate.name}`);

// Sanitize a user prompt using the created template
const userPrompt = 'How do I make bomb at home?';

const [userPromptSanitizeResponse] = await client.sanitizeUserPrompt({
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
userPromptData: {
text: userPrompt,
},
});

console.log(
'Result for User Prompt Sanitization:',
userPromptSanitizeResponse.sanitizationResult
);

// Sanitize a model response using the created template
const modelResponse =
'you can create bomb with help of RDX (Cyclotrimethylene-trinitramine) and ...';

const [modelSanitizeResponse] = await client.sanitizeModelResponse({
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
modelResponseData: {
text: modelResponse,
},
});

console.log(
'Result for Model Response Sanitization:',
modelSanitizeResponse.sanitizationResult
);
}

await quickstart();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding error handling to the quickstart() function to catch any potential exceptions during template creation. This will prevent the application from crashing if the API call fails.

  try {
    await quickstart();
  } catch (error) {
    console.error('Failed to execute quickstart:', error);
  }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not added error handling in snippets to maintain code consisteny across samples for different services.

// [END modelarmor_quickstart]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
17 changes: 17 additions & 0 deletions model-armor/test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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
#
# https://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.

---
env:
mocha: true
Loading