All Products
Search
Document Center

Artificial Intelligence Recommendation:SDK for Python

Last Updated:Sep 06, 2023

Download URL

You can download Artificial Intelligence Recommendation (AIRec) SDK for Python at https://pypi.org/project/aliyun-python-sdk-airec.

You can view the source code of AIRec SDK for Python at https://github.com/aliyun/aliyun-openapi-python-sdk/tree/master/aliyun-python-sdk-airec.

Procedure

To use AIRec SDK for Python, perform the following steps:

Step 1: Create an Alibaba Cloud account

For more information, see Sign up with Alibaba Cloud. We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2: Obtain an Alibaba Cloud AccessKey pair

Before you use AIRec SDK for Python, you must apply for an Alibaba Cloud AccessKey pair.

Go to the AccessKey Pair page. Select an AccessKey pair for AIRec SDK for Python. If no AccessKey pair is available, create an AccessKey pair and make sure that it is in the enabled state. For more information about how to create an AccessKey pair, see Create an AccessKey pair.

Step 3: Install AIRec SDK for Python

To install AIRec SDK for Python, perform the following steps:

  1. Install the core library of AIRec SDK for Python.

    • If you are using Python 2.X, run the following command:

      pip install aliyun-python-sdk-core
    • If you are using Python 3.X, run the following command:

      pip install aliyun-python-sdk-core-v3
  2. Install AIRec SDK for Python.

Run the following command:

pip install aliyun-python-sdk-airec==1.1.3

Step 4: Use AIRec SDK for Python

You can perform the following steps to use AIRec SDK for Python:

  1. Create a client. When you create a client, you must provide the region ID, AccessKey ID, and AccessKey secret.

  2. Configure the endpoint.

  3. Create a request and set parameters.

  4. Initiate the request and handle the response or exception.

Push data

Note: For more information about the JSON format of the pushed data, see Push data.

#!/usr/bin/env python
#coding=utf-8
import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

# Set the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
client = AcsClient(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'], 'cn-hangzhou')

request = CommonRequest()
request.set_accept_format('json')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_domain('airec.cn-hangzhou.aliyuncs.com')
request.set_version('2018-10-12')

request.add_query_param('RegionId', "cn-hangzhou")
request.add_header('Content-Type', 'application/json')
request.set_uri_pattern('/openapi/instances/The ID of the AIRec instance/tables/The name of the table that is used to receive data/actions/bulk')
body = '''{}'''
request.set_content(body.encode('utf-8'))

response = client.do_action_with_exception(request)

# python2:  print(response) 
print(str(response, encoding = 'utf-8'))

Obtain recommendation results

Note: For more information about the response parameters for obtaining recommendation results and common errors, see Obtain recommendation results.

#!/usr/bin/env python
#coding=utf-8
import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

# Set the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
client = AcsClient(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'], 'cn-hangzhou')

request = CommonRequest()
request.set_accept_format('json')
request.set_method('GET')
request.set_protocol_type('https') # https | http
request.set_domain('airec.cn-hangzhou.aliyuncs.com')
request.set_version('2018-10-12')


request.add_query_param('SceneId', "110110")
request.add_query_param('UserId', "123")
request.add_query_param('ReturnCount', "10")
request.add_header('Content-Type', 'application/json')
request.set_uri_pattern('/openapi/instances/The ID of the AIRec instance/actions/recommend')



response = client.do_action_with_exception(request)

# python2:  print(response) 
print(str(response, encoding = 'utf-8'))