This topic demonstrates how to use Alibaba Cloud CLI to call the Content Moderation-Enhanced Edition API, using the TextModeration API from AI Guardrails as an example for text moderation.
Prerequisites
Alibaba Cloud Command Line Interface (CLI) is a general-purpose command-line tool built on OpenAPI. You can use Alibaba Cloud CLI to automate the management and maintenance of Content Moderation-Enhanced Edition. For more information, see What is Alibaba Cloud CLI.
Alibaba Cloud CLI does not support calling OSS Compliance Detection (General Edition).
Step 1: Install Alibaba Cloud CLI
You must install Alibaba Cloud CLI before you can use it. Choose the installation method for your operating system.
Windows: Install CLI (Windows).
Linux: Install CLI (Linux).
macOS: Install CLI (macOS).
Cloud Shell comes with Alibaba Cloud CLI pre-installed and your credentials automatically configured. You can use Cloud Shell to debug Alibaba Cloud CLI commands. For more information, see What is Cloud Shell.
Step 2: Configure Alibaba Cloud CLI
An Alibaba Cloud root account has full permissions to manage and access all OpenAPI services, which poses a high security risk. We strongly recommend that you create a RAM user, grant permissions based on the principle of least privilege, and use the RAM user to access OpenAPI. For more information about the permission policies supported by Content Moderation-Enhanced Edition, see Use RAM for access control.
Before you can use Alibaba Cloud CLI, you must configure your credentials, region ID, and other information. Alibaba Cloud CLI supports multiple credential types. For more information, see Credential types. This topic shows how to configure AK-based credentials by using the AccessKey of a RAM user.
Create a RAM user and an AccessKey to configure identity credentials for later use. For more information, see Create a RAM user and Create an AccessKey.
Grant permissions to the RAM user. In this example, you must grant the RAM user the
AliyunYundunGreenWebFullAccesspermission to call the APIs of Content Moderation-Enhanced Edition. For detailed steps, see Manage permissions for RAM users.Obtain an available region ID so that you can configure your credentials. Alibaba Cloud CLI uses the region that you specify to initiate OpenAPI calls. The available regions for Content Moderation-Enhanced Edition are as follows:
NoteWhen you use Alibaba Cloud CLI, you can use the
--regionoption to specify a region to run a command. This option ignores the region information in the default credential configuration and environment variable settings. For more information, see Command-line options.Use the AccessKey of a RAM user to configure an AK-type credential in a configuration file named
AkProfile. For more information, see Configuration Example.
Step 3: Generate a CLI example
In the OpenAPI Portal, go to the TextModeration debug page.
In the Parameter Configuration section, enter the request parameters. Click the CLI Example tab to view the generated CLI example.
Copy the CLI example or run it quickly in Cloud Shell:
Click the Run Command button
to launch Cloud Shell and debug the command.Click the Copy button
to copy the CLI example. You can then run it in a local shell or use it in an automation script.
NoteWhen you copy a CLI example to a local shell for debugging, pay attention to the parameter format. For more information about the parameter format for Alibaba Cloud CLI commands, see Parameter format.
The OpenAPI Portal adds the
--regionoption to generated examples by default. When you copy the command and run it locally, the Alibaba Cloud CLI ignores the region information in your default credential configuration and environment variables, and instead uses the region specified in the command. You can delete or keep this option as needed.
Step 4: Call the Content Moderation-Enhanced Edition API
Example 1: List available APIs
This example shows how to use the --help option to list the available Content Moderation-Enhanced Edition API operations.
Run the following command.
aliyun green --helpExpected output.

Example 2: Detect non-compliant nicknames
The following example shows how to use Alibaba Cloud CLI to call the TextModeration API of Content Moderation-Enhanced Edition to detect non-compliant user nicknames. For more information about the API, see Text Moderation Plus.
Run the command.
aliyun green TextModeration --region cn-beijing --Service nickname_detection --ServiceParameters '{"content":"Content to be detected"}'Expected output.
If a system policy is hit:
{ "Code": 200, "Data": { "Labels": "sexual_content", "Reason": "{\"riskLevel\":\"high\",\"riskTips\":\"Pornography_Vulgar_Words\",\"riskWords\":\"Erotic_Services\"}", "AccountId": "10123****" }, "Message": "OK", "RequestId": "AAAAAA-BBBB-CCCCC-DDDD-EEEEEEEE****" }Matched custom keyword:
{ "Code": 200, "Data": { "Labels": "sexual_content", "Reason": "{\"riskLevel\":\"high\",\"customizedWords\":\"my_custom_keyword\",\"customizedLibs\":\"my_custom_library_name\"}", "AccountId": "10123****" }, "Message": "OK", "RequestId": "AAAAAA-BBBB-CCCCC-DDDD-EEEEEEEE****" }NoteIf a call to the Content Moderation-Enhanced Edition OpenAPI returns an error, you need to check whether the request parameters and their values are correct based on the returned error code.
You can also use the RequestID or SDK error message with the Alibaba Cloud OpenAPI Diagnostic Platform for self-service diagnostics.