Query business scenarios

Updated at:

Use the AI Guardrails SDK for Java to list all business scenarios in your account. The bizType values returned are used as parameters when creating moderation tasks or referencing an existing configuration.

Prerequisites

Before you begin, make sure you have:

  • Installed Java dependencies. For details, see Installation.

    Use the Java version specified in the Installation topic. Using a different version causes subsequent operation calls to fail.
  • (If submitting a local image or binary image stream for image moderation) Downloaded and imported the Extension.Uploader utility class into your project.

Supported regions

The DescribeUserBizTypes operation is supported in the following regions:

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

Query business scenarios

Call DescribeUserBizTypes to retrieve all business scenarios, including both custom scenarios and the default scenarios built into AI Guardrails.

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.DescribeUserBizTypesRequest;
import com.aliyuncs.green.model.v20170823.DescribeUserBizTypesResponse;
import com.aliyuncs.green.model.v20170823.DescribeUserBizTypesResponse.Item;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class DescribeBizTypesRequestSample {

    public static void main(String[] args) throws Exception {
        // Use a RAM user's credentials to reduce security risk.
        // Get the AccessKey ID from an environment variable.
        // Get the AccessKey secret from an environment variable.
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);

        DescribeUserBizTypesRequest describeUserBizTypesRequest = new DescribeUserBizTypesRequest();
        describeUserBizTypesRequest.setAcceptFormat(FormatType.JSON);
        describeUserBizTypesRequest.setMethod(MethodType.GET);
        describeUserBizTypesRequest.setEncoding("utf-8");
        // Connection timeout, in milliseconds.
        describeUserBizTypesRequest.setConnectTimeout(3000);
        // Read timeout, in milliseconds.
        describeUserBizTypesRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(describeUserBizTypesRequest);
            if (httpResponse.isSuccess()) {
                DescribeUserBizTypesResponse response = JSON.parseObject(
                        new String(httpResponse.getHttpContent(), "UTF-8"),
                        DescribeUserBizTypesResponse.class);

                System.out.println("Total business scenarios: " + response.getBizTypeList().size());
                if (response.getBizTypeList() != null) {
                    for (Item bizTypeItem : response.getBizTypeList()) {
                        System.out.println(bizTypeItem.getBizType());
                        System.out.println(bizTypeItem.getCiteTemplate());
                        System.out.println(bizTypeItem.getIndustryInfo());
                        System.out.println(bizTypeItem.getSource());
                    }
                }
            } else {
                JSONObject scrResponse = JSON.parseObject(
                        new String(httpResponse.getHttpContent(), "utf-8"));
                System.out.println("Query failed. Message: " + JSON.toJSONString(scrResponse.get("Message"), true));
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Response fields

Each item in getBizTypeList() contains the following fields:

FieldTypeDescription
bizTypeStringThe name of the business scenario. Use this value as a parameter when creating moderation tasks.
citeTemplateBooleanSpecifies whether an industry template was imported into this scenario.
industryInfoStringThe industry information associated with this scenario.
sourceStringHow the scenario was created. Valid values: custom (user-created scenario), system (AI Guardrails built-in scenario).

What's next

  • Use a bizType value from the list above when importing an existing configuration into a new business scenario.

  • Pass the bizType value as a parameter in your moderation task requests to apply the corresponding rules.

  • For the full API reference, see DescribeUserBizTypes.