本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
調用非同步端點
使用 InvokeEndpointAsync 從非同步端點上託管的模型中獲取推論。
注意
如果您尚未這麼做,請將您的推論資料 (例如機器學習模型、範例資料) 上傳到 Amazon S3。
您可以透過兩種方式之一提供請求承載。這些選項是互斥的;在請求中僅提供其中一個選項:
內嵌承載 — 對於最多 128,000 個位元組的承載,請使用
Body參數直接在請求中傳遞資料。這可避免在每次調用之前將承載上傳到 Amazon S3。Amazon S3 位置 — 對於較大的承載,請將推論資料上傳至 Amazon S3,並使用
InputLocation參數傳遞其 URI。
在請求中指定下列欄位:
對於
Body,請內嵌提供推論承載 (最多 128,000 個位元組)。使用此 或InputLocation,但不能同時使用兩者。針對
InputLocation,指定推論資料的 Amazon S3 位置。使用此 或Body,但不能同時使用兩者。對於
EndpointName,指定端點的名稱。(選用) 對於
InvocationTimeoutSeconds,您可以設定請求的最大逾時。您可以將此值設定為每個請求上限 3600 秒 (一小時)。如果您未在請求中指定此欄位,請求逾時預設值是 15 分鐘。
下列範例會將承載與 Body 參數內嵌傳送,這不需要先將輸入上傳到 Amazon S3。
# Create a low-level client representing Amazon SageMaker Runtime sagemaker_runtime = boto3.client("sagemaker-runtime", region_name=<aws_region>) # Specify the inference payload inline (up to 128,000 bytes) payload =b'{"inputs": "your inference data here"}'# The name of the endpoint. The name must be unique within an AWS 區域 in your AWS 帳戶. endpoint_name='<endpoint-name>'# After you deploy a model into production using SageMaker AI hosting # services, your client applications use this API to get inferences # from the model hosted at the specified endpoint. response = sagemaker_runtime.invoke_endpoint_async( EndpointName=endpoint_name, Body=payload, ContentType="application/json", InvocationTimeoutSeconds=3600)
或者,您可以將承載存放在 Amazon S3 中,並使用 InputLocation 參數傳遞其位置。如果您尚未這麼做,請先將推論資料上傳至 Amazon S3,再叫用端點。
# Create a low-level client representing Amazon SageMaker Runtime sagemaker_runtime = boto3.client("sagemaker-runtime", region_name=<aws_region>) # Specify the location of the input. Here, a single SVM sample input_location ="s3://bucket-name/test_point_0.libsvm"# The name of the endpoint. The name must be unique within an AWS 區域 in your AWS 帳戶. endpoint_name='<endpoint-name>'# After you deploy a model into production using SageMaker AI hosting # services, your client applications use this API to get inferences # from the model hosted at the specified endpoint. response = sagemaker_runtime.invoke_endpoint_async( EndpointName=endpoint_name, InputLocation=input_location, InvocationTimeoutSeconds=3600)
您會收到 JSON 字串形式的回應,其中包含您的請求 ID,以及 Amazon S3 儲存貯體的名稱,該儲存貯體在處理後會對 API 呼叫進行回應。