Create a business scenario

Updated at:

Use the AI Guardrails SDK for Java to create a business scenario (BizType). A business scenario is a named container for machine-assisted moderation policies that you apply through the AI Guardrails API.

Prerequisites

Before you begin, make sure you have:

  • Java dependencies installed as described in Installation. Use the exact Java version specified there — mismatched versions cause subsequent API calls to fail.

  • (If submitting local images or binary image streams) The Extension.Uploader utility class downloaded and imported into your project.

Supported regions

The CreateBizType operation is available in the following regions:

Region IDLocation
cn-shanghaiChina (Shanghai)
cn-beijingChina (Beijing)
cn-shenzhenChina (Shenzhen)
ap-southeast-1Singapore

Create a business scenario

Call the CreateBizType operation with the following parameters:

ParameterRequiredDescription
BizTypeNameYesName for the new business scenario.
BizTypeImportNoName of an existing business scenario whose configuration to copy into the new one.
CiteTemplateNoSet to true to initialize the scenario from an industry template. Requires IndustryInfo.
IndustryInfoRequired if CiteTemplate is trueIndustry classification for the template. Valid values: Social-Registration information-Profile picture, Social-Registration information-Nickname.

The following example creates a business scenario in the cn-shanghai region.

Important

An Alibaba Cloud account's AccessKey pair has full API permissions. Use a RAM user's credentials instead to follow the principle of least privilege. Store credentials in environment variables — never hardcode them in source code.

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20170823.CreateBizTypeRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;

public class CreateBizTypeRequestSample {

    public static void main(String[] args) throws Exception {
        // Load credentials from environment variables.
        // To set them:
        //   export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id>
        //   export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                accessKeyId,
                accessKeySecret);
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        CreateBizTypeRequest createBizTypeRequest = new CreateBizTypeRequest();
        // Required: name of the business scenario to create.
        createBizTypeRequest.setBizTypeName("<biz-type-name>");
        // Optional: copy configuration from an existing business scenario.
        createBizTypeRequest.setBizTypeImport("<existing-biz-type-name>");
        // Optional: initialize from an industry template.
        // If set to true, IndustryInfo is required.
        createBizTypeRequest.setCiteTemplate(true);
        // Required if CiteTemplate is true.
        // Valid values: "Social-Registration information-Profile picture"
        //               "Social-Registration information-Nickname"
        createBizTypeRequest.setIndustryInfo("Social-Registration information-Profile picture");
        createBizTypeRequest.setAcceptFormat(FormatType.JSON);
        createBizTypeRequest.setMethod(com.aliyuncs.http.MethodType.POST);
        createBizTypeRequest.setEncoding("utf-8");
        // Adjust timeouts as needed. Unit: milliseconds.
        createBizTypeRequest.setConnectTimeout(3000);
        createBizTypeRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(createBizTypeRequest);
            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "utf-8"));
                System.out.println("create success." + JSON.toJSONString(scrResponse, true));
            } else {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "utf-8"));
                System.out.println("create not success. " + JSON.toJSONString(scrResponse.get("Message"), true));
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Replace the following placeholders before running the code:

PlaceholderDescriptionExample
<biz-type-name>Name for the new business scenariomy-social-profile-check
<existing-biz-type-name>Name of an existing scenario to copy from (omit setBizTypeImport if not needed)default-social

Verify the result

If the request succeeds, the console prints create success. followed by the response body. If the request fails, the response includes a Message field describing the error.

What's next

After creating a business scenario, configure its moderation policy and use it with the AI Guardrails API for machine-assisted moderation.