|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Quickstart example for using Google Cloud Model Armor to |
| 19 | + * create a template with RAI filters and sanitize content. |
| 20 | + * |
| 21 | + * @param {string} projectId - Google Cloud project ID. |
| 22 | + * @param {string} locationId - Google Cloud location. |
| 23 | + * @param {string} templateId - ID for the template to create. |
| 24 | + */ |
| 25 | +async function quickstart( |
| 26 | + projectId = 'my-project', |
| 27 | + locationId = 'us-central1', |
| 28 | + templateId = 'my-template' |
| 29 | +) { |
| 30 | + // [START modelarmor_quickstart] |
| 31 | + /** |
| 32 | + * TODO(developer): Uncomment these variables before running the sample. |
| 33 | + */ |
| 34 | + // const projectId = 'my-project'; |
| 35 | + // const locationId = 'us-central1'; |
| 36 | + // const templateId = 'my-template'; |
| 37 | + |
| 38 | + // Imports the Model Armor library |
| 39 | + const modelarmor = require('@google-cloud/modelarmor'); |
| 40 | + const {ModelArmorClient} = modelarmor.v1; |
| 41 | + const {protos} = modelarmor; |
| 42 | + |
| 43 | + const {RaiFilterType} = protos.google.cloud.modelarmor.v1; |
| 44 | + const {DetectionConfidenceLevel} = protos.google.cloud.modelarmor.v1; |
| 45 | + |
| 46 | + // Instantiates a client |
| 47 | + const client = new ModelArmorClient({ |
| 48 | + apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`, |
| 49 | + }); |
| 50 | + |
| 51 | + const parent = `projects/${projectId}/locations/${locationId}`; |
| 52 | + |
| 53 | + // Build the Model Armor template with preferred filters |
| 54 | + // For more details on filters, refer to: |
| 55 | + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters |
| 56 | + const template = { |
| 57 | + filterConfig: { |
| 58 | + raiSettings: { |
| 59 | + raiFilters: [ |
| 60 | + { |
| 61 | + filterType: RaiFilterType.DANGEROUS, |
| 62 | + confidenceLevel: DetectionConfidenceLevel.HIGH, |
| 63 | + }, |
| 64 | + { |
| 65 | + filterType: RaiFilterType.HARASSMENT, |
| 66 | + confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE, |
| 67 | + }, |
| 68 | + { |
| 69 | + filterType: RaiFilterType.HATE_SPEECH, |
| 70 | + confidenceLevel: DetectionConfidenceLevel.HIGH, |
| 71 | + }, |
| 72 | + { |
| 73 | + filterType: RaiFilterType.SEXUALLY_EXPLICIT, |
| 74 | + confidenceLevel: DetectionConfidenceLevel.HIGH, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + const [createdTemplate] = await client.createTemplate({ |
| 82 | + parent, |
| 83 | + templateId, |
| 84 | + template, |
| 85 | + }); |
| 86 | + |
| 87 | + // Sanitize a user prompt using the created template |
| 88 | + const userPrompt = 'Unsafe user prompt'; |
| 89 | + |
| 90 | + const [userPromptSanitizeResponse] = await client.sanitizeUserPrompt({ |
| 91 | + name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, |
| 92 | + userPromptData: { |
| 93 | + text: userPrompt, |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + // Sanitize a model response using the created template |
| 98 | + const modelResponse = 'Unsanitized model output'; |
| 99 | + |
| 100 | + const [modelSanitizeResponse] = await client.sanitizeModelResponse({ |
| 101 | + name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`, |
| 102 | + modelResponseData: { |
| 103 | + text: modelResponse, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + return { |
| 108 | + templateName: createdTemplate.name, |
| 109 | + userPromptSanitizeResponse, |
| 110 | + modelSanitizeResponse, |
| 111 | + }; |
| 112 | + // [END modelarmor_quickstart] |
| 113 | +} |
| 114 | + |
| 115 | +module.exports = quickstart; |
0 commit comments