

# 准备数据以用于多模态微调
<a name="fine-tune-prepare-data-understanding"></a>

以下是为微调理解模型准备数据要遵循的指南和要求：

1. 微调所需的最小数据量取决于任务（即是复杂任务还是简单任务），但建议至少为希望模型学习的每项任务提供 100 个数据样本。

1. 建议在训练和推理期间，在零样本设置中使用经过优化的提示，以便获得最佳结果。

1. 训练数据集和验证数据集必须是 JSONL 格式的文件，其中的每一行都是与一条记录对应的一个 JSON 对象。这些文件名只能包含字母数字字符、下划线、连字符、斜杠和句点。

1. 图像和视频限制

   1. 数据集不能包含不同的媒体模态。也就是说，数据集可以是带图像的文本，也可以是带视频的文本。

   1. 一个样本（消息中的一条记录）可以有多张图像

   1. 一个样本（消息中的一条记录）只能有一个视频

1. `schemaVersion` 可以是任何字符串值

1. （*可选*）`system` 轮次可以是客户提供的自定义系统提示。

1. 支持的角色为 `user` 和 `assistant`。

1. `messages` 中的第一轮应始终以 `"role": "user"` 开头。最后一轮是机器人的回复，以 "role": "assistant" 表示。

1. `image.source.s3Location.uri` 和 `video.source.s3Location.uri` 必须可供 Amazon Bedrock 访问。

1.  Amazon Bedrock 服务角色必须能够访问 Amazon S3 中的图像文件。有关授予访问权限的更多信息，请参阅 [Create a service role for model customization](https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-iam-role.html) 

1. 图像或视频必须与数据集位于同一个 Amazon S3 存储桶中。例如，若数据集位于 `s3://amzn-s3-demo-bucket/train/train.jsonl` 中，则图像或视频必须位于 `s3://amzn-s3-demo-bucket` 中

1. 术语 `User:`、`Bot:`、`Assistant:`、`System:`、`<image>`、`<video>` 和 `[EOS]` 是保留关键字。如果用户提示或系统提示以这些关键字中的任何一个开头，或在提示中的任意位置使用这些关键字，则训练作业将因数据问题而失败。如果用例需要使用这些关键字，则必须取代具有相似含义的其他关键字，以便可以继续进行培训。

**注意**  
要在提交微调作业之前验证数据集，可以使用 GitHub 上提供的[数据集验证脚本](https://github.com/aws-samples/amazon-nova-samples/tree/main/customization/bedrock-finetuning/understanding/dataset_validation)。

**Topics**
+ [示例数据集格式](#customize-fine-tune-examples)
+ [数据集限制](#custom-fine-tune-constraints)

## 示例数据集格式
<a name="customize-fine-tune-examples"></a>

以下示例数据集格式可供参考。

### 纯文本自定义微调格式
<a name="example4"></a>

以下示例仅适用于对文本进行自定义微调。

```
// train.jsonl
{
  "schemaVersion": "bedrock-conversation-2024",
  "system": [
    {
      "text": "You are a digital assistant with a friendly personality"
    }
  ],
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "What is the capital of Mars?"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "text": "Mars does not have a capital. Perhaps it will one day."
        }
      ]
    }
  ]
}
```

### 单张图像自定义微调格式
<a name="example1"></a>

以下示例适用于对文本和单张图像进行自定义微调。

```
// train.jsonl{
    "schemaVersion": "bedrock-conversation-2024",
    "system": [{
        "text": "You are a smart assistant that answers questions respectfully"
    }],
    "messages": [{
            "role": "user",
            "content": [{
                    "text": "What does the text in this image say?"
                },
                {
                    "image": {
                        "format": "png",
                        "source": {
                            "s3Location": {
                                "uri": "s3://{{your-bucket/your-path/your-image.png}}",
                                "bucketOwner": "{{your-aws-account-id}}"
                            }
                        }
                    }
                }
            ]
        },
        {
            "role": "assistant",
            "content": [{
                "text": "The text in the attached image says 'LOL'."
            }]
        }
    ]
}
```

### 视频自定义微调格式
<a name="example3"></a>

以下示例适用于对文本和视频进行自定义微调。

```
{
    "schemaVersion": "bedrock-conversation-2024",
    "system": [{
        "text": "You are a helpful assistant designed to answer questions crisply and to the point"
    }],
    "messages": [{
            "role": "user",
            "content": [{
                    "text": "How many white items are visible in this video?"
                },
                {
                    "video": {
                        "format": "mp4",
                        "source": {
                            "s3Location": {
                                "uri": "s3://{{your-bucket/your-path/your-video.mp4}}",
                                "bucketOwner": "{{your-aws-account-id}}"
                            }
                        }
                    }
                }
            ]
        },
        {
            "role": "assistant",
            "content": [{
                "text": "There are at least eight visible items that are white"
            }]
        }
    ]
}
```

## 数据集限制
<a name="custom-fine-tune-constraints"></a>

Amazon Nova 对理解模型的模型自定义应用以下限制。


| 模型 | 最小样本数 | 最大样本数 | 上下文长度 | 
| --- |--- |--- |--- |
| Amazon Nova Micro | 8 | 20k | 32k | 
| Amazon Nova Lite | 8 | 20k | 32k | 
| Amazon Nova Pro | 8 | 20k | 32k | 


**图像和视频限制**  

|  |  | 
| --- |--- |
| 最大图像数 | 10 张/样本 | 
| 图像文件最大大小 | 10 MB | 
| 最大视频数 | 1 个/样本 | 
| 视频最大长度/时长 | 90 秒 | 
| 视频文件最大大小 | 50 MB | 

**支持的媒体格式**
+ 图像 – `png`、`jpeg`、`gif`、`webp`
+ 视频 – `mov`、`mkv`、`mp4`、`webm`