The Guardrails SDK lets you submit combined text and image content for multimodal security checks using the MultiModalGuard API. This page walks you through activating the service, setting up permissions, and integrating the SDK in Java, Python, PHP, or Go.
Prerequisites
Before you begin, ensure that you have:
An Alibaba Cloud account
A RAM (Resource Access Management) user with the
AliyunYundunGreenWebFullAccesspolicy attached (see Grant permissions to a RAM user below)An AccessKey pair for that RAM user
Activate the service
Go to the Guardrails activation page and activate the service. Billing is pay-as-you-go, charged daily based on actual usage. You are not charged until you make your first API call.
Grant permissions to a RAM user
Log on to the RAM console as a RAM administrator.
Create a RAM user. For details, see Create a RAM user.
Attach the
AliyunYundunGreenWebFullAccesssystem policy to the RAM user. For details, see Grant permissions to a RAM user.
After completing these steps, the RAM user can call Content Moderation API operations.
To get an AccessKey pair for the RAM user, see Obtain an AccessKey pair.
Step 3: Install and integrate the SDK
The following regions are supported:
Region | Public endpoint | VPC endpoint |
Singapore | green-cip.ap-southeast-1.aliyuncs.com | green-cip-vpc.ap-southeast-1.aliyuncs.com |
If you need SDK sample code in other languages, you can use the online debugging tool in the OpenAPI Developer Portal to debug API operations. The tool automatically generates SDK sample code for the corresponding API operations.
In Alibaba Cloud SDK code, you can create a default access credential by defining ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. When you call API operations of Alibaba Cloud services, the system directly accesses the credential, reads your AccessKey pair, and then automatically completes authentication. Before you use the SDK sample code, you must configure environment variables. For more information, see Configure credentials.
Sample code
Java SDK
Java 1.8 and later versions are supported.
For the original code, see Java SDK source code or Java SDK source code (OSS path).
Add the following dependency to the pom.xml file to use the SDK in a Maven project.
1. Add the following dependency to the dependencies section.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>green20220302</artifactId>
<version>2.21.0</version>
</dependency>2. Java SDK sample code.
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.green20220302.models.MultiModalGuardRequest;
import com.aliyun.green20220302.models.MultiModalGuardResponse;
import com.aliyun.green20220302.models.MultiModalGuardResponseBody;
import com.aliyun.teaopenapi.models.Config;
import java.util.Collections;
public class MultiModalGuardDemo {
public static void main(String[] args) throws Exception {
Config config = new Config();
/**
* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
* Common methods to obtain environment variables:
* Method 1:
* Obtain RAM user AccessKey ID: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain RAM user AccessKey Secret: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Obtain RAM user AccessKey ID: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain RAM user AccessKey Secret: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
config.setAccessKeyId("We recommend that you obtain the RAM user AccessKey ID from environment variables");
config.setAccessKeySecret("We recommend that you obtain the RAM user AccessKey Secret from environment variables");
//Modify the region and endpoint based on your actual situation
config.setRegionId("cn-shanghai");
config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
//Read timeout in milliseconds (ms).
config.setReadTimeout(10000);
//Connection timeout in milliseconds (ms).
config.setConnectTimeout(3000);
//Set HTTP proxy.
//config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
//Set HTTPS proxy.
//config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
com.aliyun.green20220302.Client client = new com.aliyun.green20220302.Client(config);
JSONObject serviceParameters = new JSONObject();
serviceParameters.put("content", "Test text content");
serviceParameters.put("imageUrls", Collections.singletonList("https://img.alicdn.com/tfs/xxxxxxxxxx001.png"));
MultiModalGuardRequest multiModalGuardRequest = new MultiModalGuardRequest();
// Detection type
multiModalGuardRequest.setService("text_img_security_check");
multiModalGuardRequest.setServiceParameters(serviceParameters.toJSONString());
try {
MultiModalGuardResponse response = client.multiModalGuard(multiModalGuardRequest);
if (response.getStatusCode() == 200) {
MultiModalGuardResponseBody result = response.getBody();
System.out.println(JSON.toJSONString(result));
System.out.println("requestId = " + result.getRequestId());
System.out.println("code = " + result.getCode());
System.out.println("msg = " + result.getMessage());
Integer code = result.getCode();
if (200 == code) {
MultiModalGuardResponseBody.MultiModalGuardResponseBodyData data = result.getData();
System.out.println(JSON.toJSONString(data, true));
} else {
System.out.println("multi modal guard not success. code:" + code);
}
} else {
System.out.println("response not success. status:" + response.getStatusCode());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Python SDK
Python 3.6 and later versions are supported.
For the original code, see Python SDK source code.
1. Run the following command to import the dependencies.
pip install alibabacloud_green20220302==2.21.22. Python SDK sample code.
# coding=utf-8
# python version >= 3.6
from alibabacloud_green20220302.client import Client
from alibabacloud_green20220302 import models
from alibabacloud_tea_openapi.models import Config
import json
config = Config(
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
# We recommend that you do not include your AccessKey ID and AccessKey secret in your project code for data security reasons.
# Common methods to obtain environment variables:
# Obtain RAM user AccessKey ID: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain RAM user AccessKey Secret: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
access_key_id='We recommend that you obtain the RAM user AccessKey ID from environment variables',
access_key_secret='We recommend that you obtain the RAM user AccessKey Secret from environment variables',
# Connection timeout in milliseconds (ms)
connect_timeout=10000,
# Read timeout in milliseconds (ms)
read_timeout=3000,
region_id='cn-shanghai',
endpoint='green-cip.cn-shanghai.aliyuncs.com'
)
clt = Client(config)
serviceParameters = {
'content': 'Test text content',
'imageUrls': ['https://img.alicdn.com/tfs/xxxxxxxxxx001.png']
}
multiModalGuardRequest = models.MultiModalGuardRequest(
# Detection type
service='text_img_security_check',
service_parameters=json.dumps(serviceParameters)
)
try:
response = clt.multi_modal_guard(multiModalGuardRequest)
if response.status_code == 200:
# Call successful
result = response.body
print('response success. result:{}'.format(result))
else:
print('response not success. status:{} ,result:{}'.format(response.status_code, response))
except Exception as err:
print(err)
PHP SDK
PHP 5.6 and later versions are supported.
For the original code, see PHP SDK source code.
1. Run the following command to import the dependencies.
composer require alibabacloud/green-202203022. PHP SDK sample code.
<?php
require('vendor/autoload.php');
use AlibabaCloud\Dara\Models\RuntimeOptions;
use AlibabaCloud\SDK\Green\V20220302\Models\MultiModalGuardRequest;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Green\V20220302\Green;
$config = new Config([
/**
* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
* We recommend that you do not include your AccessKey ID and AccessKey secret in your project code for data security reasons.
* Common methods to obtain environment variables:
* Obtain RAM user AccessKey ID: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain RAM user AccessKey Secret: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
"accessKeyId" => 'We recommend that you obtain the RAM user AccessKey ID from environment variables',
"accessKeySecret" => 'We recommend that you obtain the RAM user AccessKey Secret from environment variables',
// Set HTTP proxy.
// "httpProxy" => "http://10.10.xx.xx:xxxx",
// Set HTTPS proxy.
// "httpsProxy" => "https://10.10.xx.xx:xxxx",
"endpoint" => "green-cip.cn-shanghai.aliyuncs.com",
"regionId" => "cn-shanghai"
]);
// Note: The client instantiated here should be reused as much as possible to avoid repeatedly establishing connections and to improve detection performance.
$client = new Green($config);
$request = new MultiModalGuardRequest();
$request->service = "text_img_security_check";
$serviceParameters = array("content" => "Test content", "imageUrls" => ["https://img.alicdn.com/tfs/xxxxxxxxxx001.png"]);
$request->serviceParameters = json_encode($serviceParameters);
$runtime = new RuntimeOptions();
$runtime->readTimeout = 10000;
$runtime->connectTimeout = 3000;
try {
$response = $client->multiModalGuardWithOptions($request, $runtime);
print_r($response->body);
if (200 != $response->statusCode) {
print_r("response not success. code:" . $response->statusCode);
return;
}
$body = $response->body;
print_r("requestId = " . $body->requestId . "\n");
print_r("code = " . $body->code . "\n");
print_r("message = " . $body->message . "\n");
if (200 != $body->code) {
print_r("multi modal guard not success. code:" . $body->code);
}
$data = $body->data;
print_r("data = " . json_encode($data));
} catch (TeaUnableRetryError $e) {
var_dump($e->getMessage());
var_dump($e->getErrorInfo());
var_dump($e->getLastException());
var_dump($e->getLastRequest());
}Go SDK
1. Run the following command to import the dependencies.
go get github.com/alibabacloud-go/green-202203022. Go SDK sample code.
package main
import (
"encoding/json"
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
green20220302 "github.com/alibabacloud-go/green-20220302/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"net/http"
)
func main() {
// If the project code is leaked, the AccessKey pair may be leaked and security issues may occur on all resources of your account. The following code example is for reference only. We recommend that you use the more secure STS method.
config := &openapi.Config{
/**
* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
* We recommend that you do not include your AccessKey ID and AccessKey secret in your project code for data security reasons.
* Common methods to obtain environment variables:
* Obtain RAM user AccessKey ID: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
* Obtain RAM user AccessKey Secret: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
*/
AccessKeyId: tea.String("We recommend that you obtain the RAM user AccessKey ID from environment variables"),
AccessKeySecret: tea.String("We recommend that you obtain the RAM user AccessKey Secret from environment variables"),
// Set HTTP proxy.
// HttpProxy: tea.String("http://xx.xx.xx.xx:xxxx"),
// Set HTTPS proxy.
// HttpsProxy: tea.String("https://username:password@xxx.xxx.xxx.xxx:9999"),
RegionId: tea.String("cn-shanghai"),
Endpoint: tea.String("green-cip.cn-shanghai.aliyuncs.com"),
/**
* Set timeout periods. The server-side end-to-end processing timeout is 10 seconds. Configure your settings accordingly.
* If you set ReadTimeout to a value less than the time required for server-side processing, your program will receive a ReadTimeout exception.
*/
ConnectTimeout: tea.Int(3000),
ReadTimeout: tea.Int(10000),
}
client, _err := green20220302.NewClient(config)
if _err != nil {
panic(_err)
}
// Create a RuntimeObject instance and set runtime parameters.
runtime := &util.RuntimeOptions{}
runtime.ReadTimeout = tea.Int(10000)
runtime.ConnectTimeout = tea.Int(10000)
serviceParameters, _ := json.Marshal(
map[string]interface{}{
"content": "Test content",
"imageUrls": [1]string{"https://img.alicdn.com/tfs/xxxxxxxxxx001.png"},
},
)
request := green20220302.MultiModalGuardRequest{
Service: tea.String("text_img_security_check"),
ServiceParameters: tea.String(string(serviceParameters)),
}
result, _err := client.MultiModalGuardWithOptions(&request, runtime)
if _err != nil {
panic(_err)
}
if *result.StatusCode != http.StatusOK {
fmt.Printf("response not success. status:%d\n", *result.StatusCode)
return
}
body := result.Body
fmt.Printf("response success. requestId:%s, code:%d, msg:%s\n", *body.RequestId, *body.Code, *body.Message)
if *body.Code != http.StatusOK {
fmt.Printf("multi modal guard not success. code:%d\n", *body.Code)
return
}
data := body.Data
fmt.Printf("multi modal guard data:%s\n", *data)
}
Install and configure the SDK
Supported regions
| Region | Public endpoint | VPC endpoint |
|---|---|---|
| Singapore | green-cip.ap-southeast-1.aliyuncs.com | green-cip-vpc.ap-southeast-1.aliyuncs.com |
Set up credentials
Store your AccessKey pair as environment variables so you never hardcode credentials in source code:
export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>The SDK reads ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET automatically when you initialize the client. For other credential methods, see Configure credentials.
If you need sample code in a language not listed here, use the online debugging tool in the OpenAPI Developer Portal to generate SDK samples automatically.
Make a multimodal detection request
All examples call the MultiModalGuard operation with service set to text_img_security_check. Each request passes a serviceParameters JSON string containing the content to analyze.
Request parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
service | String | Yes | Detection type. Set to text_img_security_check for combined text and image checks. |
serviceParameters | String (JSON) | Yes | JSON-serialized object containing the content to analyze. See fields below. |
Fields inside `serviceParameters`:
| Field | Type | Required | Description |
|---|---|---|---|
content | String | Yes | Text content to check. |
imageUrls | Array of strings | Yes | List of image URLs to check alongside the text. |
Response structure
A successful call returns HTTP 200 with a response body. Check the body's code field to confirm the operation succeeded.
| Field | Type | Description |
|---|---|---|
requestId | String | The request ID, useful for troubleshooting. |
code | Integer | Result code. 200 means success; any other value indicates an error. |
message | String | Human-readable description of the result or error. |
data | Object | Detection result. Returned only when code is 200. |
The server-side end-to-end processing timeout is 10 seconds. Set your SDK read timeout to at least 10,000 ms to avoid ReadTimeout exceptions before the server responds.Timeout configuration
| Setting | Recommended value | Description |
|---|---|---|
| Connection timeout | 3,000 ms | Time to establish a TCP connection. |
| Read timeout | 10,000 ms | Time to wait for a response. Must be at least equal to the server-side processing time (10 s). |
Java SDK
Requires Java 1.8 or later. Source: GitHub or Maven Central.
1. Add the Maven dependency.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>green20220302</artifactId>
<version>2.21.0</version>
</dependency>2. Call `MultiModalGuard`.
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.green20220302.models.MultiModalGuardRequest;
import com.aliyun.green20220302.models.MultiModalGuardResponse;
import com.aliyun.green20220302.models.MultiModalGuardResponseBody;
import com.aliyun.teaopenapi.models.Config;
import java.util.Collections;
public class MultiModalGuardDemo {
public static void main(String[] args) throws Exception {
Config config = new Config();
// Read credentials from environment variables — do not hardcode AccessKey pairs.
config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.setRegionId("ap-southeast-1");
config.setEndpoint("green-cip.ap-southeast-1.aliyuncs.com");
// Read timeout must be >= 10,000 ms (server-side processing time).
config.setReadTimeout(10000);
config.setConnectTimeout(3000);
com.aliyun.green20220302.Client client = new com.aliyun.green20220302.Client(config);
JSONObject serviceParameters = new JSONObject();
serviceParameters.put("content", "Test text content");
serviceParameters.put("imageUrls", Collections.singletonList("https://img.alicdn.com/tfs/xxxxxxxxxx001.png"));
MultiModalGuardRequest request = new MultiModalGuardRequest();
request.setService("text_img_security_check");
request.setServiceParameters(serviceParameters.toJSONString());
try {
MultiModalGuardResponse response = client.multiModalGuard(request);
if (response.getStatusCode() == 200) {
MultiModalGuardResponseBody result = response.getBody();
System.out.println("requestId = " + result.getRequestId());
System.out.println("code = " + result.getCode());
System.out.println("message = " + result.getMessage());
if (200 == result.getCode()) {
MultiModalGuardResponseBody.MultiModalGuardResponseBodyData data = result.getData();
System.out.println(JSON.toJSONString(data, true));
} else {
System.out.println("Detection failed. code: " + result.getCode());
}
} else {
System.out.println("HTTP error. status: " + response.getStatusCode());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}Python SDK
Requires Python 3.6 or later. Source: PyPI.
1. Install the package.
pip install alibabacloud_green20220302==2.21.22. Call `multi_modal_guard`.
# coding=utf-8
import json
import os
from alibabacloud_green20220302.client import Client
from alibabacloud_green20220302 import models
from alibabacloud_tea_openapi.models import Config
config = Config(
# Read credentials from environment variables — do not hardcode AccessKey pairs.
access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
region_id='ap-southeast-1',
endpoint='green-cip.ap-southeast-1.aliyuncs.com',
# Connection timeout in milliseconds (ms).
connect_timeout=10000,
# Read timeout in milliseconds (ms).
read_timeout=3000,
)
client = Client(config)
service_parameters = {
'content': 'Test text content',
'imageUrls': ['https://img.alicdn.com/tfs/xxxxxxxxxx001.png'],
}
request = models.MultiModalGuardRequest(
service='text_img_security_check',
service_parameters=json.dumps(service_parameters),
)
try:
response = client.multi_modal_guard(request)
if response.status_code == 200:
result = response.body
print('requestId:', result.request_id)
print('code:', result.code)
print('message:', result.message)
if result.code == 200:
print('data:', result.data)
else:
print('Detection failed. code:', result.code)
else:
print('HTTP error. status:', response.status_code)
except Exception as e:
print(e)PHP SDK
Requires PHP 5.6 or later. Source: Packagist.
1. Install the package.
composer require alibabacloud/green-202203022. Call `multiModalGuardWithOptions`.
<?php
require('vendor/autoload.php');
use AlibabaCloud\Dara\Models\RuntimeOptions;
use AlibabaCloud\SDK\Green\V20220302\Models\MultiModalGuardRequest;
use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Green\V20220302\Green;
$config = new Config([
// Read credentials from environment variables — do not hardcode AccessKey pairs.
'accessKeyId' => getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),
'accessKeySecret' => getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
'endpoint' => 'green-cip.ap-southeast-1.aliyuncs.com',
'regionId' => 'ap-southeast-1',
]);
// Reuse the client instance across requests to avoid repeated connection overhead.
$client = new Green($config);
$request = new MultiModalGuardRequest();
$request->service = 'text_img_security_check';
$request->serviceParameters = json_encode([
'content' => 'Test content',
'imageUrls' => ['https://img.alicdn.com/tfs/xxxxxxxxxx001.png'],
]);
$runtime = new RuntimeOptions();
// Read timeout must be >= 10,000 ms (server-side processing time).
$runtime->readTimeout = 10000;
$runtime->connectTimeout = 3000;
try {
$response = $client->multiModalGuardWithOptions($request, $runtime);
if (200 !== $response->statusCode) {
echo 'HTTP error. status: ' . $response->statusCode . "\n";
return;
}
$body = $response->body;
echo 'requestId = ' . $body->requestId . "\n";
echo 'code = ' . $body->code . "\n";
echo 'message = ' . $body->message . "\n";
if (200 !== $body->code) {
echo 'Detection failed. code: ' . $body->code . "\n";
} else {
echo 'data = ' . json_encode($body->data) . "\n";
}
} catch (TeaUnableRetryError $e) {
var_dump($e->getMessage());
var_dump($e->getErrorInfo());
var_dump($e->getLastException());
var_dump($e->getLastRequest());
}Go SDK
1. Install the package.
go get github.com/alibabacloud-go/green-202203022. Call `MultiModalGuardWithOptions`.
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
green20220302 "github.com/alibabacloud-go/green-20220302/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
func main() {
config := &openapi.Config{
// Read credentials from environment variables — do not hardcode AccessKey pairs.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
RegionId: tea.String("ap-southeast-1"),
Endpoint: tea.String("green-cip.ap-southeast-1.aliyuncs.com"),
// Read timeout must be >= 10,000 ms (server-side processing time).
ConnectTimeout: tea.Int(3000),
ReadTimeout: tea.Int(10000),
}
client, err := green20220302.NewClient(config)
if err != nil {
panic(err)
}
runtime := &util.RuntimeOptions{
ReadTimeout: tea.Int(10000),
ConnectTimeout: tea.Int(10000),
}
serviceParameters, _ := json.Marshal(map[string]interface{}{
"content": "Test content",
"imageUrls": []string{"https://img.alicdn.com/tfs/xxxxxxxxxx001.png"},
})
request := &green20220302.MultiModalGuardRequest{
Service: tea.String("text_img_security_check"),
ServiceParameters: tea.String(string(serviceParameters)),
}
result, err := client.MultiModalGuardWithOptions(request, runtime)
if err != nil {
panic(err)
}
if *result.StatusCode != http.StatusOK {
fmt.Printf("HTTP error. status: %d\n", *result.StatusCode)
return
}
body := result.Body
fmt.Printf("requestId: %s, code: %d, message: %s\n", *body.RequestId, *body.Code, *body.Message)
if *body.Code != http.StatusOK {
fmt.Printf("Detection failed. code: %d\n", *body.Code)
return
}
fmt.Printf("data: %s\n", *body.Data)
}What's next
Content Moderation API reference — full list of API operations and parameters
Configure credentials — other authentication methods, including STS temporary credentials