

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

# 使用 `JumpStartModel` 类部署公开可用的基础模型
<a name="jumpstart-foundation-models-use-python-sdk-model-class"></a>

使用 SageMaker Python SDK，只需几行代码即可将内置算法或预训练模型部署到 SageMaker AI 终端节点。

1. 首先，在[内置算法与预训练模型表](https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html)中找到所选模型的模型 ID。

1. 使用模型 ID 将您的模型定义为 JumpStart 模型。

   ```
   from sagemaker.jumpstart.model import JumpStartModel
   
   model_id = "huggingface-text2text-flan-t5-xl"
   my_model = JumpStartModel(model_id=model_id)
   ```

1. 使用 `deploy` 方法自动部署模型进行推理。在本例中，我们使用了来自 Hugging Face 的 FLAN-T5 XL 模型。

   ```
   predictor = my_model.deploy()
   ```

1. 然后，您就可以使用 `predict` 方法对已部署的模型进行推理。

   ```
   question = "What is Southern California often abbreviated as?"
   response = predictor.predict(question)
   print(response)
   ```

**注意**  
此示例使用的基础模型 FLAN-T5 XL 适用于各种文本生成使用场景，包括问题解答、摘要、聊天机器人创建等。有关模型使用场景的更多信息，请参阅 [可用的基础模型](jumpstart-foundation-models-latest.md)。

有关该`JumpStartModel `类及其参数的更多信息，请参见[JumpStartModel](https://sagemaker.readthedocs.io/en/stable/api/inference/model.html#sagemaker.jumpstart.model.JumpStartModel)。

## 检查默认实例类型
<a name="jumpstart-foundation-models-use-python-sdk-model-class-instance-types"></a>

在使用 `JumpStartModel` 类对预训练模型进行部署时，您可以选择包含特定的模型版本或实例类型。所有 JumpStart 模型都有默认的实例类型。使用以下代码读取默认部署实例类型：

```
from sagemaker import instance_types

instance_type = instance_types.retrieve_default(
    model_id=model_id,
    model_version=model_version,
    scope="inference")
print(instance_type)
```

使用`instance_types.retrieve()`方法查看给定 JumpStart 模型的所有支持的实例类型。

## 使用推理组件将多个模型部署到共享端点
<a name="jumpstart-foundation-models-use-python-sdk-model-class-endpoint-types"></a>

推理组件是一个 SageMaker AI 托管对象，可用于将一个或多个模型部署到终端节点，以提高灵活性和可扩展性。您必须将 JumpStart 模型更改`endpoint_type`为， inference-component-based而不是默认的基于模型的端点。

```
predictor = my_model.deploy(
    endpoint_name = 'jumpstart-model-id-123456789012', 
    endpoint_type = EndpointType.INFERENCE_COMPONENT_BASED
)
```

有关使用推理组件创建端点和部署 SageMaker AI 模型的更多信息，请参阅[多种模式的资源共享利用](realtime-endpoints-deploy-models.md#deployed-shared-utilization)。

## 检查有效的输入和输出推理格式
<a name="jumpstart-foundation-models-use-python-sdk-model-class-input-output"></a>

要检查有效的数据输入和输出格式以进行推理，您可以使用 `Serializers` 和 `Deserializers` 类中的 `retrieve_options()` 方法。

```
print(sagemaker.serializers.retrieve_options(model_id=model_id, model_version=model_version))
print(sagemaker.deserializers.retrieve_options(model_id=model_id, model_version=model_version))
```

## 检查支持的内容和接受类型
<a name="jumpstart-foundation-models-use-python-sdk-model-class-content-types"></a>

同样，您也可以使用 `retrieve_options()` 方法来检查模型支持的内容和接受类型。

```
print(sagemaker.content_types.retrieve_options(model_id=model_id, model_version=model_version))
print(sagemaker.accept_types.retrieve_options(model_id=model_id, model_version=model_version))
```

有关实用程序的更多信息，请参阅[实用工具 APIs](https://sagemaker.readthedocs.io/en/stable/api/utility/index.html)。