

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

# 使用者指南
<a name="jumpstart-curated-hubs-user-guide"></a>

下列主題說明如何在 Amazon SageMaker JumpStart 策劃的模型中樞中存取和使用模型。了解如何透過 Amazon SageMaker Studio 介面或使用 SageMaker Python SDK 以程式設計方式存取您的彙整中樞模型。此外，了解如何微調彙整中樞模型，以根據您的特定使用案例和業務需求進行調整。

**Topics**
+ [存取 Amazon SageMaker JumpStart 中的彙整模型中樞](jumpstart-curated-hubs-access-hubs.md)
+ [微調彙整中樞模型](jumpstart-curated-hubs-fine-tune.md)

# 存取 Amazon SageMaker JumpStart 中的彙整模型中樞
<a name="jumpstart-curated-hubs-access-hubs"></a>

您可以透過 Studio 或 SageMaker Python SDK 存取私有模型中樞。

## 在 Studio 中存取私有模型中樞
<a name="jumpstart-curated-hubs-user-guide-studio"></a>

**重要**  
自 2023 年 11 月 30 日起，先前的 Amazon SageMaker Studio 體驗現在名為 Amazon SageMaker Studio Classic。下節專門介紹如何使用更新的 Studio 體驗。如需使用 Studio Classic 應用程式的資訊，請參閱 [Amazon SageMaker Studio Classic](studio.md)。

在 Amazon SageMaker Studio 中，透過**首頁**或左側面板的**首頁**功能表開啟 JumpStart 登陸頁面。這會開啟 **SageMaker JumpStart** 登陸頁面，您可以在其中探索模型中樞及搜尋模型。
+ 從**首頁**中，選擇**預先建置和自動化解決方案**窗格中的 **JumpStart**。
+ 從左側面板的**首頁**功能表中，導覽至 **JumpStart** 節點。

如需開始使用 Amazon SageMaker Studio 的詳細資訊，請參閱[Amazon SageMaker Studio](studio-updated.md)。

從 Studio 的 **SageMaker JumpStart** 登陸頁面，您可以探索包含您組織允許清單中模型的任何私有模型中樞。如果您只能存取一個模型中樞，則 **SageMaker JumpStart** 登陸頁面會直接帶您進入該中樞。如果您可存取多個中樞，則您會進入**中樞**頁面。

如需在 Studio 中微調、部署和評估您可存取之模型的詳細資訊，請參閱[在 Studio 中使用基礎模型](jumpstart-foundation-models-use-studio-updated.md)。

## 使用 SageMaker Python SDK 存取私有模型中樞
<a name="jumpstart-curated-hubs-user-guide-sdk"></a>

您可以使用 SageMaker Python SDK 存取私有模型中樞。您的管理員會提供您讀取、使用或編輯您彙整中樞的存取權。

**注意**  
如果跨帳戶共中樞用，則 `HUB_NAME` 必須是中樞 ARN。如果未跨帳戶共用中樞，則 `HUB_NAME` 會是中樞名稱。

1. 安裝 SageMaker Python SDK 並匯入必要的 Python 套件。

   ```
   # Install the SageMaker Python SDK
       !pip3 install sagemaker --force-reinstall --quiet
       
       # Import the necessary Python packages
       import boto3
       from sagemaker import Session
       from sagemaker.jumpstart.hub.hub import Hub
       from sagemaker.jumpstart.model import JumpStartModel
       from sagemaker.jumpstart.estimator import JumpStartEstimator
   ```

1. 初始化 SageMaker AI 工作階段，並使用中樞名稱和區域連線到您的私有中樞。

   ```
   # If a hub is shared across accounts, then the HUB_NAME must be the hub ARN
       HUB_NAME="Example-Hub-ARN" 
       REGION="us-west-2" 
       
       # Initialize a SageMaker session
       sm_client = boto3.client('sagemaker') 
       sm_runtime_client = boto3.client('sagemaker-runtime') 
       session = Session(sagemaker_client=sm_client, 
                           sagemaker_runtime_client=sm_runtime_client)
       
       # Initialize the private hub
       hub = Hub(hub_name=HUB_NAME, sagemaker_session=session)
   ```

1. 連線至私有中樞後，您可以使用下列命令列出該中樞中的所有可用模型：

   ```
   response = hub.list_models()
       models = response["hub_content_summaries"]
       while response["next_token"]:
           response = hub.list_models(next_token=response["next_token"])
           models.extend(response["hub_content_summaries"])
           
       print(models)
   ```

1. 您可以利用下列命令使用模型名稱取得特定模型的詳細資訊：

   ```
   response = hub.describe_model(model_name="example-model")
       print(response)
   ```

如需使用 SageMaker Python SDK 微調和部署您可存取模型的詳細資訊，請參閱[搭配 SageMaker Python SDK 使用基礎模型](jumpstart-foundation-models-use-python-sdk.md)。

# 微調彙整中樞模型
<a name="jumpstart-curated-hubs-fine-tune"></a>

在私有彙整模型中樞中，您可以使用模型參考執行微調訓練任務。模型參考指向 SageMaker AI 公有中樞內公開可用的 JumpStart 模型，但您可以針對特定使用案例根據自己的資料微調模型。微調任務後，您便可以存取模型權重，然後就能使用權重或部署到端點。

使用 SageMaker Python SDK，您可以僅用數行程式碼來微調彙整中樞模型。如需微調公開可用 JumpStart 模型的更多一般資訊，請參閱[用於微調的基礎模型和超參數](jumpstart-foundation-models-fine-tuning.md)。

## 先決條件
<a name="jumpstart-curated-hubs-fine-tune-prereqs"></a>

為了在彙整中樞內微調 JumpStart 模型參考，請執行下列動作：

1. 請確定使用者的 IAM 角色已連接 SageMaker AI `TrainHubModel` 許可。如需詳細資訊，請參閱《AWS IAM 使用者指南》**中的[新增和移除 IAM 身分許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。

   您應將如下列範例的政策連接至您使用者的 IAM 角色。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Sid": "VisualEditor0",
               "Effect": "Allow",
               "Action": "sagemaker:TrainHubModel",
               "Resource": "arn:aws:sagemaker:*:111122223333:hub/*"
           }
       ]
   }
   ```

------
**注意**  
如果您的彙整中樞是跨帳戶共用，且中樞內容是由另一個帳戶所擁有，請確定您的 `HubContent` (模型參考資源) 具有資源型 IAM 政策，該政策也會將 `TrainHubModel` 許可授予請求帳戶，如下列範例所示。  

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Sid": "AllowCrossAccountSageMakerAccess",
               "Effect": "Allow",
               "Principal": {
                   "AWS": "arn:aws:iam::111122223333:root"
               },
               "Action": [
                   "sagemaker:TrainHubModel"
               ],
               "Resource": [
                   "arn:aws:sagemaker:*:111122223333:hub/*"
               ]
           }
       ]
   }
   ```

1. 擁有私有彙整中樞，其中包含所要微調 JumpStart 模型的模型參考。如需建立私有中樞的詳細資訊，請參閱[建立私有模型中樞](jumpstart-curated-hubs-admin-guide-create.md)。若要了解如何將公開可用的 JumpStart 模型新增至私有中樞，請參閱[將模型新增至私有中樞](jumpstart-curated-hubs-admin-guide-add-models.md)。
**注意**  
您選擇的 JumpStart 模型應該可以微調。您可以透過查看[具備預先訓練模型表的內建演算法](https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html)來確認某個模型是否可微調。

1. 具有您想要用來微調模型的訓練資料集。資料集應該採用適合所要微調模型的訓練格式。

## 微調彙整中樞模型參考
<a name="jumpstart-curated-hubs-fine-tune-pysdk"></a>

下列程序說明如何使用 SageMaker Python SDK 在私有彙整中樞內微調模型參考。

1. 請確定您已安裝最新版本的 SageMaker Python SDK (至少 `2.242.0` 版)。如需詳細資訊，請參閱[使用 SageMaker Python SDK 的版本 2.x](https://sagemaker.readthedocs.io/en/stable/v2.html)。

   ```
   !pip install --upgrade sagemaker
   ```

1. 從 SageMaker Python SDK 匯入您需要的 適用於 Python (Boto3) 的 AWS SDK 和模組。

   ```
   import boto3
   from sagemaker.jumpstart.estimator import JumpStartEstimator
   from sagemaker.session import Session
   ```

1. 初始化 Boto3 工作階段、SageMaker AI 用戶端和 SageMaker Python SDK 工作階段。

   ```
   sagemaker_client = boto3.Session(region_name=<AWS-region>).client("sagemaker")
   sm_session = Session(sagemaker_client=sagemaker_client)
   ```

1. 建立 `JumpStartEstimator` 並提供 JumpStart 模型 ID、包含模型參考的中樞名稱，以及 SageMaker Python SDK 工作階段。如需模型 ID 的清單，請參閱[具備預先訓練模型表的內建演算法](https://sagemaker.readthedocs.io/en/stable/doc_utils/pretrainedmodels.html)。

   您可以選擇性地在建立估算器時指定 `instance_type` 和 `instance_count` 欄位。如果您沒有這麼做，訓練任務會使用您正使用模型的預設執行個體類型和計數。

   您也可以選擇性地指定您要存放經微調的模型權重之 Amazon S3 位置的 `output_path`。如果您未指定 `output_path`，則會將預設 SageMaker AI Amazon S3 儲存貯體用於您帳戶中的區域，並採用以下格式命名：`sagemaker-<region>-<account-id>`。

   ```
   estimator = JumpStartEstimator(
       model_id="meta-textgeneration-llama-3-2-1b",
       hub_name=<your-hub-name>,
       sagemaker_session=sm_session, # If you don't specify an existing session, a default one is created for you
       # Optional: specify your desired instance type and count for the training job
       # instance_type = "ml.g5.2xlarge"
       # instance_count = 1
       # Optional: specify a custom S3 location to store the fine-tuned model artifacts
       # output_path: "s3://<output-path-for-model-artifacts>"
   )
   ```

1. 使用 `training` 索引鍵建立一個字典，您會在該索引鍵中指定微調資料集的位置。此範例指向 Amazon S3 URI。如果您有其他考量，例如使用本機模式或多個訓練資料通道，請參閱 SageMaker Python SDK 文件中的 [ JumpStartEstimator.fit()](https://sagemaker.readthedocs.io/en/stable/api/training/estimators.html#sagemaker.jumpstart.estimator.JumpStartEstimator.fit) 以取得詳細資訊。

   ```
   training_input = {
       "training": "s3://<your-fine-tuning-dataset>"
   }
   ```

1. 呼叫估算器的 `fit()` 方法，傳入您的訓練資料並接受 EULA (若適用的話)。
**注意**  
下列範例會設定 `accept_eula=False.`，您應該手動將其值變更為 `True`，以接受 EULA。

   ```
   estimator.fit(inputs=training_input, accept_eula=False)
   ```

您的微調任務現在應該開始了。

您可以檢查您的微調任務，方法是在 SageMaker AI 主控台中或使用 [ListTrainingJobs](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ListTrainingJobs.html) API 檢視您的訓練任務。

您可以在 `JumpStartEstimator` 物件中指定的 Amazon S3 `output_path` 處 (區域的預設 SageMaker AI Amazon S3 儲存貯體，或您指定的自訂 Amazon S3 路徑，以適用者為主) 存取經微調的模型成品。