Engine features
Quickly set up a recommendation service with the PAI-Rec engine by using a docker-compose file to explore its core services.
Prerequisites
The service requires Docker and Docker Compose.
For installation instructions, see Install Docker and Install Docker Compose.
What you will learn
How to test the APIs provided by the engine.
How to quickly build a recommendation service based on PAI-Rec, including processes such as recall, exposure filtering, and feature loading.
How to debug the recommendation API and view service logs.
Run the service
To begin, download the required files.
Start the service.
# Decompress the package
tar zxvf pairec-demo-test.tar.gz
cd pairec-demo-test
# Start the service
docker-compose -f docker-compose.yaml up -d
Run the docker ps command to view the running containers.
➜ pairec-demo-test docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
992976bc4b21 mybigpai-registry.cn-beijing.cr.aliyuncs.com/mybigpai/pairec_base:1.0.0 "/usr/bin/rec_entryp…" 3 seconds ago Up 1 second 0.
0.0.0:8000->8000/tcp pairec
b6f62fbe4a07 mysql:8.0 "docker-entrypoint.s…" About a minute ago Up 3 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp pairec-mysql
The service exposes the /api/rec/feed endpoint on port 8000. For instructions on calling the endpoint, see the API testing section in the PAI-Rec quick start guide.
Test the service
Test the endpoint.
curl -v http://127.0.0.1:8000/api/rec/feed -d '{"uid":"1000000077", "size":10, "scene_id":"feed"}'
To view detailed service logs, run the following command:
docker logs -f pairec
Add the 'debug' parameter to the request to view more detailed logs, including whether user and item features are loaded correctly and if model scores meet expectations:
curl -v http://127.0.0.1:8000/api/rec/feed -d '{"uid":"1000000077", "size":10, "scene_id":"feed", "debug":true}'
Stop the service.
docker-compose -f docker-compose.yaml down
How it works
For simplicity, this demo uses MySQL as the storage engine. In production environments, you typically use distributed storage engines such as Redis, Hologres, Tablestore, or iGraph. For MySQL configuration details, see the docker-compose.yaml file.
For details about the overall configuration, see the config.json file.
Table data
The pairec_demo_test.sql file provides the statements for creating tables and initializing data.
-
u2i_recall: Contains recall data from the User-to-Item (U2I) recall method.
-
user_expose_history: The exposure filtering table. The engine writes data from API responses to this table to prevent the same items from being returned in subsequent calls.
-
user_feature: The user feature table. The engine loads user features from this table for use by the ranking model.
To view the table schema or data, run the following commands:
# Log on to the MySQL Docker container.
docker exec -it pairec-mysql bash
# Connect to MySQL.
mysql -uroot -ptest
# Switch to the database.
use pairec_demo_test
# View the tables.
show tables;
Data source configuration
pairec-mysql is the custom data source name.
"MysqlConfs": {
"pairec-mysql" :{
"DSN": "root:test@tcp(db:3306)/pairec_demo_test?multiStatements=true"
}
}
Recall configuration
"RecallConfs": [
{
"Name": "u2i_recall",
"RecallType": "UserCustomRecall",
"RecallCount": 500,
"DaoConf" :{
"AdapterType": "mysql",
"MysqlName": "pairec-mysql",
"MysqlTable": "u2i_recall"
}
}
]
-
'feed' is the name of the defined scenario.
-
The 'RecallNames' parameter configures multi-channel recall. In this example, only 'u2i_recall' is configured.
"SceneConfs": {
"feed": {
"default": {
"RecallNames": ["u2i_recall"]
}
}
}
Filter configuration
Exposure filtering is configured similarly to recall.
FilterConfs contains the specific configurations, and scenarios use 'FilterNames' to reference them. Because 'FilterNames' is defined under 'default' instead of a specific scenario, the filters apply to all scenarios.
"FilterConfs": [
{
"Name":"User2ItemExposureFilter",
"FilterType":"User2ItemExposureFilter",
"WriteLog": true,
"DaoConf":{
"Adapter":"User2ItemExposureMysqlDao",
"AdapterType":"mysql",
"MysqlName": "pairec-mysql",
"MysqlTable": "user_expose_history"
}
}
],
"FilterNames": {
"default": [
"UniqueFilter",
"User2ItemExposureFilter"
]
}
To check if exposure filtering is working, call the API endpoint multiple times. The returned items should be different each time.
curl -v http://127.0.0.1:8000/api/rec/feed -d '{"uid":"1000000077", "size":10, "scene_id":"feed"}'
In the service logs, you will see an entry like requestId=d440c298-3890-4a6c-91e3-9654c83cc72a event=User2ItemExposureFilter count=213 cost=5. The 'count' value represents the number of items remaining after exposure filtering and should decrease with each call.
Feature loading
After retrieving item data, the engine scores items with a ranking model. User and item features must be loaded first. PAI-Rec supports high-performance feature loading through simple configuration. For details, see Configure features.
"FeatureConfs": {
"feed": {
"AsynLoadFeature" : true,
"FeatureLoadConfs": [
{
"FeatureDaoConf": {
"AdapterType": "mysql",
"MysqlName": "pairec-mysql",
"FeatureKey": "user:uid",
"UserFeatureKeyName": "userid",
"MysqlTable": "user_feature",
"UserSelectFields":"*",
"FeatureStore":"user"
},
"Features" :[]
}
]
}
}
Next steps
-
See Deploy a PAI-Rec engine service for instructions on deploying the service with an official image.
-
See Quickstart: Create a project for instructions on how to customize, compile, and package the engine code.
-
For detailed configuration options, see Engine configuration.