PAI-Rec A/B service integration
PAI-Rec provides a lightweight A/B Service for rapid experimentation with recall strategies, ranking models, and model parameters, with minimal impact on your existing system. The service currently supports server-side experiments.
A/B service overview
The A/B Service includes the following components:
AB Web Console: A backend management system for configuring experiments. Data is persisted to a MySQL database.
AB Server: An HTTP API service deployed on PAI-EAS. It reads data from the MySQL database, so PAI-EAS must have direct network access to the database. For more information, see Configure network access.
AB SDK: A software development kit that you integrate into your server-side application. It supports experiment configuration, traffic allocation, traffic routing, and experiment matching. It currently supports Go, Python, and Java.
Integrate A/B Service experiment features
Procedure
Select an environment
Set the environment for the engine service to daily, staging, or product in one of two ways:
Set RunMode in the config.json file.
Set the PAIREC_ENVIRONMENT environment variable. This setting takes precedence over the setting in config.json.
The staging engine service loads experiment information from the staging environment. Ensure that the environment variables are consistent.
Configure experiment parameters
PAI-Rec predefines a set of experiment parameters. You must follow the naming rules exactly. Otherwise, even if a request matches an experiment, the service cannot find the corresponding parameters.
Recall parameters
To reference existing recalls, use Category+".RecallNames", where Category defaults to default.
{
"default.RecallNames": [
"hot_group_recall",
"hot_global_recall"
]
}Note: The recall names referenced here must be defined in your configuration file under RecallConfs.
Filter parameters
{
"filterNames": [
"UniqueFilter",
"UserExposureFilter"
]
}Note: Except for UniqueFilter, the filter names referenced here must be defined in your configuration file under FilterConfs. Multiple filter modules are executed in sequence.
Ranking and feature parameters
Different ranking models are often trained with different feature sets, so these two types of parameters are frequently used together.
{
"rankconf": {
"RankAlgoList": [
"pai_homepage_fm"
],
"RankScore": "${pai_homepage_fm}",
"Processor": "EasyRec"
},
"features.scene.name": "xxx",
"user_features.scene.name": "xxx"
}When the model service is EasyRec, you must also set the Processor field in rankconf to EasyRec.
If you only want to adjust the RankScore parameter in an experiment, use the following simple configuration:
{
"rankscore": "xxx"
}Note: The values in RankAlgoList must be defined in your engine configuration file under AlgoConfs to be available for reference. You only need to use one of the features.scene.name or user_features.scene.name parameters, depending on whether your feature configurations are defined in FeatureConfs or UserFeatureConfs.
Re-ranking parameters
You can configure re-ranking parameters in two ways: reference an existing configuration from your configuration file, or override its parameters.
To reference an existing re-ranking configuration, use
Category+".SortNames", where Category defaults todefault.{ "default.SortNames": [ "ItemRankScore", "BoostScoreSort" ] }
Note: Except for ItemRankScore, the re-ranking names referenced here must be defined in your configuration file under SortConfs. Multiple re-ranking modules are executed in sequence.
To override an existing re-ranking configuration, use
"sort.${SortName}". For example, assume a boost score re-ranking module (BoostScoreSort) is defined in your engine configuration. If you want to adjust some of its parameters in an experiment, use the following configuration. This example shows the re-ranking definition in the engine configuration file.{ "SortConfs": [ { "Name": "BoostScoreSort", "SortType": "BoostScoreSort", "Debug": false, "BoostScoreConditions": [ { "Conditions": [ { "Name": "sex", "Domain": "item", "Type": "string", "Value": "gender", "Operator": "equal" } ], "Expression": "score * 2" } ] } ] }If you want to modify this re-ranking configuration in an experiment, for example, to adjust
ConditionsandExpression, you can use the following experiment configuration:{ "sort.${SortName}": { "BoostScoreConditions": [ { "Conditions": [ { "Name": "age", "Domain": "item", "Type": "int", "Value": "18", "Operator": "equal" } ], "Expression": "score * 10" } ] } }NoteNote: Replace
${SortName}in the configuration withBoostScoreSort. Requests routed to this experiment will use the re-ranking strategy defined in the experiment.
Coarse ranking parameters
{
"generalRankConf": {
"FeatureLoadConfs": [
{
"FeatureDaoConf": {}
}
],
"RankConf": {},
"ActionConfs": []
}
}The coarse ranking parameters in an experiment are almost identical to the coarse ranking configuration in the engine configuration file. For more information, see Coarse ranking configuration.
Traffic control parameters
Traffic control is a type of re-ranking (Sort). To experiment on the traffic control module as a whole, use re-ranking parameters. For example:
{
"default.SortNames": [
"TrafficControlSort" // Note: This parameter must be defined under SortConfs in the engine configuration file.
]
}However, traffic control can involve multiple tasks, each with multiple targets. A single master switch makes it difficult to observe the effect of each individual task. For fine-grained control, use experiment parameters to manage each task and target individually:
{
"pid_task_params":{
"${task_id}" : {
"turn_off":false,
"kp": 5,
"ki": 1,
"kd": 1
}
},
"pid_target_params":{
"${target_id}" :{
"turn_off":false,
"kp": 5,
"ki": 1,
"kd": 1
}
}
}The entire configuration is a JSON object. You can include both pid_task_params and pid_target_params, or just one of them.
The pid_task_params block allows you to manage the online status and PID parameters for each control task.
The pid_target_params block allows you to manage the online status and PID parameters for each control target.
If turn_off is true, the corresponding control task or target is disabled. If false, it is enabled.
If experiment parameters conflict, they are prioritized in the following order: pid_target_params (highest), pid_task_params, and then the engine configuration file parameters.
Note: Replace ${task_id} and ${target_id} with their actual values.
Match experiments
For each recommendation request, match the experiment and build the context. For example:
func (c *HomeFeedController) makeRecommendContext() {
c.context = context.NewRecommendContext()
c.context.Size = c.param.Limit
c.context.Param = &c.param
c.context.RecommendId = c.RequestId
c.context.Config = recconf.Config
c.context.Debug = c.param.Debug
abcontext := model.ExperimentContext{
Uid: c.param.DistinctId,
RequestId: c.RequestId,
FilterParams: map[string]interface{}{},
}
if abtest.GetExperimentClient() != nil {
c.context.ExperimentResult = abtest.GetExperimentClient().MatchExperiment(c.param.SceneId, &abcontext)
log.Info(c.context.ExperimentResult.Info())
}
}Retrieve experiment parameters
Retrieve experiment parameters from the RecommendContext object through context.ExperimentResult. The GetLayerParams method returns parameters from a specific layer and supports Get, GetInt, GetFloat, and GetInt64 functions. The first argument is the parameter name, and the second is the default value returned if the parameter is not found.
count := r.recallCount
if context.ExperimentResult != nil {
count = context.ExperimentResult.GetLayerParams("").GetInt("recall.base.count", count)
}
fmt.Println("test count", count)Run recall experiments
To experiment on a specific recall, dynamically adjust its configuration with parameters. Follow these rules:
The parameter name must be in the format:
"recall."+<existing_recall_name>.The parameter configuration must be a JSON object.
The relevant recall instance must implement the CloneWithConfig method, which generates a new recall instance from the provided parameters. For example:
type MyRecall struct { version string } func NewMyRecall() *MyRecall { r := MyRecall{version: "v1"} return &r } func (m *MyRecall) CloneWithConfig(params map[string]interface{}) *MyRecall { r := MyRecall{} if _, ok := params["version"]; ok { r.version = params["version"].(string) } return &r }
Integrate A/B Service parameter features
Use this feature as follows:
// Get the specific scene name.
scene := context.GetParameter("scene").(string)
// Retrieve the parameter list by scene, and then use a Get* function to read the specific parameter value.
count := abtest.GetParams(scene).GetInt("count", 100)
fmt.Println("recall count:", count)Obtain the SDK
You can select and integrate the PAI-Rec Config SDK based on the programming language of your project. The following SDKs are supported:
Java SDK: aliyun-pairec-config-java-sdk
Go SDK: aliyun-pairec-config-go-sdk