All Products
Search
Document Center

Artificial Intelligence Recommendation:Set up a PAI-Rec project

Last Updated:Apr 01, 2026

pairecmd is the command-line tool for scaffolding PAI-Rec recommendation engine projects. This guide walks you through downloading pairecmd, creating a project, compiling and packaging it, running the service locally, and testing the built-in recommendation API.

Prerequisites

Before you begin, make sure you have:

  • Go installed and GOPATH configured

  • Docker installed (required to build and push container images)

  • An Alibaba Cloud Container Registry instance (required for the image packaging step)

Download pairecmd

Download the binary for your platform:

PlatformDownload link
Linuxpairecmd
macOS (Intel)pairecmdmac
macOS (Apple Silicon)pairecmdmacarm
Windowspairecmdwin

On Linux or macOS, grant execute permission after downloading. Replace <binary> with the downloaded filename:

chmod +x <binary>
Version 2 of pairecmd supports the PAI-Rec console. For production projects, use the console to manage your project configuration.

Create a project

Run the following command to create a project named pairec-demo. The command generates a pairec-demo folder containing the project scaffold in the current directory.

./pairecmdmac project --name pairec-demo

The generated folder has the following structure:

pairec-demo
├── Makefile
├── conf
│   └── config.json.production
├── docker
│   └── Dockerfile
├── go.mod
└── src
    ├── controller
    │   └── feed.go
    └── main.go

Navigate into the project folder and install dependencies:

cd pairec-demo
go mod tidy

Compile and package the project

The project includes a Makefile with targets for compiling and packaging.

Compile the project:

make && make build

If the compilation fails, run go mod tidy first to resolve any missing dependencies.

Package the project into a container image:

make release

Before running make release, activate a personal image in Alibaba Cloud Container Registry and create the image repository referenced by BIN_NAME in the Makefile.

image-20240715174501233.png

Run the project

Navigate to the pairec-demo folder and start the service. Use the config parameter to specify the configuration file path. Logs are printed to the terminal.

go run src/main.go --config=conf/config.json.production --alsologtostderr

To verify the service is running, open another terminal and send a test request:

curl -v http://localhost:8000/api/rec/feed -d '{"uid":"76295990", "size":10, "scene_id":"home_feed"}'

A successful response returns HTTP 200 with a JSON body similar to the example in the Request and response example section below.

For details on the config parameter options, see Engine configuration.

API reference

The generated project includes a built-in recommendation API. The implementation is in controller/feed.go.

Endpoint

POST /api/rec/feed

Request parameters

ParameterTypeRequiredDescriptionExample
uidstringYesUser ID"1000079"
sizeintegerYesNumber of items to retrieve10
scene_idstringYesScenario IDhome_feed
featuresJSON objectNoContext features as key-value pairs{"age":20, "sex":"male"}
complex_type_featuresJSON arrayNoContext features with explicit type information, required when sending typed data to the model service[{"name":"age", "type":"int", "values":20}]
item_idstringNoItem ID for similarity-based recommendations248791390
item_listJSON arrayNoCustom recall list[{"item_id":"1111", "score":1}]
debugbooleanNoEnable debug mode to print additional logstrue
request_idstringNoCustom request ID. If omitted, the PAI-Rec engine generates one automatically"c46c3f5e-6b32-4b5c-8aac-59a319941248"

`item_list` behavior

item_list is a JSON array of objects. Each object must include an item_id field. If an object includes a score field, that value is used as the recall score. All other fields are treated as item properties.

`complex_type_features` format

Pass complex context features as an array. Each element has the following fields:

FieldDescriptionExample
nameContext feature nameage
typeContext feature typeint
valuesContext feature value — supports scalar values, arrays, and maps20

Supported 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>.

Examples:

  • Single value: [{"name":"age", "type":"int", "values":20}, {"name":"sex", "values":"male", "type":"string"}]

  • Array: [{"name":"list_features", "type":"list<int>", "values":[1,2,3]}]

  • Map: [{"name":"map_features", "type":"map<int,int>", "values":{"1":10,"2":20,"3":30}}]

complex_type_features and features can be used together in the same request.

Request and response example

Request:

curl -v http://host/api/rec/feed -d '{"uid":"76295990", "size":10, "scene_id":"home_feed"}'

Response:

{
    "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"
        }
    ]
}

Response fields

FieldTypeDescriptionExample
codeintegerBusiness status code200
msgstringStatus messagesuccess
request_idstringUnique request IDe332fe9c-7d99-45a8-a047-bc7ec33d07f6
sizeintegerNumber of recommended items returned10
experiment_idstringExperiment ID. Empty string if no A/B testing is activeER2_L1#EG1#E2
itemsJSON arrayList of recommended items

Each item in items contains:

FieldTypeDescriptionExample
item_idstringRecommended item ID3v5RE7417j7R
retrieve_idstringRecall source IDu2i_recall
scorefloatRecommendation score0.45
extraJSON objectCustom output fields. Not returned unless configured

Error codes

CodeDescriptionmsg
200Request succeededsuccess
299Insufficient items returneditems size not enough
400Parameter erroruid not empty or unexpected end of JSON input
500Server errorHTTP status code returned

Custom output fields

By default, each item in the response includes item_id, retrieve_id, and score. To return additional fields — such as item properties or model scores — configure OutputFields under SceneConfs for the target scenario. The API returns the configured fields in the extra field.

Example configuration:

"SceneConfs": {
  "${scene_name}": {
    "default": {
      "RecallNames": [
        "collaborative_filter"
      ],
      "OutputFields": [
        "item:type",
        "item:age",
        "score:*",
        "score:model_v1_ctr"
      ]
    }
  }
}
  • Fields prefixed with item: return the corresponding item property. If the property does not exist, null is returned.

  • Fields prefixed with score: return the score from the specified model.

  • score:* returns all model scores, placed in algo_scores.

FAQ

How do I pass an item list through the API for reranking?

Pass the list using the item_list parameter. In the engine configuration, set up a Context Item Recall (ContextItemRecall) so the PAI-Rec engine reads items directly from item_list.

How do I instrument the client to use experiment reports?

The API response includes request_id and experiment_id. Record both fields in your impression and click behavior logs at a minimum. Including them in additional logs — such as playback duration, add-to-cart events, and purchases — gives more complete performance data.

Once your behavior logs are instrumented, generate the offline Experiment Report Source Table and register it with the AB Test platform. Configure custom metrics as needed, then view results in the Experiment Metrics Report. For the SQL pattern used to generate the source table, see Data Registration and Field Configuration.

What's next