Python

更新时间:
复制 MD 格式

Python SDK

Configure access credentials using environment variables

  1. Note:

    1. An AccessKey for an Alibaba Cloud account grants full access to all APIs and therefore poses a high security threat. We recommend that you create and use a Resource Access Management (RAM) user for API calls or daily O&M. To create a RAM user, log on to the RAM console.

    2. To prevent key leaks, do not hardcode your AccessKey ID and AccessKey secret in your code. Instead, configure environment variables to store and access your access credentials.

  2. Configuring Linux and macOS

    export NLP_AK_ENV=<access_key_id>
    export NLP_SK_ENV=<access_key_secret>

    Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about how to obtain an AccessKey ID and an AccessKey secret, see Step 2: Obtain an AccessKey for your account.

  3. Configuration method for Windows

    1. Create a file for the environment variables. Add the NLP_AK_ENV and NLP_SK_ENV environment variables and set their values to your AccessKey ID and AccessKey secret.

    2. Restart Windows.

Call the AliNLP 2.0 SDK (Recommended)

# pip install command
pip install aliyun-python-sdk-alinlp==1.0.20
import json
import os

# This example shows how to perform tokenization. For the API names and parameters of other algorithms, see the relevant documents.
from aliyunsdkalinlp.request.v20200629 import GetWsCustomizedChGeneralRequest

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException

/**
 * An AccessKey for an Alibaba Cloud account grants full access to all APIs and poses a high security threat. For better security, create and use a Resource Access Management (RAM) user for API calls or daily O&M. To create a RAM user, log on to the RAM console.
 * This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
 * To prevent key leaks, do not hardcode your AccessKey ID and AccessKey secret into your code.
 */
access_key_id = os.environ['NLP_AK_ENV']
access_key_secret = os.environ['NLP_SK_ENV']

# Create an AcsClient instance
client = AcsClient(
    access_key_id,
    access_key_secret,
    "cn-hangzhou"
)
request = GetWsCustomizedChGeneralRequest.GetWsCustomizedChGeneralRequest()
request.set_Text("YUNMAI Good light smart body fat scale precision Bluetooth scale weight measurement human body fat scale household health electron scale")
request.set_OutType("1")
request.set_ServiceCode("alinlp")
request.set_TokenizerId("GENERAL_CHN")
response = client.do_action_with_exception(request)
resp_obj = json.loads(response)
print(resp_obj)

Note: In GetWsCustomizedChGeneralRequest, GetWsCustomizedChGeneral is the action name of the algorithm. Replace it with the value for your desired algorithm and update the corresponding parameters. You can find this value under the example for the Action request parameter in the API reference document.

Call using a common request

Common request documentation

import json
import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

/**
 * An AccessKey for an Alibaba Cloud account grants full access to all APIs and poses a high security threat. For better security, create and use a Resource Access Management (RAM) user for API calls or daily O&M. To create a RAM user, log on to the RAM console.
 * This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
 * To prevent key leaks, do not hardcode your AccessKey ID and AccessKey secret into your code.
 */
access_key_id = os.environ['NLP_AK_ENV']
access_key_secret = os.environ['NLP_SK_ENV']

# Create an AcsClient instance
client = AcsClient(
    access_key_id,
    access_key_secret,
    "cn-hangzhou"
)

request = CommonRequest()

# The domain and version are static fields.
request.set_domain('alinlp.cn-hangzhou.aliyuncs.com')
request.set_version('2020-06-29')

# Find the action name in the API document.
request.set_action_name('GetWsCustomizedChGeneral')

# Find the parameters to add in the API document.
request.add_query_param('ServiceCode', 'alinlp')
request.add_query_param('Text', 'YUNMAI Good light smart body fat scale precision Bluetooth scale weight measurement human body fat scale household health electron scale')
request.add_query_param('TokenizerId', 'GENERAL_CHN')
response = client.do_action_with_exception(request)
resp_obj = json.loads(response)
print(resp_obj)

Troubleshoot call exceptions

If an exception occurs during a call, see Troubleshoot call exceptions (error code summary) and find the corresponding description in the table. The description explains the specific cause of the error and provides the solution.