发送 http 请求 Google Vertex AI 端点

Sending http request Google Vertex AI end point

我刚刚在 Google vertex AI 上部署了一个 ML 模型,它可以使用 vertex AI 网络界面进行预测。但是是否可以从浏览器向这个已部署的模型发送请求。像

http://myapp.cloud.google.com/input="features of an example" 

并将预测作为输出。 谢谢

是的,您可以使用端点 URL 作为发送。

https://us-central1-aiplatform.googleapis.com/v1beta1/projects/<PROJECT_ID>/locations/us-central1/endpoints/<ENDPOINT_ID>:predict

数据应在 POST 参数中给出。

{
  "instances": 
    [1.4838871833555929,
 1.8659883497083019,
 2.234620276849616,
 1.0187816540094903,
 -2.530890710602246,
 -1.6046416850441676,
 -0.4651483719733302,
 -0.4952254087173721,
 0.774676376873553]
}

URL 应该是基于区域的。

端点URL

Vertex AI 文档 - 指南 - Send an online prediction request

HTTP method and URL:

  • LOCATION: The region where you are using Vertex AI.
  • PROJECT: Your project ID
  • ENDPOINT_ID: The ID for the endpoint.
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/endpoints/ENDPOINT_ID:predict

或者我们可以使用 gcloud ai endpoints list 命令。

$ LOCATION='us-central1'
$ gcloud ai endpoints list --region=${LOCATION} --uri
Using endpoint [https://us-central1-aiplatform.googleapis.com/]
https://us-central1-aiplatform.googleapis.com/v1beta1/projects/335513839215/locations/us-central1/endpoints/5031620295400620032

我们可以在 Vertex AI API - Service: aiplatform.googleapis.com

中检查可用的 Vertex AI API 服务端点 URL FQDN 部分
https://us-central1-aiplatform.googleapis.com
https://us-east1-aiplatform.googleapis.com
https://us-east4-aiplatform.googleapis.com
https://us-west1-aiplatform.googleapis.com
https://us-west2-aiplatform.googleapis.com
https://northamerica-northeast1-aiplatform.googleapis.com
https://northamerica-northeast2-aiplatform.googleapis.com
https://europe-west1-aiplatform.googleapis.com
https://europe-west2-aiplatform.googleapis.com
https://europe-west3-aiplatform.googleapis.com
https://europe-west4-aiplatform.googleapis.com
https://europe-west6-aiplatform.googleapis.com
https://asia-east1-aiplatform.googleapis.com
https://asia-east2-aiplatform.googleapis.com
https://asia-northeast1-aiplatform.googleapis.com
https://asia-northeast3-aiplatform.googleapis.com
https://asia-south1-aiplatform.googleapis.com
https://asia-southeast1-aiplatform.googleapis.com
https://australia-southeast1-aiplatform.googleapis.com

Google API 用于顶点端点预测

上面的URL格式是基于API定义Method: projects.locations.endpoints.predict.

POST https://{service-endpoint}/v1/{endpoint}:predict

Where {service-endpoint} is one of the supported service endpoints.


身份验证

GCP 默认使用 OAuth 2.0 for authentication,Vertex AI 端点使用 HTTP 授权 header 中设置的 OAuth 2.0 访问令牌作为 Bearer 令牌对访问进行身份验证。

RFC 6749 - OAuth 2.0 授权框架

     +--------+                               +---------------+
     |        |--(A)- Authorization Request ->|   Resource    |
     |        |                               |     Owner     |
     |        |<-(B)-- Authorization Grant ---|               |
     |        |                               +---------------+
     |        |
     |        |                               +---------------+
     |        |--(C)-- Authorization Grant -->| Authorization |
     | Client |                               |     Server    |
     |        |<-(D)----- Access Token -------|               |
     |        |                               +---------------+
     |        |
     |        |                               +---------------+
     |        |--(E)----- Access Token ------>|    Resource   |
     |        |                               |     Server    |
     |        |<-(F)--- Protected Resource ---|               |
     +--------+                               +---------------+

如果我们有可用的 gcloud 命令,例如在 GCP 环境中,我们可以使用 ADC (Application Default Credentials) 首先通过 gcloud auth application-default login 进行身份验证,然后使用 gloud auth print-access-token 命令获取令牌。然后可以将令牌指定给 HTTP header,如下面的 Vertex AI 文档所示。

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/endpoints/ENDPOINT_ID:predict"

请求body格式

{
  "instances": [
    value
  ],
  "parameters": value
}

要设置的实际值取决于框架。如果您从自定义模型中获得预测,请参阅 Get online predictions from custom-trained models- Format your input for online prediction