

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

# 使用提示指令微调大型语言模型（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)。

经过微调的 LAnguage 网络 (FLAN) 模型使用指令调整来使模型更适合解决一般的下游 NLP 任务。Amazon 在 FLAN 模型系列中 SageMaker JumpStart 提供了许多基础模型。例如，FLAN-T5 模型针对广泛的任务进行了指令微调，以提高各种常见使用案例的零样本性能。通过额外的数据和微调，基于指令的模型可以针对在预训练期间未考虑过的更具体的任务进一步进行调整。

要在特定任务中使用提示 - 响应对任务指令对 LLM 进行微调：

1. 在 JSON 文件中编写指令。有关提示 - 响应对文件所需格式和数据文件夹结构的更多信息，请参阅 [准备并上传训练数据，以便进行基于指令的微调](#jumpstart-foundation-models-fine-tuning-instruction-based-prepare-data)。

1. 创建您的微调训练作业。有关说明，请参阅[创建基于指令的微调的训练作业](#jumpstart-foundation-models-fine-tuning-instruction-based-train)。

您可以在中找到 end-to-end示例[示例笔记本](#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 Base
+ Flan-T5 Large
+ Flan-T5 Small
+ 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 行文本文件格式提供，其中每一行都是一个字典。所有训练数据必须放在一个文件夹中。此文件夹可包含多个 .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 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>

有关基于指令的微调的更多信息，请参阅以下示例笔记本：
+ [微调 LLa MA 2 机型 JumpStart](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)