

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

# 使用 API 建立微調文字生成模型的 AutoML 任務
<a name="autopilot-create-experiment-finetune-llms"></a>

大型語言模型 (LLM) 在多種生成任務中脫穎而出，包括文字生成、摘要、完稿、問答等。之所以能有如此表現，可以歸因於其規模龐大，並對各種資料集和各類任務進行了廣泛的訓練。但是，醫療保健和金融服務等特定領域可能需要進行自訂微調，以適應獨特的資料和使用案例。透過根據特定領域量身打造的訓練，LLM 可以提高效能，並為目標應用程式提供更準確的輸出。

Autopilot 提供一系列微調預先訓練的生成文字模型的功能。明確來說，Autopilot 支援一系列由 JumpStart 提供支援的通用大型語言模型 (LLM) 進行**指令微調**。

**注意**  
現階段，可在 Autopilot 中進行微調的文字生成模型，僅在 SageMaker Canvas 提供服務的區域內可用。如需[支援區域的完整清單](https://docs.aws.amazon.com/sagemaker/latest/dg/canvas.html)，請參閱 SageMaker Canvas 的文件。

微調預先訓練的模型需要有明確指令的特定資料集，以指導模型如何生成輸出或針對任務執行動作。該模型從資料集中學習，調整其參數以符合提供的說明。指令微調包含使用格式設定為提示-回應配對，並以指令形式表達的標記範例。如需微調的詳細資訊，請參閱[微調基礎模型](https://docs.aws.amazon.com/sagemaker/latest/dg/jumpstart-foundation-models-fine-tuning.html)。

以下指南描述建立 Amazon SageMaker Autopilot 任務作為前導實驗的過程，藉由 SageMaker [API 參考](https://docs.aws.amazon.com/sagemaker/latest/dg/autopilot-reference.html)微調文字生成 LLM。

**注意**  
文字和影像分類、時間序列預測以及大型語言模型的微調等任務，僅能透過 [AutoML REST API](autopilot-reference.md) 第 2 版獨家取得。如果您選擇的語言是 Python，您可以直接參考 [適用於 Python (Boto3) 的 AWS SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker/client/create_auto_ml_job_v2.html) 或 Amazon SageMaker Python SDK 的 [AutoMLV2 物件](https://sagemaker.readthedocs.io/en/stable/api/training/automlv2.html#sagemaker.automl.automlv2.AutoMLV2)。  
偏好使用者介面便利性的使用者，可以使用 [Amazon SageMaker Canvas](https://docs.aws.amazon.com/sagemaker/latest/dg/canvas-getting-started.html) 存取預先訓練的模型、生成式 AI 基礎模型，或建立針對特定文字、影像分類、預測需求或生成式 AI 量身打造的自訂模型。

若要以程式設計方式建立 Autopilot 實驗來微調 LLM，您可以使用 Amazon SageMaker Autopilot 支援的任何語言或 AWS CLI來呼叫 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html) API。

有關此 API 動作如何以您選擇的語言轉換為函式的詳細資訊，請參閱 `CreateAutoMLJobV2` 的[另請參閱](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#API_CreateAutoMLJobV2_SeeAlso)章節，並選擇 SDK。例如，對於 Python 使用者，請參閱 適用於 Python (Boto3) 的 AWS SDK中的`[create\_auto\_ml\_job\_v2](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_auto_ml_job_v2)`的完整請求語法。

**注意**  
Autopilot 在微調大型語言模型時，無需訓練和評估多個候選模型。相反地，Autopilot 會使用您的資料集直接微調您的目標模型，以增強預設目標指標，即交叉熵損失。在 Autopilot 中微調語言模型時，無需設定 `AutoMLJobObjective`欄位。

一旦您的 LLM 經過微調，您可以在進行 `[DescribeAutoMLJobV2](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeAutoMLJobV2.html)` API 呼叫時，透過 `[BestCandidate](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CandidateProperties.html#sagemaker-Type-CandidateProperties-CandidateMetrics)` 存取各種 ROUGE 分數來評估其效能。該模型還提供了有關其訓練和驗證損失以及困惑度的資訊。如需評估經微調的模型所產生文字品質的完整指標清單，請參閱[用於微調 Autopilot 中大型語言模型的指標](autopilot-llms-finetuning-metrics.md)。

## 先決條件
<a name="autopilot-llms-finetuning-api-prerequisites"></a>

使用 Autopilot 在 SageMaker AI 中建立微調實驗之前，請務必執行下列步驟：
+ (選用) 選擇您要微調的預先訓練模型。

  如需 Amazon SageMaker Autopilot 中可用於微調的預先訓練模型清單，請參閱[支援大型語言模型進行微調](autopilot-llms-finetuning-models.md)。選擇模型並非強制要求；如果未指定模型，Autopilot 將自動預設為 *Falcon7BInstruct*。
+ 建立指令的資料集。請參閱[資料集檔案類型與輸入資料格式](autopilot-llms-finetuning-data-format.md)，以了解指令型資料集的格式需求。
+ 將您的資料集放入 Amazon S3 儲存貯體。
+ 授予對 Amazon S3 儲存貯體的完整存取權，該儲存貯體包含用於執行實驗的 SageMaker AI 執行角色的輸入資料。
  + 如需有關擷取 SageMaker AI 執行角色的資訊，請參閱[取得您的執行角色](sagemaker-roles.md#sagemaker-roles-get-execution-role)。
  + 如需授予 SageMaker AI 執行角色存取 Amazon S3 中一或多個特定儲存貯體的相關資訊，請參閱[建立執行角色](sagemaker-roles.md#sagemaker-roles-create-execution-role)中的*新增額外的 Amazon S3 權限給 SageMaker AI 執行角色*。
+ 此外，您應該為執行角色提供必要的權限，以存取 JumpStart 使用的預設儲存 Amazon S3 儲存貯體。儲存和擷取 JumpStart 預先訓練模型成品時，需要此存取權。若要授予此 Amazon S3 儲存貯體的存取權，您必須在執行角色上建立新的內嵌自訂政策。

  以下是在 `us-west-2` 中設定 AutoML 微調任務時，您可以在 JSON 編輯器中使用的範例政策：

  *JumpStart 的儲存貯體名稱遵循取決於 的預定模式 AWS 區域。您必須相應地調整儲存貯體的名稱。*

  ```
  {
      "Sid": "Statement1",
      "Effect": "Allow",
      "Action": [
          "s3:GetObject",
          "s3:PutObject",
          "s3:ListBucket"
      ],
      "Resource": [
          "arn:aws:s3:::jumpstart-cache-prod-{{us-west-2}}",
          "arn:aws:s3:::jumpstart-cache-prod-{{us-west-2/*}}"
      ]
  }
  ```

完成此操作後，您可以透過這個執行角色的 ARN 在 Autopilot API 請求中使用。

## 必要參數
<a name="autopilot-llms-finetuning-api-required-params"></a>

當呼叫 `[CreateAutoMLJobV2](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html)` 以建立 LLM 微調的 Autopilot 實驗時，您必須提供以下值：
+ 用於指定任務的名稱的 `[AutoMLJobName](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#API_CreateAutoMLJobV2_RequestSyntax)`。名稱應為類型`string`，並且最小長度為 1 個字元，最大長度為 32 個字元。
+ 至少在`[AutoMLJobInputDataConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#sagemaker-CreateAutoMLJobV2-request-AutoMLJobInputDataConfig)`中有一個`training`類型的`[AutoMLJobChannel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLJobChannel.html)`。此通道指定微調資料集的 Amazon S3 儲存貯體名稱。您有選擇定義`validation`通道的選項。如果未提供驗證通道，且在 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLDataSplitConfig.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLDataSplitConfig.html) 中設定了 `ValidationFraction`，則會使用此分數將訓練資料集隨機劃分為訓練和驗證集。此外，您可以指定資料集的內容類型 (CSV 或 Parquet 檔案)。
+ `[TextGenerationJobConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_TextGenerationJobConfig.html)` 類型的 `[AutoMLProblemTypeConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#sagemaker-CreateAutoMLJobV2-request-AutoMLProblemTypeConfig)` 用於配置訓練任務的設定。

  特別是，您可以在 `BaseModelName` 欄位中指定要微調的基本模型名稱。如需 Amazon SageMaker Autopilot 中可用於微調的預訓練模型清單，請參閱[支援大型語言模型進行微調](autopilot-llms-finetuning-models.md)。
+ 以 `[OutputDataConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLOutputDataConfig.html)` 指定 Amazon S3 輸出路徑，以存放 AutoML 任務的成品。
+ `[RoleArn](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJob.html#sagemaker-CreateAutoMLJob-request-RoleArn)` 用來指定用於存取您的資料的角色的 ARN。

以下是對 `CreateAutoMLJobV2` 進行 API 呼叫以微調 (`Falcon7BInstruct`) 模型時，所使用的完整請求格式範例。

```
{
   "AutoMLJobName": "<job_name>",
   "AutoMLJobInputDataConfig": [ 
      { 
         "ChannelType": "training",
         "CompressionType": "None",
         "ContentType": "text/csv", 
         "DataSource": { 
            "S3DataSource": { 
               "S3DataType": "S3Prefix",
               "S3Uri": "s3://<bucket_name>/<input_data>.csv"
            }
         }
      }
   ],
  "OutputDataConfig": {
      "S3OutputPath": "s3://<bucket_name>/output",
      "KmsKeyId": "arn:aws:kms:<region>:<account_id>:key/<key_value>"
   },
   "RoleArn":"arn:aws:iam::<account_id>:role/<sagemaker_execution_role_name>",
   "AutoMLProblemTypeConfig": {
        "TextGenerationJobConfig": {
            "BaseModelName": "Falcon7BInstruct"
       }
   }
}
```

所有其他參數都是選用參數。

## 選用的參數
<a name="autopilot-llms-finetuning-api-optional-params"></a>

以下各章節提供一些選用參數的詳細資訊，您可以將這些參數傳送至您的微調 AutoML 任務。

### 如何指定 AutoML 任務的訓練和驗證資料集
<a name="autopilot-llms-finetuning-data-training-or-validation"></a>

您可以提供自己的驗證資料集和自訂資料分割比例，或讓 Autopilot 自動分割資料集。

每個 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLJobChannel.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLJobChannel.html) 物件 (請參閱 [AutomlJobinputDataConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#sagemaker-CreateAutoMLJobV2-request-AutoMLJobInputDataConfig) 所需的參數) 都有一個 `ChannelType`，可以設定為 `training` 或 `validation` 值，指定建構機器學習模型時如何使用資料。

至少必須提供一個資料來源，最多允許兩個資料來源：一個用於訓練資料，另一個用於驗證資料。將資料分割為訓練和驗證資料集的方式，取決於您有一個或兩個資料來源。
+ 如果您只有**一個資料來源**，則`ChannelType`依預設會將其設定為`training`，且必須具有此值。
  + 如果未設定 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLDataSplitConfig.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLDataSplitConfig.html) 中的 `ValidationFraction` 值，則預設會使用來自此來源的 0.2 (20%) 資料進行驗證。
  + 如果設定`ValidationFraction`為介於 0 和 1 之間的值，則會根據指定的值來分割資料集，其中值會指定用於驗證的資料集分數。
+ 如果您有**兩個資料來源**，則必須將其中一個`AutoMLJobChannel`物件的`ChannelType`設定為`training`，即預設值。其他資料來源的`ChannelType`必須設定為`validation`。這兩個資料來源必須具有相同的格式 (CSV 或 Parquet)，以及相同的結構描述。在這種情況下，您不得設定`ValidationFraction`的值，因為每個來源的所有資料都會用於訓練或驗證。設定此值會導致錯誤。

### 如何啟用自動部署
<a name="autopilot-llms-finetuning-auto-model-deployment"></a>

Autopilot 可讓您將經微調的模型自動部署到端點。若要啟用經微調的模型的自動部署，請在 AutoML 任務請求中包括`[ModelDeployConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#sagemaker-CreateAutoMLJobV2-request-ModelDeployConfig)`。這可讓您將經微調的模型部署到 SageMaker AI 端點。以下是可用於自訂的組態。
+ 若要讓 Autopilot 產生端點名稱，請將`[AutoGenerateEndpointName](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelDeployConfig.html#API_ModelDeployConfig_Contents)`設定為`True`。
+ 若要為端點提供您自己的名稱，請設定`[AutoGenerateEndpointName](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelDeployConfig.html#API_ModelDeployConfig_Contents) to False and provide a name of your choice in [EndpointName](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelDeployConfig.html#API_ModelDeployConfig_Contents)`。

### 使用 AutoML API 微調模型時如何設定接受 EULA
<a name="autopilot-llms-finetuning-set-eula"></a>

對於需要在微調之前接受終端使用者授權協議的模型，您可以在設定 `[AutoMLProblemTypeConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#sagemaker-CreateAutoMLJobV2-request-AutoMLProblemTypeConfig)` 時將 `[TextGenerationJobConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_TextGenerationJobConfig.html)` 中 `[ModelAccessConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ModelAccessConfig.html)` 的 `AcceptEula` 屬性設定為 `True`，以接受 EULA。

### 如何設定超參數以最佳化模型的學習過程
<a name="autopilot-llms-finetuning-set-hyperparameters"></a>

設定 `[AutoMLProblemTypeConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#sagemaker-CreateAutoMLJobV2-request-AutoMLProblemTypeConfig)` 時，您可以在 `[TextGenerationJobConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_TextGenerationJobConfig.html)` 的 `TextGenerationHyperParameters` 屬性中設定超參數值，用以最佳化文字生成模型的學習過程。

Autopilot 允許在所有模型中設定四個常見的超參數。
+ `epochCount`：其值應為字串，包含 `1` 到 `10` 範圍內的整數值。
+ `batchSize`：其值應為字串，包含 `1` 到 `64` 範圍內的整數值。
+ `learningRate`：其值應為字串，包含 `0` 到 `1` 範圍內的浮點值。
+ `learningRateWarmupSteps`：其值應為字串，包含 `0` 到 `250` 範圍內的整數值。

如需每個超參數的詳細資訊，請參閱[用於最佳化文字生成模型學習過程的超參數](autopilot-llms-finetuning-hyperparameters.md)。

下列 JSON 範例顯示傳遞至 TextGenerationJobConfig 的 `TextGenerationHyperParameters` 欄位，其中所有四個超參數都已設定。

```
"AutoMLProblemTypeConfig": {
  "TextGenerationJobConfig": {
    "BaseModelName": "Falcon7B",
    "TextGenerationHyperParameters": {"epochCount":"5", "learningRate":"0.000001", "batchSize": "32", "learningRateWarmupSteps": "10"}
  }
}
```