Callback API

更新时间:
复制 MD 格式

PAI-Rec's built-in callback API captures request parameters, user features, and item features at the exact moment of each recommendation request. Use these logs for data analytics, offline model training, or online learning.

Offline methods — such as associating features by time windows — are unreliable because latency between system links is hard to estimate, causing feature leakage. The callback API solves this by recording features at the source: when the recommendation request reaches the recommendation service, the system logs the request ID, user features, and item features to a message queue (DataHub or Kafka), then syncs the data to MaxCompute (ODPS).

Callback API flow diagram

Prerequisites

Before you begin, make sure you have:

  • A PAI-Rec deployment with the DPI engine configured

  • A DataHub project and the endpoint URL for your data source

  • (Optional) An EasyRec model, if your callback flow uses model-generated features

API reference

Endpoint

POST /api/callback

Request parameters

ParameterTypeRequiredDescriptionConstraintsExample
scene_idstringYesThe scene name for which to log datahomepage
uidstringYesThe user registration ID85578510
request_idstringYesA unique identifier for the recommendation requestd9cb1c8d***
item_listjson listYesThe list of recommended item IDsEach item must include item_id[{"item_id":"99886867"}, {"item_id":"99888623"}]
featuresjson stringNoUser features at the time of the requestValid JSON string{"age":25, "city":"beijing"}
request_infojson stringNoAdditional request informationValid JSON string{"recom_id":"12334234"}

Request example

curl 'http://host/api/callback' \
  -d '{"uid":"84603208","request_id":"d9cb1c8d-4d3f-491b-9ea3-380481dabde3","scene_id":"homepage","features":{"age":25, "city":"beijing"},"item_list":[{"item_id":"113939841"},{"item_id":"113764910"}],"request_info":{"recom_id":"1111111"}}'

Response

{
  "code": 200,
  "msg": "success"
}
FieldTypeDescription
codeintegerHTTP status code. 200 indicates success.
msgstringStatus message. success indicates the callback was recorded.

Configure the callback

Callback configuration has two parts: the CallBackConfs block that defines the logging behavior, and the DatahubConfs block that defines where to write the data.

CallBackConfs

"CallBackConfs": {
  "home_feed": {
    "DataSource": {
      "Name": "pairec_callback_dh",
      "Type": "datahub"
    },
    "RankConf": {
      "RankAlgoList": [
        "ali_rnk_v2_woid_callback_public_v2"
      ],
      "ContextFeatures": [
        "none"
      ],
      "Processor": "EasyRec"
    },
    "RawFeatures": false,
    "RawFeaturesRate": 0,
    "ItemSize": 100,
    "ItemSizeRate": 10,
    "UseUserFeatures": true
  }
}
ParameterDescriptionDefaultConstraints
home_feed (scene name)The scene for which to log dataMust match a scene name in SceneConfs
DataSource.TypeThe message queue typeCurrently only datahub is supported
DataSource.NameThe DataHub data source nameMust match a key in DatahubConfs
RankConfThe model configuration, identical to the DPI engine model configOmit this field if no model-generated features are used
RawFeaturesSpecifies whether to record raw item features from the EasyRec modelfalseSet to true to enable; must also set RawFeaturesRate
RawFeaturesRateSampling ratio for raw features0Integer from 0 to 100. Takes effect only when RawFeatures is true.
ItemSizeMaximum number of items to process per callbackAll items from the recommendation flowItems are truncated to the first ItemSize items before sampling
ItemSizeRateSampling ratio applied after ItemSize truncationInteger from 1 to 100. If both ItemSize and ItemSizeRate are set, the system first truncates to ItemSize items, then samples by ItemSizeRate.
UseUserFeaturesSpecifies whether to use user features from the recommendation flow when AutoInvokeCallBack is enabledWhen set to true, no separate user feature retrieval in FeatureConfs is needed for the callback flow

DatahubConfs

PAI-Rec automatically creates the DataHub topic based on the topic name and schema — no manual topic creation is required.

"DatahubConfs": {
  "pairec_callback_dh": {
    "Endpoint": "http://dh-cn-hangzhou-int-vpc.aliyuncs.com",
    "ProjectName": "${ProjectName}",
    "TopicName": "pairec_callback_log",
    "Schemas": [
      {"Field": "request_id",        "Type": "string"},
      {"Field": "module",            "Type": "string"},
      {"Field": "scene",             "Type": "string"},
      {"Field": "request_time",      "Type": "integer"},
      {"Field": "user_features",     "Type": "string"},
      {"Field": "item_features",     "Type": "string"},
      {"Field": "request_info",      "Type": "string"},
      {"Field": "user_id",           "Type": "string"},
      {"Field": "item_id",           "Type": "string"},
      {"Field": "raw_features",      "Type": "string"},
      {"Field": "generate_features", "Type": "string"},
      {"Field": "context_features",  "Type": "string"}
    ]
  }
}

Replace ${ProjectName} with your DataHub project name.

Feature loading configuration

Feature loading for the callback flow follows the same structure as standard feature configuration. The only difference is the scene alias: use {scene_name}_callback as the scene name in FeatureConfs. For example, for the scene home_feed, use home_feed_callback.

"FeatureConfs": {
  "home_feed_callback": {
    "AsynLoadFeature": true,
    "FeatureLoadConfs": [
      {
        "FeatureDaoConf": {
          "AdapterType": "hologres",
          "HologresName": "pairec-holo",
          "FeatureKey": "user:uid",
          "UserFeatureKeyName": "client_str",
          "HologresTableName": "dwd_ali_user_all_feature_v2_holo",
          "UserSelectFields": "*",
          "FeatureStore": "user"
        },
        "Features": [
          {
            "FeatureType": "new_feature",
            "FeatureName": "day_h",
            "Normalizer": "hour_in_day",
            "FeatureStore": "user"
          },
          {
            "FeatureType": "new_feature",
            "FeatureName": "week_day",
            "Normalizer": "weekday",
            "FeatureStore": "user"
          },
          {
            "FeatureType": "new_feature",
            "FeatureName": "rand_int_v",
            "Normalizer": "random",
            "FeatureStore": "user"
          }
        ]
      },
      {
        "FeatureDaoConf": {
          "AdapterType": "hologres",
          "HologresName": "pairec-holo",
          "FeatureKey": "user:uid",
          "UserFeatureKeyName": "client_str",
          "HologresTableName": "dwd_ali_user_table_v3_expo_static_feature_v2_holo",
          "UserSelectFields": "*",
          "FeatureStore": "user"
        }
      }
    ]
  }
}

Log format

Each callback request contains one user ID and a list of items. Because user features can be large, user and item features are written as separate log records to the same DataHub topic, distinguished by the module field.

User feature log

FieldTypeDescription
modulestringAlways "user" — identifies this as a user feature record
request_idstringThe request ID from the callback request
scenestringThe scene name from the callback request
request_timeintegerThe Unix timestamp of the request
user_idstringThe user ID
user_featuresstringUser features as a JSON string
request_infostringAdditional request information from the callback request

Item feature log

FieldTypeDescription
modulestringAlways "item" — identifies this as an item feature record
request_idstringThe request ID from the callback request
scenestringThe scene name from the callback request
request_timeintegerThe Unix timestamp of the request
user_idstringThe user ID
item_idstringThe item ID
item_featuresstringItem features as a JSON string
raw_featuresstringRaw features returned by the EasyRec model
generate_featuresstringFeature Generation (FG) output from the EasyRec model
context_featuresstringContext features returned by the EasyRec model

Enable automatic scene callback

By default, the callback is a separate service that you call manually after each recommendation request. The DPI engine also supports automatic callback invocation: after a recommendation request completes, the system calls the callback flow automatically.

To enable automatic callbacks, set AutoInvokeCallBack to true in SceneConfs:

"SceneConfs": {
  "${scene_name}": {
    "default": {
      "RecallNames": [
        "collaborative_filter"
      ],
      "AutoInvokeCallBack": true,
      "AutoInvokeCallBackRate": 100
    }
  }
}

Replace ${scene_name} with the actual scene name, such as home_feed.

ParameterDescriptionDefaultConstraints
AutoInvokeCallBackSpecifies whether to automatically call the callback flow after each recommendation requestSet to true to enable
AutoInvokeCallBackRatePercentage of recommendation requests that trigger an automatic callbackInteger from 1 to 100. If set to 0 or omitted, all traffic triggers a callback.

What's next