推理服务 ml-server

用 ml-server 部署 sklearn/xgboost/R 模型、不知道 config.json 怎么写、各模型格式怎么保存、调用接口路径/方法是什么时读这篇

平台使用 / 服务化与推理
ml-server推理服务inferencesklearnxgboostxgbpmmlR 模型config.jsonmodel_pathframeworkpredict标准化接口model status apimetadata apipredict api

推理服务 ml-server

ml-server 是平台提供的轻量推理服务镜像,统一封装了 sklearn / xgboost / R(pmml) 模型, 并对外暴露与 TFServing 风格一致的标准化 HTTP 接口。

镜像

ccr.ccs.tencentyun.com/cube-studio/ml-server:20251001

与代码一致:myapp/config.py:1325myapp/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:212myapp/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"}

最后更新 2026-06-30完整文档以官方仓库为准:GitHub Wiki