Multimodal SDK Reference
This topic describes how to integrate the AI Guardrails SDK.
Step 1: Activate the service
Go to the Activate AI Guardrails page and activate AI Guardrails. The default billing method is pay-as-you-go. You are billed for actual usage daily. No charges apply until you make API calls. After you integrate the API operations, the system automatically generates bills based on your usage.
Step 2: Grant permissions to a RAM user
Before you integrate the SDK or call the API, you must grant permissions to a RAM user. You can create an AccessKey pair for your Alibaba Cloud account and RAM users. You must use the AccessKey pair to complete identity authentication when you call Alibaba Cloud API operations. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.
Grant permissions to a RAM user
Log on to the RAM console using your Alibaba Cloud account.
Create a RAM user. For details, see Create a RAM user.
Grant the
AliyunYundunGreenWebFullAccesssystem policy to the RAM user. This policy grants full access to Content Moderation. For details, see Manage RAM user permissions.The RAM user can now call the Content Moderation API.
Step 3: Install and integrate the SDK
The following regions are supported:
Region | Public endpoint | Internal endpoint |
China (Shanghai) | https://green-cip.cn-shanghai.aliyuncs.com | https://green-cip-vpc.cn-shanghai.aliyuncs.com |
China (Beijing) | https://green-cip.cn-beijing.aliyuncs.com | https://green-cip-vpc.cn-beijing.aliyuncs.com |
China (Hangzhou) | https://green-cip.cn-hangzhou.aliyuncs.com | https://green-cip-vpc.cn-hangzhou.aliyuncs.com |
China (Shenzhen) | https://green-cip.cn-shenzhen.aliyuncs.com | https://green-cip-vpc.cn-shenzhen.aliyuncs.com |
China (Chengdu) | https://green-cip.cn-chengdu.aliyuncs.com | Not available |
China (Hong Kong) | https://green-cip.cn-hongkong.aliyuncs.com | Not available |
China East 1 Finance (Hangzhou) | https://green-cip.cn-hangzhou-finance.aliyuncs.com | Not available |
China East 2 Finance (Shanghai) | https://green-cip.cn-shanghai-finance-1.aliyuncs.com | Not available |
Shenzhen Finance | https://green-cip.cn-shenzhen-finance-1.aliyuncs.com | Not available |
Singapore | https://green-cip.ap-southeast-1.aliyuncs.com | https://green-cip-vpc.ap-southeast-1.aliyuncs.com |
Germany (Frankfurt) | https://green-cip.eu-central-1.aliyuncs.com | https://green-cip-vpc.eu-central-1.aliyuncs.com |
US (Virginia) | https://green-cip.us-east-1.aliyuncs.com | https://green-cip-vpc.us-central-1.aliyuncs.com |
To obtain SDK sample code in other programming languages, test API operations in OpenAPI Explorer. OpenAPI Explorer automatically generates sample code for these API operations.
Alibaba Cloud SDKs create default credentials by reading the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. When you call an API operation, the program reads your AccessKey from these variables and automatically completes authentication. Before you use SDK sample code, you must configure the environment variables. For more information, see Configure authentication credentials.
Sample code
Java SDK
Java 1.8 or later is supported.
For the source code, see Java SDK source code.
Add the 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>3.3.3</version>
</dependency>2. Sample Java SDK code for integration.
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. For security, use a RAM user to call API operations or perform routine O&M.
* Common methods to obtain environment variables:
* Method 1:
* Obtain the AccessKey ID of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of the RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Obtain the AccessKey ID of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of the RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
config.setAccessKeyId("Obtain the AccessKey ID of the RAM user from an environment variable.");
config.setAccessKeySecret("Obtain the AccessKey secret of the RAM user from an environment variable.");
// Modify the region and endpoint as needed.
config.setRegionId("cn-shanghai");
config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
// The read timeout period. Unit: milliseconds (ms).
config.setReadTimeout(10000);
// The connection timeout period. Unit: milliseconds (ms).
config.setConnectTimeout(3000);
// Set the HTTP proxy.
//config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
// Set the 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", "Sample text for testing");
serviceParameters.put("imageUrls", Collections.singletonList("https://img.alicdn.com/tfs/xxxxxxxxxx001.png"));
MultiModalGuardRequest multiModalGuardRequest = new MultiModalGuardRequest();
// The 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 or later is supported.
For the source code, see Python SDK source code.
1. Run the following command to import the dependency.
pip install alibabacloud_green20220302==3.2.42. Sample Python SDK code for integration.
# 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. For security, use a RAM user to call API operations or perform routine O&M.
# Do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
# Common methods to obtain environment variables:
# Obtain the AccessKey ID of the RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
# Obtain the AccessKey secret of the RAM user: os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
access_key_id='Obtain the AccessKey ID of the RAM user from an environment variable',
access_key_secret='Obtain the AccessKey secret of the RAM user from an environment variable',
# The connection timeout period. Unit: milliseconds (ms).
connect_timeout=10000,
# The read timeout period. Unit: milliseconds (ms).
read_timeout=3000,
region_id='cn-shanghai',
endpoint='green-cip.cn-shanghai.aliyuncs.com'
)
clt = Client(config)
serviceParameters = {
'content': 'Sample text for testing',
'imageUrls': ['https://img.alicdn.com/tfs/xxxxxxxxxx001.png']
}
multiModalGuardRequest = models.MultiModalGuardRequest(
# The detection type.
service='text_img_security_check',
service_parameters=json.dumps(serviceParameters)
)
try:
response = clt.multi_modal_guard(multiModalGuardRequest)
if response.status_code == 200:
# The call is 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 or later is supported.
For the source code, see PHP SDK source code.
1. Run the following command to import the dependency.
composer require alibabacloud/green-20220302 3.2.4
Go SDK
1. Run the following command to import the dependency.
go get github.com/alibabacloud-go/green-20220302/v3