快速开始

安装命令

我们可以使用 pairecmd 快速创建项目。现在 V2 版本支持 PAI-Rec 云上控制台,目前也推荐用 V2 版本。

V2 版本

下载地址:

linux 版本

mac 版本

mac arm版本

window 版本

V1 版本

下载地址:

linux 版本

mac 版本

window 版本

下载之后,在类 *nix 系统,需要设置下可执行文件。 chmod +x pairecmd

创建项目

以创建项目名称 pairec-demo 举例,执行命令后,会在当前目录生成 pairec-demo 目录,里面包含项目代码。

./pairecmdmac project --name pairec-demo

生成的目录结构如下

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

进入 pairec-demo 目录,执行

go mod tidy

编译打包项目

项目里提供了 Makefile 文件, 如果要编译的话,直接运行

make && make build

如果遇到错误的话,可以先执行

go mod tidy

如果要打包成镜像的话,可以

make release

可以结合实际情况来修改 Makefile 文件。

image-20240715174501233

可以通过 阿里云镜像 开通个人镜像,然后创建BIN_NAME 指定的镜像仓库。

运行项目

可以进入到 pairec-demo 目录, 运行一下命令启动服务,日志会输出到终端上。

config 传入配置文件路径。

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

可以用下面的接口进行测试。可以开启另一个终端,运行

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

如果使用配置的热加载功能,可以参考这里

Config 具体的配置可以参考配置说明模块。

接口测试

在生成的项目中,本身已经实现了一个推荐接口,具体实现可以参考 controller/feed.go 。 接口定义如下。

接口名称

/api/rec/feed

请求参数

参数名称

参数说明

类型

是否必须

用例

uid

用户id

string

“1000079”

size

获取item数量

integer

10

scene_id

场景id

String

home_feed

features

上下文特征

json map

{“age”:20, “sex”:”male”}

complex_type_features

复杂类型的上下文特征,如果需要有类型的信息请求模型服务,需要设置此值。

json array

[{“name”:”age”, “type”:”int”, “values”:20}, {“name”:”sex”, “values”:”male”, “type”:”string”}]

item_id

相似性推荐传入的物品ID

string

248791390

item_list

自定义的召回传入列表

json array

[{“item_id”:”1111”, “score”:1},{“item_id”:”222”, “score”:0.95}]

debug

debug 用,打印更多的日志

bool

true

request_id

请求唯一标识ID,此参数为空时,PAI-Rec 引擎会自动生成,如果用户传入,使用参数值当成请求的ID

string

“c46c3f5e-6b32-4b5c-8aac-59a319941248”

当有一批召回的数据,希望通过接口传递进来,可以赋值给 item_list。 item_list 是一个数组,每个 item 是 map object ,item 中必须有 item_id 字段,表示物品的ID。其余都是可选的。 item 中如果包含 score 字段,会当成召回得分处理。其余字段当成 item 属性进行处理。

complex_type_features

复杂类型的上下文特征需要数组传递过来,每个元素包含的内容如下

名称

说明

用例

name

上下文特征名称

age

type

上下文特征类型

int

values

上下文特征值,这里可以设置多种类型,支持具体值,数组, map 等

20

使用示例

  1. 传递单个值,[{"name":"age", "type":"int", "values":20}, {"name":"sex", "values":"male", "type":"string"}]

  2. 传递数组 [{"name":"list_features", "type":"list<int>", "values":[1,2,3]}]

  3. 传递map [{"name":"map_features", "type":"map<int,int>", "values":{"1":10,"2":20,"3":30}}], 或者 [{"name":"map_features", "type":"map<int,int>", "values":{"1":"10","2":"20","3":"30"}}],

type 支持的类型包括 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>, map<int64,string>

传递上下文中, complex_type_features 和 features 可以同时使用。

接口返回

名称

说明

类型

示例

code

接口返回业务码

int

200

msg

业务信息

string

success

request_id

请求的唯一标识

string

e332fe9c-7d99-45a8-a047-bc7ec33d07f6

size

返回的推荐条目数量

int

10

experiment_id

实验ID 标识, 没有对接 AB 实验,返回为空

string

ER2_L1#EG1#E2

items

返回推荐条目的列表

json array

[{“item_id”:”248791390”,”score”:0.9991594902203332,”retrieve_id”:”mock_recall”}]

items 条目具体说明如下

名称

说明

类型

示例

item_id

推荐物品ID

string

3v5RE7417j7R

retrieve_id

召回源ID标识

string

u2i_recall

score

推荐得分

float

0.45

错误码说明

错误码

说明

msg

200

接口正常

success

299

返回的数量不足

items size not enough

400

参数错误,不同的情况, msg 说明不同

uid not empty 或者nexpected end of JSON input 等

500

服务器错误, 以 HTTP 错误码形式返回

请求样例

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

返回数据

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