调用模型服务


完成模型版本部署上线操作后,可通过模型服务的接口,调用已部署的模型服务。智能集市支持通过 REST API 和 GRPC 调用模型服务。

通过 HTTP 调用模型服务

以下示例为通过 HTTP 协议调用 REST API 模型服务。

获取模型服务接口URL

模型服务接口URL的范式为:


{URI-scheme}://{domainName}/eap/metrics/{namespace}/v0.2/{namespace}/{modelName}/{modelName-instanceName-deployment}/api/v1.0/predictions


其中:

  • URI-sheme:协议,支持HTTP协议。

  • domainName:服务的网关地址,其范式为eap.{enos-environment}.{abc}.com,其中enos-environment为EnOS的部署环境名称。

  • namespace:部署服务的容器资源名称,如下图所示:

  • modelName:模型名称。

  • instanceName:模型版本部署实例名称。

请求示例

以调用某个预测模型的服务为例,请求格式如下:

url: http://eap.{domainName}/eap/metrics/mmc-dw4pdv-o15960025444321/v0.2/mmc-dw4pdv-o15960025444321/demo01/demo01-instance-deployment/api/v1.0/predictions

method: POST

requestBody:
{
  "data": {
    "names": [
      "AGE",
      "AGE1",
      "AGE1",
      "AGE1"
    ],
    "ndarray": [
      [
        6,
        3,
        2,
        2
      ]
    ]
  }
}

feedback 接口请求示例

feedback接口请求格式如下:

url: http://eap.{domainName}/eap/metrics/mmc-dw4pdv-o15960025444321/v0.2/mmc-dw4pdv-o15960025444321/demo01/demo01-instance-deployment/api/v1.0/feedback

method:POST

requestBody:
{
  "request": {
    "data": {
      "names": [
        "AGE",
        "RACE",
        "DCAPS",
        "VOL"
      ],
      "ndarray": [
        [
          1,
          1,
          1,
          100
        ]
      ]
    }
  },
  "response": {
    "meta": {
      "routing": {
        "eg-router": 1
      }
    },
  "data": {
    "names": [
      "t:0",
      "t:1",
      "t:2"
    ],
    "ndarray": [
      [
        0.027491291429107768,
        0.00240284367849394,
        1.0586489239828885E-4
      ]
    ]
  }
  },
  "reward": 1
}


其中:

  • request:传入测试数据。

  • response:返回真实数据。

  • data:真实预测结果。

  • routing:老虎机部署参数,指定进行预测的模型版本(按照模型版本部署顺序)。

  • reward:老虎机参数,激励值。

通过 Seldon Client 调用模型服务

基于 Seldon 提供的框架服务,智能集市支持安装 Seldon Client 后,通过 REST(internal / external)和 GRPC 调用模型服务 API。


以下示例为通过 Seldon Client 调用模型服务 API。

安装 Seldon Client

当前 Seldon Client 仅支持 Python 下的 pip 安装方式。在连接公网的状态下,可通过 Notebook 或 Python 编辑器安装。示例如下:

pip install seldon_core


安装完成后,在调用模型服务之前,需要在调用代码前引入 Seldon Client。示例如下:

from seldon_core.seldon_client import SeldonClient
import seldon_core.seldon_client
import time

获取模型服务接口URL

获取模型服务接口URL的方法,参考通过 HTTP 调用模型服务中的说明。

通过 REST(internal)调用服务

在 EnOS 内部调用模型服务的示例如下:

from seldon_core.seldon_client import SeldonClient
import seldon_core.seldon_client
import time

if __name__ == '__main__':
    sc = SeldonClient(deployment_name="demo01-instance-deployment", # set deployment name
                      namespace="mmc-rd9vj2-o15960025444321", # set namespace where deployment is running
                      gateway_endpoint="istio-ingressgateway.istio-system.svc.cluster.local:80",
                      gateway="istio")

    success_cnt = 0
    failure_cnt = 0
    start_time = time.time()
    range_num = 1
    for i in range (range_num):
        res = sc.predict(transport="rest", json_data=[[1,25,167,1.205,265.133,24.771,860.392,1.181,41.64,1.329,281.878,18.26,
                                                                            852.903,1.389,80.508,13360]])
        if res.success:
            print(res)
            success_cnt = success_cnt + 1
        else:
            print(res)
            failure_cnt = failure_cnt + 1
    end_time = time.time()
    qps = range_num / (end_time - start_time)

    print(success_cnt)
    print(failure_cnt)
    print(qps)

通过 REST(external)调用服务

如果需要在 EnOS 外部调用模型服务,需要通过 EnOS API管理 将服务挂载为外部可访问的 API,然后通过 REST(external)调用模型服务。有关 EnOS API管理的详细信息,参考 有关API管理


在 EnOS 外部调用模型服务的示例如下:

from seldon_core.seldon_client import SeldonClient
from seldon_core.seldon_client import microservice_api_rest_seldon_message
import seldon_core.seldon_client
import time


if __name__ == '__main__':
    sc = SeldonClient(gateway_endpoint="eap.{enos-environment}.{abc}.com",
                      gateway="istio")

    success_cnt = 0
    failure_cnt = 0
    start_time = time.time()
    range_num = 1
    for i in range (range_num):
        res = sc.predict(gateway_prefix="/eap/metrics/mmc-dw4pdv-o15960025444321/v0.2/mmc-rd9vj2-o15960025444321/demo01/demo01-instance-deployment/",
                         transport="rest", json_data=[[1,25,167,1.205,265.133,24.771,860.392,1.181,41.64,1.329,281.878,18.26,
                                                                            852.903,1.389,80.508,13360]])
        if res.success:
            print(res)
            success_cnt = success_cnt + 1
        else:
            print(res)
            failure_cnt = failure_cnt + 1
    end_time = time.time()
    qps = range_num / (end_time - start_time)

    print(success_cnt)
    print(failure_cnt)
    print(qps)

通过 GRPC 调用服务

通过 GRPC 调用模型服务的示例如下:

from seldon_core.seldon_client import SeldonClient
from seldon_core.seldon_client import microservice_api_rest_seldon_message
import seldon_core.seldon_client
import time

if __name__ == '__main__':
    sc = SeldonClient(deployment_name="demo01-instance-deployment", # set deployment name
                      namespace="mmc-rd9vj2-o15960025444321", # set namespace where deployment is running
                      gateway_endpoint="istio-ingressgateway.istio-system.svc.cluster.local:80",
                      gateway="istio")

    success_cnt = 0
    failure_cnt = 0
    start_time = time.time()
    range_num = 1
    for i in range (range_num):
        res = sc.predict(transport="grpc", json_data=[[1,25,167,1.205,265.133,24.771,860.392,1.181,41.64,1.329,281.878,18.26,
                                                                            852.903,1.389,80.508,13360]])
        if res.success:
            print(res)
            success_cnt = success_cnt + 1
        else:
            print(res)
            failure_cnt = failure_cnt + 1
    end_time = time.time()
    qps = range_num / (end_time - start_time)

    print(success_cnt)
    print(failure_cnt)
    print(qps)