推理服务 ml-server
ml-server 是平台提供的轻量推理服务镜像,统一封装了 sklearn / xgboost / R(pmml) 模型,
并对外暴露与 TFServing 风格一致的标准化 HTTP 接口。
镜像
ccr.ccs.tencentyun.com/cube-studio/ml-server:20251001
与代码一致:
myapp/config.py:1325、myapp/init/init-inference.json:7。
配置 config.json
[
{
"name": "模型英文名",
"model_path": "模型地址",
"framework": "sklearn",
"version": "20231001",
"enable": true,
"output": [
{ "name": "y", "type": "enum", "choices": [0,1] }
],
"input": [
{ "name": "key1", "type": "int" },
{ "name": "key2", "type": "enum", "choices": [0,1,2,3,4,5,6] },
{ "name": "key3", "type": "float" }
],
"example": [
{ "key1": 30, "key2": 0, "key3": -1.8 }
]
},
{
"name": "模型英文名",
"model_path": "模型地址",
"framework": "r",
"version": "20231001",
"enable": true
},
{
"name": "模型英文名",
"model_path": "模型地址",
"framework": "xgb",
"version": "20231001",
"enable": true
}
]
字段说明:
model_path支持本地路径,也支持 http/https 在线地址(在线地址会被自动下载,见images/serving/ml/server.py:174)。- 模型格式约定(见
images/serving/ml/server.py:175-199):xgb(xgboost)模型保存为.model格式;r(R 语言)模型保存为.pmml格式;sklearn模型保存为.pkl(pickle)或.joblib格式。
enable默认true;置false时该模型不会被加载。
启动命令
python server.py --config_path xxx
在平台部署时,配置文件会挂载到容器内
/config/config.json,平台默认启动命令为python server.py --config_path /config/config.json(见myapp/views/view_inferenceserving.py:86)。 也支持用环境变量CONFIG_PATH指定配置路径,默认config.json(见images/serving/ml/server.py:202、:209)。 服务监听端口为 80(见images/serving/ml/server.py:212、myapp/views/view_inferenceserving.py:107)。
标准化模型接口
注意:ml-server 的接口路径全部使用斜杠分隔(
.../predict、.../metadata), 与 TFServing 的冒号语法(:predict)不同(见images/serving/ml/server.py:27-91)。
Model status API:
GET http://host:port/v1/models/${MODEL_NAME}
GET http://host:port/v1/models/${MODEL_NAME}/versions/${VERSION}
示例:
http://demo.service.xxx/v1/models/my_model1/versions/20210924
Model Metadata API:
GET http://host:port/v1/models/${MODEL_NAME}/metadata
GET http://host:port/v1/models/${MODEL_NAME}/versions/${VERSION}/metadata
示例:
http://demo.service.xxx/v1/models/my_model1/versions/20210924/metadata
Predict API:
POST http://host:port/v1/models/${MODEL_NAME}/predict
POST http://host:port/v1/models/${MODEL_NAME}/versions/${VERSION}/predict
示例:
http://xx.xx.xx.xx/v1/models/my_model1/versions/20210924/predict
预测返回格式(见 images/serving/ml/server.py:147-156):
{
"status": 0,
"message": "success",
"result": {
"y": [...], // 预测类别 / 预测值
"score": [...] // 预测概率(无 predict_proba 时为 null)
}
}
模型不存在时返回 {"status": -1, "result": {}, "message": "model not exist"}。