Install the AI Guardrails SDK for Java by adding Maven dependencies to your project. This guide uses aliyun-java-sdk-green 3.6.6 as an example.
Prerequisites
Before you begin, make sure you have:
Java 1.6 or later. Run
java -versionto check.Apache Maven. Required to manage dependencies.
Add Maven dependencies
Add the following dependencies to the <dependencies> section of your pom.xml:
<!-- Core SDK -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.1</version>
</dependency>
<!-- AI Guardrails (Content Moderation) -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-green</artifactId>
<version>3.6.6</version>
</dependency>
<!-- JSON serialization -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.9</version>
</dependency>
<!-- OSS SDK: required for uploading local or binary files before moderation -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>Optional dependencies
If your code needs to read files or convert strings, add:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>Upload local or binary files
To submit a local or binary file for content moderation, download and import the Extension.Uploader utility class into your project.
Configure environment variables
The SDK reads your AccessKey pair from environment variables at runtime. When you call API operations, the system automatically completes authentication using these credentials. Before you use the SDK sample code, set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.
Linux / macOS
export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>Windows (Command Prompt)
setx ALIBABA_CLOUD_ACCESS_KEY_ID "<your-access-key-id>"
setx ALIBABA_CLOUD_ACCESS_KEY_SECRET "<your-access-key-secret>"After running setx on Windows, restart any open terminal sessions for the variables to take effect.Replace <your-access-key-id> and <your-access-key-secret> with your Alibaba Cloud AccessKey pair. For instructions on creating an AccessKey pair, see Configure credentials.