

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 调用异步端点
<a name="async-inference-invoke-endpoint"></a>

使用 `InvokeEndpointAsync`，从托管在异步端点上的模型获取推理。

**注意**  
如果您还没有上传推理数据（例如机器学习模型、示例数据），请将数据上传到 Amazon S3。

您可以通过以下两种方式之一提供请求有效负载。这些选项是相互排斥的；在请求中仅提供其中一个：
+ **内联有效负**载-对于不超过 128,000 字节的有效负载，请使用参数直接在请求中`Body`传递数据。这样可以避免在每次调用之前将有效负载上传到 Amazon S3。
+ **Amazon S3 位置** — 对于较大的负载，请将您的推理数据上传到 Amazon S3，然后将其 URI 与参数一起传递。`InputLocation`

在您的请求中指定以下字段：
+ 对于`Body`，提供内联推理有效负载（最多 128,000 字节）。使用这个*或者*`InputLocation`，但不能两者兼而有之。
+ 对于`InputLocation`，请指定您的推理数据的 Amazon S3 位置。使用这个*或者*`Body`，但不能两者兼而有之。
+ 对于 `EndpointName`，请指定端点的名称。
+ （可选）对于 `InvocationTimeoutSeconds`，您可以设置请求的最大超时时间。您可以分别为每个请求设置此值，最大为 3600 秒（1 小时）。如果您未在请求中指定此字段，则默认情况下，请求超时时间为 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 调用的响应。