All Products
Search
Document Center

Artificial Intelligence Recommendation:Obtain recommendation results

Last Updated:Sep 18, 2023

Overview

This topic describes how to obtain recommendation results from the Artificial Intelligence Recommendation (AIRec) service. If you call an API operation to obtain recommendation results, the data is returned in the JSON format.

The content in this topic is applicable to the AIRec instances of Industry Operation Edition and Algorithm Configuration Edition.

Parameters

Parameter

Type

Description

Required

Remarks

userId

string

The ID of the user who requires the recommendation service.

You cannot leave this parameter and the imei parameter empty at the same time.

If the user ID does not exist in the user table, personalized recommendations cannot be provided.

imei

string

The ID of the user device. The ID is an International Mobile Equipment Identity (IMEI) value for an Android device or an Identifier for Advertisers (IDFA) value for an iOS device.

You cannot leave this parameter and the userId parameter empty at the same time. This parameter is required for unregistered users.

The value must be a 32-bit Message-Digest Algorithm 5 (MD5) value.

returnCount

Integer

The number of recommendation entries that are returned for a single request.

Yes

We recommend that you set this parameter to 20.

sceneId

string

The ID of the scenario in which the item is to be recommended.

Yes

Default value: 1. Set this parameter to the scenario ID that is contained in the uploaded data.

items

string

The benchmark items in a scenario whose recommendation service type is set to related recommendations.

This parameter is required only if the recommendation service type of the scenario is set to related recommendations.

The value must be in the format of itemId:itemType,itemId:itemType.

ip

string

The IP address used for the last logon.

No

-

rankOpen

boolean

Specifies whether to perform personalized ranking based on the user ID in the filtering and ranking phases.

No

Default value: true.

recType

string

The type of the recommendation service.

No

The value hot indicates popular recommendations, the value rr indicates related recommendations, and the value gul indicates the recommendations that you may like. If you set the recType parameter to rr or gul, the personalized recommendation feature is enabled. The personalized recommendation feature is disabled only if you set the rankOpen parameter to false and the recType parameter to hot.

filter

string

The filter condition for recommendations.

No

For more information about how to use the recommendation filtering feature, see Use the recommendation filtering feature to customize the filtering of feed streams.

strategy

string

Specifies whether to enable the features of pinning items to the top and recommending items in fixed positions.

No

If the value of the strategy parameter is set to fixedSlot in the initial request for accessing a page, the recommendation results for the items that are pinned to the top and recommended in fixed positions are returned. By default, the default algorithm is used for recommendations.

clearExposure

string

Specifies whether to clear records about exposed items.

No

If the value of the clearExposure parameter is set to true, the records about exposed items for the current user are cleared. The user is allowed to view the items that have been exposed, and AIRec starts to record the exposed items again. If you do not need to clear the records about exposed items, leave this parameter empty.

Common errors that may occur if you use server SDKs to obtain recommendation results

Causes of an empty recommendation result:

1. The values of specific request parameters are invalid. Make sure that the value you specified for the scene_id parameter is contained in the uploaded data.

Make sure that the value you specified for the user_id parameter is contained in the user table. If the value you specified for the user_id parameter does not exist in the user table, a new user is created.

Make sure that the number of recommended items is less than the number of items that can be recommended in the specified scenario.

Note: A maximum of 50 recommended items can be returned if a server SDK is used to obtain recommendation results. In a scenario whose recommendation service type is set to related recommendations, you must specify the existing item in the format of item_id:item_type, as shown in the following figure.相关推荐请求参数2. Only a small number of items can meet the recommendation conditions. As a result, after the exposure blocking feature is enabled, no available items can be recommended to users and the recommendation result is empty.

Sample code

package com.aliyun.airec;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.airec.model.v20181012.RecommendRequest;
import com.aliyuncs.airec.model.v20181012.RecommendResponse;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class Recommend {

    public static void main(String args[]) {
        // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, 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 within your account may be compromised. 

        // In this example, the AccessKey ID and AccessKey secret are stored in the environment variables to implement identity verification. 
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        DefaultProfile.addEndpoint("cn-hangzhou", "Airec", "airec.cn-hangzhou.aliyuncs.com");

        DefaultAcsClient client = new DefaultAcsClient(profile);

        RecommendRequest request = new RecommendRequest();

        // You must set the following parameters: 
        request.setInstanceId("airec-xxx");
        request.setReturnCount(10);
        request.setSceneId("Scenario ID");

        // At least one of the following parameters must be reported: 
        request.setUserId("0");
        request.setImei("0");

        // This parameter is optional if the recommendation service type of the scenario is set to related recommendations.
        //request.setItems("88:article,33:image");

        // Set the rankOpen parameter to false and the recType parameter to hot if you need to disable the personalized recommendation feature.
        //request.setRankOpen(false);
        //request.setRecType("hot");

        request.setAcceptFormat(FormatType.JSON);

        try {
            RecommendResponse response = client.getAcsResponse(request);

            for (RecommendResponse.ResultItem item : response.getResult()) {
                System.out.println(item.getItemId());
                System.out.println(item.getItemType());
                System.out.println(item.getTraceId());
                System.out.println(item.getTraceInfo());
                System.out.println(item.getMatchInfo());
                System.out.println(item.getWeight());
                System.out.println(item.getPosition());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Demo for pushing data and obtaining recommendation results

For more information, see airec-demo.