Getting started
Installation command
You can use `pairecmd` to quickly create projects. Version 2 supports the PAI-Rec console. We recommend using the console.
Version 2
Download links:
After the download is complete, set the executable file permission on a UNIX-like system by running the following command: chmod +x pairecmd
Create a project
To create a project named `pairec-demo`, run the following command. This command generates a `pairec-demo` folder that contains the project code in the current directory.
./pairecmdmac project --name pairec-demoThe generated folder has the following structure:
pairec-demo
├── Makefile
├── conf
│ └── config.json.production
├── docker
│ └── Dockerfile
├── go.mod
└── src
├── controller
│ └── feed.go
└── main.goNavigate to the `pairec-demo` folder and run the following command:
go mod tidyCompile and package the project
The project provides a Makefile. To compile the project, run the following command:
make && make buildIf an error occurs, first run the following command:
go mod tidyTo package the project into an image, run the following command:
make releaseYou can modify the Makefile as needed.

Note: You can activate a personal image using Alibaba Cloud Container Registry and then create the image repository specified by `BIN_NAME`.
Run the project
Navigate to the `pairec-demo` folder and run the following command to start the service. The logs are printed to the terminal.
Use the `config` parameter to specify the path of the configuration file.
go run src/main.go --config=conf/config.json.production --alsologtostderrTo test the API, open another terminal and run the following command:
curl -v http://localhost:8000/api/rec/feed -d '{"uid":"76295990", "size":10, "scene_id":"home_feed"}'For more information about the `config` parameter, see Engine configuration.
API testing
The generated project includes a built-in recommendation API. For more information about the implementation, see the `controller/feed.go` file. The API is defined as follows:
API name
/api/rec/feed
Request parameters
Parameter | Description | Type | Required | Example |
uid | The user ID. | string | Yes | "1000079" |
size | The number of items to retrieve. | integer | Yes | 10 |
scene_id | The scenario ID. | String | Yes | home_feed |
features | The context features. | json map | No | {"age":20, "sex":"male"} |
complex_type_features | The context features of complex types. Set this parameter to include type information in requests sent to the model service. | json array | No | [{"name":"age", "type":"int", "values":20}, {"name":"sex", "values":"male", "type":"string"}] |
item_id | The item ID for similarity recommendations. | string | No | 248791390 |
item_list | A custom list for recall. | json array | No | [{"item_id":"1111", "score":1},{"item_id":"222", "score":0.95}] |
debug | Specifies whether to enable the debug mode to print more logs. | bool | No | true |
request_id | The unique ID of the request. If you do not specify this parameter, the PAI-Rec engine automatically generates one. If you specify this parameter, the value is used as the request ID. | string | No | "c46c3f5e-6b32-4b5c-8aac-59a319941248" |
If you have a batch of recall data to pass through the API, assign it to the `item_list` parameter. The `item_list` parameter is an array where each item is a map object. Each item must contain the `item_id` field, which specifies the item ID. All other fields are optional. If an item contains the `score` field, its value is used as the recall score. All other fields are processed as item properties.
complex_type_features
Context features of complex types must be passed as an array. Each element contains the following information:
Name | Description | Example |
name | The name of the context feature. | age |
type | The type of the context feature. | int |
values | The value of the context feature. You can set various types, such as specific values, arrays, and maps. | 20 |
Examples:
Pass a single value:
[{"name":"age", "type":"int", "values":20}, {"name":"sex", "values":"male", "type":"string"}]Pass an array:
[{"name":"list_features", "type":"list<int>", "values":[1,2,3]}]Pass a map:
[{"name":"map_features", "type":"map<int,int>", "values":{"1":10,"2":20,"3":30}}], or[{"name":"map_features", "type":"map<int,int>", "values":{"1":"10","2":"20","3":"30"}}],
The `type` parameter supports the following types: int, int64, float, double, string, list<int> , list<int64> , list<float>, list<double>, list<string>, map<int,int>, map<int,int64>, map<int,float>, map<int,double>, map<int,string>, map<string,int>, map<string,int64>, map<string,float>, map<string,double>, map<string,string>, map<int64,int>, map<int64,int64>,map<int64,float>, map<int64,double>, and map<int64,string>.
You can use the `complex_type_features` and `features` parameters at the same time.
API response
Name | Description | Type | Example |
code | The business code returned for the API call. | int | 200 |
msg | The business information. | string | success |
request_id | The unique ID of the request. | string | e332fe9c-7d99-45a8-a047-bc7ec33d07f6 |
size | The number of recommended items returned. | int | 10 |
experiment_id | The experiment ID. If no A/B testing is performed, an empty string is returned. | string | ER2_L1#EG1#E2 |
items | A list of recommended items. | json array | [{"item_id":"248791390","score":0.9991594902203332,"retrieve_id":"mock_recall"}] |
The following table describes the parameters in an item.
Name | Description | Type | Example |
item_id | The ID of the recommended item. | string | 3v5RE7417j7R |
retrieve_id | The ID of the recall source. | string | u2i_recall |
score | The recommendation score. | float | 0.45 |
extra | The custom output fields. This parameter is not returned if it is not specified. | json map | For more information, see the description of custom output fields. |
Error codes
Error code | Description | msg |
200 | The API call is successful. | success |
299 | The number of returned items is insufficient. | items size not enough |
400 | A parameter error occurred. The description in the `msg` field varies based on the error type. | uid not empty or unexpected end of JSON input |
500 | A server error occurred. An HTTP status code is returned. |
Request sample
curl -v http://host/api/rec/feed -d '{"uid":"76295990", "size":10, "scene_id":"home_feed"}'Response data
{
"code":200,
"msg":"success",
"request_id":"e332fe9c-7d99-45a8-a047-bc7ec33d07f6",
"size":10,
"experiment_id":"",
"items":[
{
"item_id":"248791390",
"score":0.9991594902203332,
"retrieve_id":"mock_recall"
},
...
]
}Output custom fields
By default, the `item_id`, `retrieve_id`, and `score` fields are returned for each item. To output additional custom fields, such as item properties or model scores, you can customize the output fields for a specific scenario in `SceneConfs`. To do this, define the configuration in `OutputFields`. The API then returns the custom fields in the `extra` field.
The following code provides a configuration example:
"SceneConfs": {
"${scene_name}": {
"default": {
"RecallNames": [
"collaborative_filter"
],
"OutputFields": [
"item:type",
"item:age",
"score:*",
"score:model_v1_ctr"
]
}
}
}A field that starts with `item:` outputs the corresponding item property. If the property does not exist, `null` is returned.
A field that starts with `score:` outputs the score returned by the model.
`score:*` outputs all model scores. The scores are placed in `algo_scores`.
FAQ
Q: How do I pass an item list through the recommendation API for re-sorting?
Answer: You can pass the item list using the `item_list` API parameter. Then, in the engine configuration, set up a Context Item Recall (ContextItemRecall). This configuration allows the PAI-Rec engine to read the item list from the `item_list` parameter.
Q: How do I instrument the client to record PAI-Rec data and use experiment reports?
Answer: The recommendation API returns two fields: `request_id` and `experiment_id`. You must instrument your client to record these fields. At a minimum, you must include these fields in the impression and click behavior logs. We also recommend that you include them in other behavior logs, such as playback duration, add-to-cart actions, and purchases. If these other behaviors are not instrumented, the performance statistics may be affected. For more information, see the SQL use case in Data Registration and Field Configuration. You can generate the offline "Experiment Report Source Table", register it with the AB Test platform, and configure custom metrics. After the report data is calculated, you can view the experiment report in the Experiment Metrics Report.