Initialization

更新时间:
复制 MD 格式

Initialize the AI Guardrails SDK for Python by creating a client instance with your credentials and region. The client is the entry point for all API calls.

Prerequisites

Before you begin, ensure that you have:

Set environment variables

Store your credentials as environment variables to avoid hardcoding sensitive values in your code.

Linux and 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>"

Replace <your-access-key-id> and <your-access-key-secret> with the AccessKey ID and AccessKey secret of your RAM user.

Warning

Never hardcode credentials in your source code or commit them to version control. Always load credentials from environment variables or a secrets manager.

Create a client instance

Both use cases use client.AcsClient from the aliyunsdkcore package. Pass your AccessKey credentials from environment variables and specify the region.

Moderate images, audio, videos, and text

import os
from aliyunsdkcore import client

# Load credentials from environment variables
access_key_id = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']

clt = client.AcsClient(access_key_id, access_key_secret, "cn-shanghai")

Manage custom image libraries, term libraries, and text pattern libraries

import os
from aliyunsdkcore import client

# Load credentials from environment variables
access_key_id = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']

clt = client.AcsClient(access_key_id, access_key_secret, "cn-shanghai")
Note

Both client types use the same constructor. Use the first client for content moderation API calls, and the second for managing custom libraries (image, term, and text pattern libraries).

What's next

  • Call content moderation APIs using the client instance you created.

  • Browse available endpoints to choose the region closest to your users: Endpoints.