All Products
Search
Document Center

Artificial Intelligence Recommendation:SDK for Go

Last Updated:Sep 06, 2023

SDK for Go

Procedure

To use Artificial Intelligence Recommendation (AIRec) SDK for Go, 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 Go, you must apply for an Alibaba Cloud AccessKey pair. Go to the AccessKey Pair page. Select an AccessKey pair for AIRec SDK for Go. 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 the Go development environment

We recommend that you use Go 1.5 or later for AIRec SDK. You can visit the Go official website to download and install the Go development environment.

Step 4: Install AIRec SDK for Go

Obtain and install AIRec SDK for Go of the latest version.

go get github.com/aliyun/alibaba-cloud-sdk-go

AIRec-related protocol code is stored in the services/airec directory.

Step 5: Use AIRec SDK for Go

The following sample code describes how to use AIRec SDK for Go to create an instance.

Push data

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

package main

import (
    "os"
    "fmt"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

func main() {
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in AIRec 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 resources that belong to your account may be compromised. 

    // In this example, the AccessKey ID and AccessKey secret are stored in the environment variables to implement identity verification. 
    client, err := sdk.NewClientWithAccessKey("cn-hangzhou", os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
    if err != nil {
        panic(err)
    }

    request := requests.NewCommonRequest()
    request.Method = "POST"
    request.Scheme = "https" // https | http
    request.Domain = "airec.cn-hangzhou.aliyuncs.com"
    request.Version = "2018-10-12"
    request.PathPattern = "/openapi/instances/The ID of the AIRec instance/tables/The name of the table that is used to receive data/actions/bulk"
    request.Headers["Content-Type"] = "application/json"


    // Specify the JSON data that complies with the specifications in the body parameter.
    body := ``
    request.Content = []byte(body)

    response, err := client.ProcessCommonRequest(request)
    if err != nil {
        panic(err)
    }
    fmt.Print(response.GetHttpContentString())
}

Obtain recommendation results

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

package main

import (
    "os"
    "fmt"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

func main() {
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in AIRec 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 resources that belong to your account may be compromised. 

    // In this example, the AccessKey ID and AccessKey secret are stored in the environment variables to implement identity verification. 
    client, err := sdk.NewClientWithAccessKey("cn-hangzhou", os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
    if err != nil {
        panic(err)
    }

    request := requests.NewCommonRequest()
    request.Method = "GET"
    request.Scheme = "https" // https | http
    request.Domain = "airec.cn-hangzhou.aliyuncs.com"
    request.Version = "2018-10-12"
    request.PathPattern = "/openapi/instances/airec-xxx/actions/recommend"

    request.Headers["Content-Type"] = "application/json"

    // Specify the number of returned items for a single request.
    request.QueryParams["ReturnCount"] = "10"

    // Specify the ID of the user for the request.
    request.QueryParams["UserId"] = "1234"

    // Specify the scenario ID of the recommendation result for the request.
    request.QueryParams["SceneId"] = "test01"


    response, err := client.ProcessCommonRequest(request)
    if err != nil {
        panic(err)
    }
    fmt.Print(response.GetHttpContentString())
}