

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

# 部署生成式 AI 推理建议
<a name="generative-ai-inference-recommendations-deploy"></a>

当推荐作业完成时，每项建议都包含部署就绪配置。您可以通过 AI Studio 中的单个操作将所选配置部署到 SageMaker AI 推理端点，也可以通过 API 以编程方式部署到 SageMaker 人工智能推理端点。

## 了解部署配置
<a name="generative-ai-inference-recommendations-deploy-overview"></a>

工作响应中的每条建议都包含一个包含以下信息的`DeploymentConfiguration`对象：

`ImageUri`  
容器镜像 URI 已针对推荐的实例类型进行了优化。

`InstanceType`  
推荐的部署实例类型。

`InstanceCount`  
达到性能目标所需的实例数量。

`CopyCountPerInstance`  
每个实例要运行的模型副本数。如果设置为大于 1 的值，则会在每个实例上加载模型的多个副本以增加吞吐量。

`EnvironmentVariables`  
为实现最佳性能而配置的环境变量，例如张量 parallel 大小和最大序列长度。

`S3`  
模型工件的 S3 频道引用，包括任何经过优化的模型输出。

## 使用 API 进行部署
<a name="generative-ai-inference-recommendations-deploy-api"></a>

要以编程方式部署推荐，请使用推荐中的模型包创建 A SageMaker I 模型和端点。每项建议都包含一个带有模型包 ARN 和推理规范名称的`ModelDetails`对象。这是最简单的部署路径，因为模型包已经包含容器镜像、环境变量和模型工件通道。

```
import boto3

client = boto3.client("sagemaker", region_name="us-west-2")

# Get the recommendation from a completed job
response = client.describe_ai_recommendation_job(
    AIRecommendationJobName="my-recommendation-job"
)

# Select a recommendation (e.g., the first one)
recommendation = response["Recommendations"][0]
model_details = recommendation["ModelDetails"]
deploy_config = recommendation["DeploymentConfiguration"]

# Create a model from the model package.
# The model package already contains the container image, environment
# variables, and S3 data channels (base model + optimization artifacts).
model_name = "my-recommended-model"
container_def = {
    "ModelPackageName": model_details["ModelPackageArn"],
}
# If the recommendation uses a named inference specification (e.g., for
# a specific optimization variant), specify it so SageMaker selects the
# correct container and instance configuration from the model package.
if model_details.get("InferenceSpecificationName"):
    container_def["InferenceSpecificationName"] = model_details["InferenceSpecificationName"]

client.create_model(
    ModelName=model_name,
    PrimaryContainer=container_def,
    ExecutionRoleArn="arn:aws:iam::111122223333:role/ExampleRole",
)

# Create an endpoint configuration
endpoint_config_name = "my-recommended-endpoint-config"
production_variant = {
    "VariantName": "AllTraffic",
    "ModelName": model_name,
    "InstanceType": deploy_config["InstanceType"],
    "InitialInstanceCount": deploy_config.get("InstanceCount", 1),
}
copy_count = deploy_config.get("CopyCountPerInstance")
if copy_count and copy_count > 1:
    production_variant["InferenceAmiVersion"] = "al2-ami-sagemaker-inference-gpu-2"
    production_variant["RoutingConfig"] = {"RoutingStrategy": "LEAST_OUTSTANDING_REQUESTS"}

client.create_endpoint_config(
    EndpointConfigName=endpoint_config_name,
    ProductionVariants=[production_variant],
)

# Create the endpoint
endpoint_name = "my-recommended-endpoint"
client.create_endpoint(
    EndpointName=endpoint_name,
    EndpointConfigName=endpoint_config_name,
)
print(f"Endpoint {endpoint_name} is being created.")
```

创建终端节点后，您可以使用 `DescribeEndpoint` API 监控其状态，直至其进入`InService`状态。

```
import time

while True:
    response = client.describe_endpoint(EndpointName=endpoint_name)
    status = response["EndpointStatus"]
    print(f"Endpoint status: {status}")
    if status in ("InService", "Failed"):
        break
    time.sleep(60)
```

## 从 SageMaker AI 工作室部署
<a name="generative-ai-inference-recommendations-deploy-studio"></a>

您也可以通过单个操作直接从 SageMaker AI Studio 部署推荐的配置。在 SageMaker AI Studio 中，导航到已完成的推荐作业，查看推荐及其性能指标，然后选择要部署的配置。