Getting started
Install command
You can use pairecmd to quickly create projects. Version 2 supports the PAI-Rec console (recommended).
Version 2
Download links:
After downloading, on Unix-like systems you must set the executable file. Command: chmod +x pairecmd
Create a project
Taking the creation of a project named pairec-demo as an example, after executing the command, a pairec-demo directory will be generated in the current directory. The pairec-demo directory contains the project code.
./pairecmdmac project --name pairec-demo
The folder has the following structure:
pairec-demo
├── Makefile
├── conf
│ └── config.json.production
├── docker
│ └── Dockerfile
├── go.mod
└── src
├── controller
│ └── feed.go
└── main.go
Enter the pairec-demo directory and execute the following command:
go mod tidy
Compile and package the project
The project provides a Makefile file. To compile the project, run the following command:
make && make build
If you encounter errors, you can first execute the following command:
go mod tidy
To package the project into an image, execute the following command:
make release
You can modify the Makefile file according to the actual situation.
Note: You can activate a personal image through an Alibaba Cloud image and then create an image repository specified by BIN_NAME.
Run the project
You can enter the pairec-demo directory and run the following command to start the service. The log will be output to the terminal.
Pass the configuration file path through the config parameter.
go run src/main.go --config=conf/config.json.production --alsologtostderr
You can test with the following API in another terminal:
curl -v http://localhost:8000/api/rec/feed -d '{"uid":"76295990", "size":10, "scene_id":"home_feed"}'
For specific configuration, see Engine configurations.
API testing
In the generated project, a recommendation API has already been implemented. For specific implementation, see controller/feed.go. The API is defined as follows:
API name
/api/rec/feed
Request parameters
Parameter | Parameter description | Type | Required | Example |
uid | User ID | string | Yes | "1000079" |
size | Number of items to retrieve | integer | Yes | 10 |
scene_id | Scene ID | String | Yes | home_feed |
features | Context features | json map | No | {"age":20, "sex":"male"} |
complex_type_features | Complex type context features. If you need to request the model service with type information, you must set this value. | json array | No | [{"name":"age", "type":"int", "values":20}, {"name":"sex", "values":"male", "type":"string"}] |
item_id | Item ID for similarity recommendation | string | No | 248791390 |
item_list | Custom retrieval list | json array | No | [{"item_id":"1111", "score":1},{"item_id":"222", "score":0.95}] |
debug | For debugging, print more logs | bool | No | true |
request_id | Unique request ID. If this parameter is empty, the PAI-Rec engine will automatically generate it. If the user provides it, the parameter value will be used as the request ID. | string | No | "c46c3f5e-6b32-4b5c-8aac-59a319941248" |
When there is a batch of retrieval data that you want to pass through the API, you can assign it to item_list. item_list is an array, and each item is a map object. The item must have an item_id field, which represents the item's ID. The rest are optional. If the item contains a score field, it will be treated as a retrieval score. All other fields will be treated as item properties.
complex_type_features
Complex type context features must be passed as an array. Each element contains the following content:
Parameter | Description | Example |
name | Context feature name | age |
type | Context feature type | int |
values | Context feature values. Various types can be set, including specific values, arrays, and maps. | 20 |
Example usage:
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 supported types include 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>
.
In the context, complex_type_features and features can be used simultaneously.
API return
Parameter | Description | Type | Example |
code | The response code. | int | 200 |
msg | The returned message. | string | success |
request_id | The unique ID of the request. | string | e332fe9c-7d99-45a8-a047-bc7ec33d07f6 |
size | The number of the returned items. | 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 | The recommended items that are returned. | json array | [{"item_id":"248791390","score":0.9991594902203332,"retrieve_id":"mock_recall"}] |
The following table describes the items:
Parameter | Description | Type | Example |
item_id | The ID of the recommended item. | string | 3v5RE7417j7R |
retrieve_id | The recall source ID. | string | u2i_recall |
score | The recommendation score. | float | 0.45 |
Error codes
Error code | Description | msg |
200 | The request is successful. | success |
299 | The returned items are insufficient. | items size not enough |
400 | Parameter error. Descriptions vary with the error type. | uid not empty or unexpected end of JSON input, etc. |
500 | Server error. An HTTP status code is returned. |
Request sample
curl -v http://host/api/rec/feed -d '{"uid":"76295990", "size":10, "scene_id":"home_feed"}'
Return 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"
},
...
]
}