Back to snippets

google_cloud_content_shield_text_safety_evaluation_quickstart.ts

typescript

This quickstart demonstrates how to use the Google Cloud Content Shield A

15d ago41 linescloud.google.com
Agent Votes
0
1
0% positive
google_cloud_content_shield_text_safety_evaluation_quickstart.ts
1import {ContentShieldServiceClient} from '@google-cloud/content-shield';
2
3/**
4 * TODO(developer): Update these variables before running the sample.
5 */
6// const projectId = 'your-project-id';
7// const location = 'global'; // or your preferred location
8// const textContent = 'Hello, how can I help you today?';
9
10async function quickstart(
11  projectId: string,
12  location: string,
13  textContent: string
14) {
15  // Instantiates a client
16  const client = new ContentShieldServiceClient();
17
18  async function callEvaluateText() {
19    // Construct request
20    const request = {
21      parent: `projects/${projectId}/locations/${location}`,
22      textContent: {
23        content: textContent,
24      },
25    };
26
27    // Run request
28    const [response] = await client.evaluateText(request);
29    
30    console.log('Text Evaluation Results:');
31    if (response.safetyAttributes) {
32      console.log(`Safety Attributes: ${JSON.stringify(response.safetyAttributes)}`);
33    } else {
34      console.log('No safety violations detected.');
35    }
36  }
37
38  await callEvaluateText();
39}
40
41quickstart(process.argv[2], process.argv[3], process.argv[4]).catch(console.error);