

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

# 使用提示指示微調大型語言模型 (LLM)
<a name="jumpstart-foundation-models-fine-tuning-instruction-based"></a>

指令式微調使用標籤的範例來改善預先訓練的基礎模型在特定任務上的效能。帶有標籤的範例依指令被格式化為提示、回應和用詞。此微調程序會改變模型的權重。有關指令式微調的更多資訊，請參閱文獻[FLAN 簡介：更通用的指令微調語言模型](https://ai.googleblog.com/2021/10/introducing-flan-more-generalizable.html)和[擴展指令微調的語言模型。](https://arxiv.org/abs/2210.11416)

微調語言網路 (FLAN) 模型使用指令調整，讓模型更適合解決一般下游 NLP 任務。Amazon SageMaker JumpStart 在 FLAN 模型系列中提供了許多基礎模型。例如，FLAN-T5 模型會針對各種任務進行指令微調，以提升各種常見使用案例的零樣本表現。透過額外的資料和微調，指令式模型可以進一步適應訓練期間沒考慮到的更具體任務。

若要使用提示-回應對任務指示微調特定任務上的 LLM：

1. 在 JSON 檔案中準備您的指示。如需提示-回應對檔案所需格式和資料資料夾結構的詳細資訊，請參閱[準備和上傳訓練資料以進行指令微調](#jumpstart-foundation-models-fine-tuning-instruction-based-prepare-data)。

1. 建立您的微調訓練任務。如需說明，請參閱[為指令微調建立訓練任務](#jumpstart-foundation-models-fine-tuning-instruction-based-train)。

您可以在 [範例筆記本](#jumpstart-foundation-models-fine-tuning-instruction-based-examples) 中找到端對端範例。

只有一部分的 JumpStart 基礎模型與指令微調相容。指令式網域適應性微調適用於下列基礎模型：

**注意**  
某些 JumpStart 基礎模型 (例如 Llama 2 7B) 需要先接受終端使用者授權協議，才能微調和執行推論。如需詳細資訊，請參閱[終端使用者授權協議](jumpstart-foundation-models-choose.md#jumpstart-foundation-models-choose-eula)。
+ Flan-T5 基本
+ Flan-T5 大
+ Flan-T5 小
+ Flan-T5 XL
+ Flan-T5 XXL
+ Llama 2 13B
+ Llama 2 13B Chat
+ Llama 2 13B Neuron
+ Llama 2 70B
+ Llama 2 70B Chat
+ Llama 2 7B
+ Llama 2 7B Chat
+ Llama 2 7B Neuron
+ Mistral 7B
+ RedPajama INCITE Base 3B V1
+ RedPajama INCITE Base 7B V1
+ RedPajama INCITE Chat 3B V1
+ RedPajama INCITE Chat 7B V1
+ RedPajama INCITE Instruct 3B V1
+ RedPajama INCITE Instruct 7B V1

## 準備和上傳訓練資料以進行指令微調
<a name="jumpstart-foundation-models-fine-tuning-instruction-based-prepare-data"></a>

指令微調的訓練資料必須以 JSON Lines 文字檔案格式提供，其中每一行都是字典。所有訓練資料都必須位於單一資料夾中。資料夾可以包含多個 .jsonl 檔案。

訓練資料夾也可以包含範本 JSON 檔案 (`template.json`)，該檔案描述資料的輸入和輸出格式。如果未提供任何範本檔案，則會使用下列範本檔案：

```
{
  "prompt": "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Input:\n{context}",
  "completion": "{response}"
}
```

根據 `template.json` 檔案，訓練資料的每個 .jsonl 項目都必須包含 `{instruction}`、`{context}`和 `{response}` 欄位。

如果您提供自訂範本 JSON 檔案，請使用 `"prompt"` 和 `"completion"` 金鑰來定義您自己的必要欄位。根據下列自訂範本 JSON 檔案，訓練資料的每個 .jsonl 項目都必須包含 `{question}`、`{context}` 和 `{answer}` 欄位：

```
{
  "prompt": "question: {question} context: {context}",
  "completion": "{answer}"
}
```

### 分割用於訓練和測試的資料
<a name="jumpstart-foundation-models-fine-tuning-instruction-based-split-data"></a>

您可以選擇性地提供另一個包含驗證資料的資料夾。此資料夾也應該包含一或多個 .jsonl 檔案。如果沒有提供驗證資料集，則會保留某個數量的訓練資料供驗證之用。當您選擇超參數來微調模型時，可以調整用於驗證的訓練資料百分比。

### 將微調資料上傳至 Amazon S3
<a name="jumpstart-foundation-models-fine-tuning-instruction-based-upload-data"></a>

將準備好的資料上傳至 Amazon Simple Storage Service (Amazon S3)，以便在微調 JumpStart 基礎模型時使用。您可以使用下列命令上傳資料：

```
from sagemaker.s3 import S3Uploader
import sagemaker
import random

output_bucket = sagemaker.Session().default_bucket()
local_data_file = {{"train.jsonl"}}
train_data_location = f{{"s3://{output_bucket}/dolly_dataset"}}
S3Uploader.upload(local_data_file, train_data_location)
S3Uploader.upload("template.json", train_data_location)
print(f"Training data: {train_data_location}")
```

## 為指令微調建立訓練任務
<a name="jumpstart-foundation-models-fine-tuning-instruction-based-train"></a>

將資料上傳至 Amazon S3 之後，您可以微調和部署 JumpStart 基礎模型。若要在 Studio 中微調模型，請參閱[在 Studio 中微調模型](jumpstart-foundation-models-use-studio-updated-fine-tune.md)。若要使用 SageMaker Python SDK 微調模型，請參閱[使用 `JumpStartEstimator` 類別微調公開可用的基礎模型](jumpstart-foundation-models-use-python-sdk-estimator-class.md)。

## 範例筆記本
<a name="jumpstart-foundation-models-fine-tuning-instruction-based-examples"></a>

如需指令微調的詳細資訊，請參閱下列範例筆記本：
+ [在 JumpStart 微調 LLaMA 2 模型](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/llama-2-finetuning.html)
+ [SageMaker JumpStart 簡介 - 使用 Mistral 模型產生文字](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/mistral-7b-instruction-domain-adaptation-finetuning.html)
+ [SageMaker JumpStart 簡介 - 使用 Falcon 模型產生文字](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/falcon-7b-instruction-domain-adaptation-finetuning.html)
+ [SageMaker JumpStart 基礎模型 - HuggingFace Text2Text 指令微調](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/jumpstart-foundation-models/instruction-fine-tuning-flan-t5.html)