

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 `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` 變更為以推論元件為基礎，而不是預設的以模型為基礎端點。

```
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))
```

如需公用程式的詳細資訊，請參閱[公用程式 API](https://sagemaker.readthedocs.io/en/stable/api/utility/index.html)。