【问题标题】:Error call Google Vertex AI endpoint from a python backend从 python 后端调用 Google Vertex AI 端点时出错
【发布时间】:2022-09-27 18:19:45
【问题描述】:

我正在尝试向我的 google vertex ai 端点发送一个 http post 请求以进行预测。尽管我确实在请求标头中设置了承载令牌,但请求仍然失败并出现以下错误:

{
\"error\": {
    \"code\": 401,
    \"message\": \"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.\",
    \"status\": \"UNAUTHENTICATED\",
    \"details\": [
        {
            \"@type\": \"type.googleapis.com/google.rpc.ErrorInfo\",
            \"reason\": \"ACCESS_TOKEN_TYPE_UNSUPPORTED\",
            \"metadata\": {
                \"service\": \"aiplatform.googleapis.com\",
                \"method\": \"google.cloud.aiplatform.v1.PredictionService.Predict\"
            }
        }
    ]
}

}

由于我是从 python 后端进行此调用的,因此我不确定消息中建议的 OAuth 2 是否是明智且适用的选择。

该模型已经部署并在顶点 ai 上进行了端点测试,并且运行良好。我想做的是使用邮递员通过http post请求发送相同的预测任务,这就是失败的原因。

请求网址如下所示:

https://[LOCATION]-aiplatform.googleapis.com/v1/projects/[PROJECT ID]/locations/[LOCATION]/endpoints/[ENDPOINT ID]:predict

其中令牌持有者在 potman 授权选项卡中设置,实例在请求正文中设置。

  • 你能分享你的代码让我们重现你的场景吗
  • @AnjelaB该模型已经部署并在顶点ai上进行了终点测试,并且运行良好。我想做的是使用邮递员通过http post请求发送相同的预测任务,这就是失败的原因。
  • @AnjelaB 我已经更详细地更新了这个问题。

标签: python google-cloud-platform google-cloud-endpoints google-cloud-ml google-cloud-vertex-ai


【解决方案1】:

我通常这样称呼 Vertex AI 端点:

from google.cloud import aiplatform
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/user/1a2b3c4d.json"

aip_endpoint_name = (
    f"projects/your-project/locations/us-west1/endpoints/1234567"
)

endpoint = aiplatform.Endpoint(aip_endpoint_name)

在这里,您可以根据需要对输入进行编码。

def encode_64(input):
    message = input
    message_bytes = message.encode('ascii')
    base64_bytes = base64.b64encode(message_bytes)
    base64_message = base64_bytes.decode('ascii')
    return base64_message

检查输入类型的模型签名:

saved_model_cli show --dir /home/jupyter/model --all

然后调用端点

instances_list = [{"input_1": {"b64": encode_64("doctor")}}]

instances = [json_format.ParseDict(s, Value()) for s in instances_list]

results = endpoint.predict(instances=instances)
print(results.predictions)

根据您提交给 Vertex AI 端点的输入类型(整数、数组、字符串、图像),您可能需要更改编码。

【讨论】:

  • 我不知道为什么,但这也对我有用:)
【解决方案2】:

如果您在发出 http post 请求时不想使用 oAuth 2.0 进行身份验证,则可能需要使用 Application Default Credentials。在这个documentation 中,您可以按照逐步获取服务帐户密钥以通过环境变量传递凭据。

【讨论】:

  • 我确实从文档中收集了一些见解。使用代码传递凭据是一个可行的选项,但仅用于存储。我的问题是,aiplatform.init 中凭证参数的预期值是多少?使用 json 服务帐户密钥失败。
【解决方案3】:

您可以尝试以下操作:

导入子流程 导入base64 导入请求 导入json

def get_headers():
  gcloud_access_token = subprocess.check_output("gcloud auth print-access-token".split(' ')).decode().rstrip('\n')
  return {"authorization": "Bearer " + gcloud_access_token}

def send_get_request(uri):
  return requests.get(uri, headers=get_headers(), verify=False)

它应该可以在您发送请求的机器上配置 gcloud(您可以使用 gcloud init 完成此操作)并有权访问顶点预测。

【讨论】:

    猜你喜欢
    • 2021-12-02
    • 2021-11-06
    • 2022-08-03
    • 2023-02-09
    • 2023-02-17
    • 1970-01-01
    • 2022-08-02
    • 2023-01-26
    • 2014-05-08
    相关资源
    最近更新 更多