This topic provides a docker-compose.yaml file for you to quickly build a PAI-Rec-based recommendation service. This helps you quickly understand the capabilities provided by PAI-Rec.
Prerequisites
Docker Engine and Docker Compose are installed. They are required to run the built recommendation service.
For more information about how to install Docker Engine, see Install Docker Engine. For more information about how to install Docker Compose, see Overview of installing Docker Compose.
Themes
1. Test a service provided by PAI-Rec
2. Build a PAI-Rec-based recommendation service that incorporates processes such as recall, exposure blocking, and feature loading
3. Debug the recommendation API and view the service logs
Run the recommendation service
You can visit the website and download the required files.
Run the following command to run the service:
# Decompress the file.
tar zxvf pairec-demo-test.tar.gz
cd pairec-demo-test
# Start the service.
docker-compose -f docker-compose.yaml up -dRun the docker ps command.

The output indicates that you can call the /api/rec/feed interface to access the service over port 8000. For more information about how to call the interface, see Interface test.
Test the service
Run the following command to test the /api/rec/feed interface:
curl -v http://127.0.0.1:8000/api/rec/feed -d '{"uid":"1000000077", "size":10, "scene_id":"feed"}'Run the following command to query the logs of the service:
docker logs -f pairecRun the following command to query the service logs from which you can know whether the user features and item features are loaded as expected and whether the model scores meet expectations. The debug parameter is added to the code.
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 downHow it works
In this example, MySQL is used as the storage engine. In a production service, we recommend that you use a distributed storage engine such as ApsaraDB for Redis, Hologres, Tablestore, or iGraph. For more information about how MySQL works, see the docker-compose.yaml file.
For more information about the overall configuration, see the config.json file.
Table data
The pairec_demo_test.sql file provides demo data and SQL statements used to create tables.
u2i_recall: the U2I table that contains recall data. The data is obtained by using the U2I recall method.
user_expose_history: the exposure blocking table. The data returned by the interface is written to the exposure blocking table. When you call the interface next time, the data of the recommended items will not be returned repeatedly.
user_feature: the user feature table. The user features are loaded. They are used by ranking models.
Run the following command to query the structures or data of the tables:
# Log on to the MySQL Docker container.
docker exec -it pairec-mysql bash
# Log on to MySQL.
mysql -uroot -ptest
# Use the following 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 custom scenario.
The RecallNames parameter specifies the names of the multiple types of recalls. In this example, the value is set to u2i_recall.
"SceneConfs": {
"feed": {
"default": {
"RecallNames": ["u2i_recall"]
}
}
}Filter configuration
In this example, exposure blocking is configured.
The configuration of exposure blocking is similar to the recall configuration. FilterConfs specifies the filter configurations. The FilterNames parameter specifies the filter policies based on scenarios. No specific scenario is specified. The value default is used and it indicates that the filter policies 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"
]
}Run the following command to call the interface multiple times to check whether exposure blocking takes effect. If exposure blocking takes effect, the outputs are different for each call.
curl -v http://127.0.0.1:8000/api/rec/feed -d '{"uid":"1000000077", "size":10, "scene_id":"feed"}'The service logs contain the following information: requestId=d440c298-3890-4a6c-91e3-9654c83cc72a event=User2ItemExposureFilter count=213 cost=5. count indicates the number of items that are filtered after exposure blocking. The value of the count parameter shall reduce each time you call the interface.
Feature loading
The user features and item features must be loaded for item scoring. PAI-Rec can efficiently load feature data by using the feature configurations. For more information, 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" :[]
}
]
}
}References
For more information about how to use an official image to deploy the PAI-Rec engine, see Deploy a PAI-Rec engine service.
For more information about how to customize code for PAI-Rec engine development and compile and package the project as an image, see Create a project.
For more information about configurations of the PAI-Rec engine, see Engine configurations.