

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

# 準備資料以進行多模態微調
<a name="fine-tune-prepare-data-understanding"></a>

**重要**  
開始準備資料集之前，請確定監督式微調 (SFT) 是適合您使用案例的正確方法。SFT 會教導模型新*行為*、回應格式和推理模式。它不會教導模型新的事實知識。如果您的主要目標是介紹特定領域的事實、術語或尚未看到的模型知識，請考慮擷取擴增產生 (RAG)，以在推論時提供該內容。如需在 SFT、強化微調 (RFT) 和 RAG 之間進行選擇的指引，請參閱 [SageMaker 訓練任務上的 Amazon Nova 自訂](nova-model-training-job.md)。

以下是準備資料以微調理解模型的指導方針和要求：

1. 用於微調的資料大小下限取決於任務 (即複雜或簡單)，但我們建議您至少為希望模型學習的每個任務提供 100 個範例。

1. 我們建議您在訓練和推論期間以零樣本設定使用最佳化提示詞，以獲得最佳結果。

1. 訓練和驗證資料集必須是 JSONL 檔案，其中每一行都是對應於記錄的 JSON 物件。這些檔案名稱只能包含英數字元、底線、連字號、斜線和句點。

1. 影像和影片限制條件

   1. 資料集不能包含不同的媒體模態。也就是說，資料集要麽是帶影像的文字，要麽是帶影片的文字。

   1. 一個範例 (訊息中的單一記錄) 可以有多個影像

   1. 一個範例 (訊息中的單一記錄) 只能有 1 個影片

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

1. (*選用*) `system` 回合可以是客戶提供的自訂系統提示詞。

1. 支援的角色為 `user` 和 `assistant`。

1. 第一個回合 `messages` 應一律以 `"role": "user"` 開頭。最後一圈是機器人的回應，以 表示`"role": "assistant"`。

1. Amazon Bedrock 必須可存取 `image.source.s3Location.uri` 和 `video.source.s3Location.uri`。

1.  您的 Amazon Bedrock 服務角色必須能夠存取 Amazon S3 中的影像檔案。如需授與存取權的詳細資訊，請參閱[建立用於模型自訂的服務角色](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)。

## 設計有效的訓練範例
<a name="designing-effective-training-examples"></a>

您的訓練資料應該示範您希望模型展現*的行為*。SFT 會教導模型*如何*回應，而不是知道*什麼*。如果您發現自己建立訓練範例主要是為了注入事實知識 （例如，「錯誤碼 E-45 是什麼意思？」 答案為「E-45 表示感應器逾時」)，請考慮 RAG 或提示工程是否可以達成相同的結果，而無需微調。

將來源資料轉換為對話格式時，請遵循下列原則：

1. **從實際使用者查詢開始。**寫入使用者轉彎，反映最終使用者在生產環境中實際提示模型的方式。避免不會反映實際使用模式的人工或過度簡化問題。

1. **撰寫黃金標準助理回應。**每個助理輪換應該是您希望模型產生的理想回應 - 在所有範例中正確、格式正確且一致的色調和結構。

1. **以策略方式使用系統提示。**在系統轉彎中放置持久性指示，例如輸出格式、角色和網域內容。在訓練和推論期間使用相同的系統提示，以獲得最佳結果。

1. **讓範例專注於每個範例的一個技能。**如果您需要模型進行分類、回答問題和總結，請為每個技能建立單獨的範例集，而不是將多個技能結合在單一範例中。

1. **優先考慮品質而非數量。**數百個高品質、一致的範例通常優於數千個雜訊或矛盾的範例。在訓練之前刪除並檢閱您的資料。

### 常見的資料轉換模式
<a name="common-data-transformation-patterns"></a>

下列模式說明如何將常見的企業資料格式轉換為有效的 SFT 訓練範例。

**模式 1：要分類或查詢的結構化記錄**  
當您的來源資料是具有 `issue`、 `cause`和 等欄位的記錄資料表或資料庫`resolution`時，請將每個記錄作為使用者問題框架，其中包含相關的輸入欄位和助理回應作為目標輸出欄位。

```
// train.jsonl
{
  "schemaVersion": "bedrock-conversation-2024",
  "system": [
    {
      "text": "You are a technical support assistant. Given an issue description, identify the most likely root cause and recommended resolution."
    }
  ],
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "A user reports: 'Application crashes on startup after the latest update.' What is the likely cause and resolution?"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "text": "Cause: Incompatible configuration file from the previous version.\nResolution: Delete the local configuration cache and restart the application to regenerate default settings."
        }
      ]
    }
  ]
}
```

**模式 2：結構化問答的階層或分類資料**  
當您的來源資料是程式碼階層或分類，例如產品類別樹狀目錄或分類系統時， 會產生問題，要求模型導覽階層。在系統提示或使用者轉彎中包含分類內容。

```
// train.jsonl
{
  "schemaVersion": "bedrock-conversation-2024",
  "system": [
    {
      "text": "You are a product catalog assistant. Answer questions about product categories and classifications accurately."
    }
  ],
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "What top-level category does product code 'EL-4520' belong to, and what subcategories does it include?"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "text": "Product code EL-4520 belongs to the Electronics category. It includes the subcategories: Audio Equipment, Portable Speakers, and Wireless Accessories."
        }
      ]
    }
  ]
}
```

**模式 3：教學對話方塊的程序文件**  
當您的來源資料包含標準操作程序、執行手冊或step-by-step指南時，請將程序轉換為任務導向問題。使用者詢問如何執行任務，而助理會以一致的格式提供步驟。

```
// train.jsonl
{
  "schemaVersion": "bedrock-conversation-2024",
  "system": [
    {
      "text": "You are an IT operations assistant. Provide step-by-step instructions based on approved runbooks and standard operating procedures."
    }
  ],
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "How do I reset a user's multi-factor authentication when they have lost access to their device?"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "text": "Follow these steps to reset a user's multi-factor authentication:\n1. Verify the user's identity using the approved verification procedure.\n2. Disable the existing MFA configuration in the identity management console.\n3. Generate a new temporary access code and send it to the user's verified email.\n4. Instruct the user to log in with the temporary code and re-enroll their new device.\n5. Confirm the new MFA device is working by requesting a test authentication.\n6. Document the reset in the support ticket."
        }
      ]
    }
  ]
}
```

**注意**  
在模式 1 和 2 中，如果事實內容 （例如程式碼、類別或解析度） 尚未存在於模型的預先訓練資料中，請考慮使用 RAG 在推論時間提供此資訊，而不是僅依賴 SFT 來記住它。SFT 最有效地教導模型回應格式和推理模式，而 RAG 會處理事實基礎。

**Topics**
+ [設計有效的訓練範例](#designing-effective-training-examples)
+ [資料集格式範例](#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`