-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(modelarmor): Added snippet for quickstart #4052
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
rudrakhsha-crest
wants to merge
10
commits into
GoogleCloudPlatform:main
Choose a base branch
from
rudrakhsha-crest:model-armor-snippet-quickstart
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
874955b
Added CRUD code snippets with codeowners file
rudrakhsha-crest 8270ed7
Solved linting errors
rudrakhsha-crest 3824b65
Added header comment
rudrakhsha-crest c78e8be
Added quickstart code snippet for model armor
rudrakhsha-crest 9a20c63
Removed crud related files
rudrakhsha-crest 2df6b5f
Updated prompts in the quickstart template along with test cases
rudrakhsha-crest 4c623bd
Removed extra functions from quickstart code snippets
rudrakhsha-crest 36329ed
Merge branch 'main' into model-armor-snippet-quickstart
rudrakhsha-crest cb43a3e
Updated according to latest design in the other PRs after addressing …
rudrakhsha-crest dd5b227
Merge branch 'model-armor-snippet-quickstart' of github.com:rudrakhsh…
rudrakhsha-crest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// 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 quickstart( | ||
projectId = 'my-project', | ||
locationId = 'us-central1', | ||
templateId = 'my-template' | ||
) { | ||
// [START modelarmor_quickstart] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// 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`, | ||
}); | ||
|
||
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, | ||
}); | ||
|
||
// Sanitize a user prompt using the created template | ||
const userPrompt = 'Unsafe user prompt'; | ||
|
||
const [userPromptSanitizeResponse] = await client.sanitizeUserPrompt({ | ||
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, | ||
userPromptData: { | ||
text: userPrompt, | ||
}, | ||
}); | ||
|
||
// Sanitize a model response using the created template | ||
const modelResponse = 'Unsanitized model output'; | ||
|
||
const [modelSanitizeResponse] = await client.sanitizeModelResponse({ | ||
name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, | ||
modelResponseData: { | ||
text: modelResponse, | ||
}, | ||
}); | ||
|
||
return { | ||
templateName: createdTemplate.name, | ||
userPromptSanitizeResponse, | ||
modelSanitizeResponse | ||
}; | ||
// [END modelarmor_quickstart] | ||
} | ||
|
||
module.exports = quickstart; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 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'; | ||
|
||
const cp = require('child_process'); | ||
const {v4: uuidv4} = require('uuid'); | ||
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1; | ||
|
||
let projectId; | ||
const locationId = process.env.GCLOUD_LOCATION || 'us-central1'; | ||
const options = { | ||
apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`, | ||
}; | ||
|
||
const client = new ModelArmorClient(options); | ||
const templateIdPrefix = `test-template-${uuidv4().substring(0, 8)}`; | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
|
||
describe('Model Armor tests', () => { | ||
const templatesToDelete = []; | ||
|
||
before(async () => { | ||
projectId = await client.getProjectId(); | ||
}); | ||
|
||
after(async () => { | ||
// Clean up all templates | ||
for (const templateName of templatesToDelete) { | ||
try { | ||
await client.deleteTemplate({name: templateName}); | ||
console.log(`Cleaned up template: ${templateName}`); | ||
} catch (error) { | ||
console.error(`Failed to delete template ${templateName}:`, error); | ||
} | ||
} | ||
}); | ||
|
||
// =================== Quickstart Tests =================== | ||
|
||
it('should create a template and sanitize content', async () => { | ||
const quickstart = require('../snippets/quickstart'); | ||
const testQuickstartTemplateId = `${templateIdPrefix}-quickstart`; | ||
|
||
await quickstart( | ||
projectId, | ||
locationId, | ||
testQuickstartTemplateId | ||
); | ||
|
||
templatesToDelete.push( | ||
`projects/${projectId}/locations/${locationId}/templates/${testQuickstartTemplateId}` | ||
); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These TODO comments should be addressed. Consider providing default values or removing the comments if they are no longer relevant.