错误调用来自Python后端的Google Vertex AI端点

发布于 2025-02-09 01:57:38 字数 1104 浏览 3 评论 0原文

我正在尝试将HTTP POST请求发送到我的Google Vertex AI端点进行预测。尽管我确实在请求标题中设置了携带者令牌,但是请求仍然失败,以下错误:

{
"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上部署和端点测试,并且效果很好。我要做的是使用Postman通过HTTP POST请求发送相同的预测任务,这就是失败的。

请求URL看起来像这样:

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

在POTMAN授权选项卡和请求主体中设置的实例中设置了令牌承载者的位置。

I am trying to send an http post request to my google vertex ai endpoint for prediction. Though I do set the Bearer Token in the request header, the request still fails with the below error:

{
"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"
            }
        }
    ]
}

}

Since I am making this call from a python backend, I'm not sure if OAuth 2 as suggested in the message would be wise and applicable choice.

The model is already deployed and endpointed test on vertex ai and it worked fine. What I am trying to do is send same prediction task via an http post request using postman and this is what failed.

The request url looks like this:

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

Where token bearer is set in the potman authorization tab and instance set in request body.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一人独醉 2025-02-16 01:57:38

我通常以这种方式调用顶点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)

根据提交到顶点AI端点的输入的类型(整数,数组,字符串,图像)调用端点,您可能必须更改编码。

I usually call Vertex AI endpoint this way:

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)

Here, you encode the input according to your needs.

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

Check the model signature for the input type:

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

Then call the endpoint

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)

According to the type of the input you are submitting to Vertex AI endpoint (integer, array, string, image), you may have to change the encoding.

风苍溪 2025-02-16 01:57:38

如果您不想在提出http post请求时使用OAuth 2.0进行身份验证,则可能需要使用 application默认凭据。在此文档>通过环境变量传递凭据。

If you don't want to use oAuth 2.0 for authentication when making http post request, you may want to use Application Default Credentials. In this documentation you can follow the step by step on getting service account key to passing credentials via environment variable.

薄荷→糖丶微凉 2025-02-16 01:57:38

您可以尝试以下操作:

导入子过程
导入基础64
导入请求
导入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配置的FF(可以通过使用GCLOUD INIT来执行此操作),并具有访问顶点预测的权限。

Can you try following:

import subprocess
import base64
import requests
import 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)

It should work ff you have gcloud configured (you can do this by using gcloud init) on the machine you are sending request from and have permissions to access Vertex Prediction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文