All Products
Search
Document Center

Artificial Intelligence Recommendation:SDK for Python

Last Updated:Nov 07, 2024

This topic describes how to install and use the Python SDK for Artificial Intelligence Recommendation (AIRec) in detail. The main content includes how to call AIRec to create an instance, view instance details by specifying the instance ID, and start a full data import task for the instance. Complete integration steps are also provided.

Prerequisites

Step 1: Import the SDK

Alibaba Cloud SDK supports both generic and specialized methods to call OpenAPI. For more information, see Generic calls and specialized calls. Different methods require different SDKs.

Specialized calls

You can visit OpenAPI Explorer, find the service that you want to use, and view the supported languages and installation methods. Then, import the SDK of the service to your project. The steps to obtain the SDK in this example are as follows:

  1. Visit Artificial Intelligence Recommendation.

  2. In the Languages section, select the SDK language you need.

  3. Select the Installation Method you need, and copy the code to your project.

  4. Add the dependency to your project.

Run the following command to install the Python SDK:

pip install alibabacloud_airec20201126==1.0.0

Generic calls

Generic calls do not depend on any specific SDK, but only on the core package com.aliyun.tea-openapi. Run the following command to install the core package for Python. For the latest version, see tea-openapi.

pip install alibabacloud-tea-openapi

Step 2: Initialize the client

Make sure that you correctly specify the endpoint of service based on the region where AIRec is located. For more information about, see Regions and endpoints.

The following example uses specialized call code to describe the calling process in detail. If you choose the generic call method, see Generic calls and specialized calls for more information.

Use an AccessKey to initialize

Note

The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey Secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources that belong to your account may be compromised.

In this example, the AccessKey pair is saved in the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.

import os
import sys
from typing import List
from alibabacloud_airec20201126.client import Client as Airec20201126Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


@staticmethod
def create_client() -> Airec20201126Client:
        config = open_api_models.Config(
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.,
            access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.,
            access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
        )
        # For more information about the Endpoint, see https://api.aliyun.com/product/Airec
        config.endpoint = f'airec.cn-hangzhou.aliyuncs.com'
        return Airec20201126Client(config)

Step 3: Call the AIRec API by using the initialized client

Note

After the client is initialized, you can call the AIRec API by using the client. For more information, see List of operations by function.

API name:CreateInstance

This API is used to create a subscription instance of AIRec. You do not need to specify parameters when you call this API. You need to configure the runtime settings based on your business requirements. You can also customize the runtime settings to meet specific requirements.

// Runtime settings
runtime = util_models.RuntimeOptions()  

The following sample code shows how to use an AccessKey to create a subscription instance of AIRec:

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys
from typing import List
from alibabacloud_airec20201126.client import Client as Airec20201126Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> Airec20201126Client:
    
        config = open_api_models.Config(
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.,
            access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.,
            access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
        )
        # For more information about the Endpoint, see https://api.aliyun.com/product/Airec
        config.endpoint = f'airec.cn-hangzhou.aliyuncs.com'
        return Airec20201126Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        runtime = util_models.RuntimeOptions()
        headers = {}
        try:
            # Write your code to print the response of the API operation if necessary.
            client.create_instance_with_options(headers, runtime)
        except Exception as error:
            # Handle exceptions with caution in your actual business scenario and never ignore exceptions in your project.
            # Report an error message.
            print(error.message)
            # Show the URL for troubleshooting.
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        runtime = util_models.RuntimeOptions()
        headers = {}
        try:
            # Write your code to print the response of the API operation if necessary.
            await client.create_instance_with_options_async(headers, runtime)
        except Exception as error:
            # Handle exceptions with caution in your actual business scenario and never ignore exceptions in your project.
            # Report an error message.
            print(error.message)
            # Show the URL for troubleshooting.
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])

API name:DescribeInstance

This API is used to view the details of an instance by specifying the instance ID. You need to configure the parameters and runtime settings based on your business requirements. You can also customize the runtime settings to meet specific requirements.

// Runtime settings
runtime = util_models.RuntimeOptions()
headers = {}
            // Set parameters when you call the API
            client.describe_instance_with_options('airec-cn-****', headers, runtime)

The following sample code shows how to use an AccessKey to view the details of an instance by specifying the instance ID:

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys

from typing import List

from alibabacloud_airec20201126.client import Client as Airec20201126Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> Airec20201126Client:
     
        config = open_api_models.Config(
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.,
            access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.,
            access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
        )
        # For more information about the Endpoint, see https://api.aliyun.com/product/Airec
        config.endpoint = f'airec.cn-hangzhou.aliyuncs.com'
        return Airec20201126Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        runtime = util_models.RuntimeOptions()
        headers = {}
        try:
            # Write your code to print the response of the API operation if necessary.
            client.describe_instance_with_options('airec-cn-****', headers, runtime)
        except Exception as error:
            # Handle exceptions with caution in your actual business scenario and never ignore exceptions in your project.
            # Report an error message.
            print(error.message)
            # Show the URL for troubleshooting.
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        runtime = util_models.RuntimeOptions()
        headers = {}
        try:
            # Write your code to print the response of the API operation if necessary.
            await client.describe_instance_with_options_async('airec-cn-****', headers, runtime)
        except Exception as error:
            # Handle exceptions with caution in your actual business scenario and never ignore exceptions in your project.
            # Report an error message.
            print(error.message)
            # Show the URL for troubleshooting.
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])

API name:RunInstance

This API is used to start an instance and begin a full data import task. You need to configure the parameters and runtime settings based on your business requirements. You can also customize the runtime settings to meet specific requirements.

 // Runtime settings
 runtime = util_models.RuntimeOptions()
 headers = {}
            // Set parameters when you call the API
            client.run_instance_with_options('airec-cn-****', headers, runtime)

The following sample code shows how to use an AccessKey to start an instance and begin a full data import task by specifying the instance ID:

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys

from typing import List

from alibabacloud_airec20201126.client import Client as Airec20201126Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> Airec20201126Client:
     
        config = open_api_models.Config(
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.,
            access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
            # Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.,
            access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
        )
        # For more information about the Endpoint, see https://api.aliyun.com/product/Airec
        config.endpoint = f'airec.cn-hangzhou.aliyuncs.com'
        return Airec20201126Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        runtime = util_models.RuntimeOptions()
        headers = {}
        try:
            # Write your code to print the response of the API operation if necessary.
            client.run_instance_with_options('airec-cn-****', headers, runtime)
        except Exception as error:
            # Handle exceptions with caution in your actual business scenario and never ignore exceptions in your project.
            # Report an error message.
            print(error.message)
            # Show the URL for troubleshooting.
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        client = Sample.create_client()
        runtime = util_models.RuntimeOptions()
        headers = {}
        try:
            # Write your code to print the response of the API operation if necessary.
            await client.run_instance_with_options_async('airec-cn-****', headers, runtime)
        except Exception as error:
            # Handle exceptions with caution in your actual business scenario and never ignore exceptions in your project.
            # Report an error message.
            print(error.message)
            # Show the URL for troubleshooting.
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])