

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

# 準備開放權重模型的資料
<a name="rft-prepare-data-open-weight"></a>

當您使用 OpenAI 相容 APIs 進行強化微調來微調開放加權模型時，請使用目的為 的 JSONL 格式提示來提供訓練資料`fine-tune`。

## 訓練資料格式和要求
<a name="rft-data-format-open-weight"></a>

訓練資料必須遵循 OpenAI 聊天完成格式與 100-20K 範例。每個訓練範例都包含：
+ `messages`：在此欄位中，包含使用者、系統或助理角色，其中包含提供給模型的輸入提示。
+ `reference_answer`：在此欄位中，它應該包含預期輸出或評估條件，您的獎勵函數會用來對模型的回應進行評分。它不限於結構化輸出，它可以包含任何格式，協助您的獎勵函數評估品質。
+ 【選用】 您可以新增分級器 Lambda 用於分級的欄位。

**使用要求：**
+ JSONL 格式，其中包含 OpenAI 聊天完成格式的提示 （每行一個提示）
+ 目的必須設定為 `fine-tune`
+ 訓練資料集至少 100 筆記錄
+ Amazon Bedrock 會自動驗證訓練資料集格式

------
#### [ Example: General question-answering ]

```
{
            "messages": [
                {
                    "role": "system", 
                    "content": "You are a helpful assistant"
                },
                {
                    role": "user", 
                    "content": "What is machine learning?"}
            ],
            "reference_answer": "Machine learning is a subset of artificial intelligence that enables computers to learn and make decisions from data without being explicitly programmed."
            }
```

------
#### [ Example: Math problem ]

```
{
  "id": "sample-001",
  "messages": [
    {
      "role": "system",
      "content": "You are a math tutor"
    },
    {
      "role": "user",
      "content": "Solve: 2x + 5 = 13"
    }
  ],
  "reference_answer": {
    "solution": "x = 4",
    "steps": ["2x = 13 - 5", "2x = 8", "x = 4"]
  }
}
```

------

## 檔案 API
<a name="fine-tuning-openai-files-api"></a>

您可以使用 OpenAI 相容檔案 API 上傳訓練資料，以進行微調任務。檔案會安全地存放在 Amazon Bedrock 中，並在建立微調任務時使用。如需完整的 API 詳細資訊，請參閱[OpenAI檔案文件](https://platform.openai.com/docs/api-reference/files)。

### 上傳訓練檔案
<a name="fine-tuning-openai-upload-file"></a>

若要上傳訓練檔案，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

------
#### [ OpenAI SDK (Python) ]

```
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
from openai import OpenAI
client = OpenAI()

# Upload training file
with open(TRAINING_FILE_PATH, 'rb') as f:
    file_response = client.files.create(
        file=f,
        purpose='fine-tune'
    )

# Store file ID for next steps
training_file_id = file_response.id
print(f"✅ Training file uploaded successfully: {training_file_id}")
```

------
#### [ HTTP request ]

向 提出 POST 請求`/v1/files`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/files \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F purpose="fine-tune" \
  -F file="@training_data.jsonl"
```

------

### 擷取檔案詳細資訊
<a name="fine-tuning-openai-retrieve-file"></a>

若要擷取特定檔案的詳細資訊，請選擇您偏好方法的標籤，然後遵循下列步驟：

------
#### [ OpenAI SDK (Python) ]

```
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
from openai import OpenAI
client = OpenAI()

# Retrieve file details
file_details = client.files.retrieve(training_file_id)

# Print raw response
print(json.dumps(file_details.model_dump(), indent=2))
```

------
#### [ HTTP request ]

向 提出 GET 請求`/v1/files/{file_id}`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/files/file-abc123 \
  -H "Authorization: Bearer $OPENAI_API_KEY"
```

------

### 列出檔案
<a name="fine-tuning-openai-list-files"></a>

若要列出上傳的檔案，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

------
#### [ OpenAI SDK (Python) ]

```
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
from openai import OpenAI
client = OpenAI()

# List files
files_response = client.files.list(purpose='fine-tune')

# Print raw response
print(json.dumps(files_response.model_dump(), indent=2))
```

------
#### [ HTTP request ]

向 提出 GET 請求`/v1/files`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/files?purpose=fine-tune \
  -H "Authorization: Bearer $OPENAI_API_KEY"
```

------

### 刪除檔案
<a name="fine-tuning-openai-delete-file"></a>

若要刪除檔案，請選擇您偏好方法的標籤，然後遵循下列步驟：

------
#### [ OpenAI SDK (Python) ]

```
# Requires OPENAI_API_KEY and OPENAI_BASE_URL environment variables
from openai import OpenAI
client = OpenAI()

# Delete file
delete_response = client.files.delete(training_file_id)
```

------
#### [ HTTP request ]

向 提出 DELETE 請求`/v1/files/{file_id}`：

```
curl -X DELETE https://bedrock-mantle.us-west-2.api.aws/v1/files/file-abc123 \
  -H "Authorization: Bearer $OPENAI_API_KEY"
```

------

## 有效訓練資料的特性
<a name="rft-data-characteristics-open-weight"></a>

有效的 RFT 訓練資料需要三個關鍵特性：
+ **清晰度和一致性** – 使用具有一致格式的明確、不明確提示。避免矛盾的標籤、模棱兩可的指示，或誤導訓練的衝突參考答案。
+ **多樣性** - 包括各種輸入格式、邊緣案例和難度等級，反映不同使用者類型和案例的生產使用模式。
+ **高效率獎勵函數** – 設計快速執行 （秒，而非分鐘） AWS Lambda、平行化並傳回一致分數的函數，以進行符合成本效益的訓練。

## 其他屬性
<a name="rft-additional-properties-open-weight"></a>

RFT 資料格式支援核心結構描述需求之外的自訂欄位 (`messages` 和 `reference_answer`)。此彈性可讓您新增獎勵函數進行適當評估所需的任何其他資料。

**注意**  
您不需要在配方中設定此項目。資料格式本質上支援其他欄位。只要將它們包含在訓練資料 JSON 中，它們就會在 `metadata` 欄位中傳遞給您的獎勵函數。

**常見的其他屬性**
+ `task_id` – 用於追蹤的唯一識別符
+ `difficulty_level` – 問題複雜性指標
+ `domain` – 主題區域或類別
+ `expected_reasoning_steps` – 解決方案中的步驟數量

這些額外的欄位會在評估期間傳遞給您的獎勵函數，以根據您的特定使用案例量身打造複雜的評分邏輯。

**具有其他屬性的範例**

------
#### [ Chemistry problem ]

```
{
  "id": "chem-001",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful chemistry assistant"
    },
    {
      "role": "user",
      "content": "Predict hydrogen bond donors and acceptors for this SMILES: CCN(CC)CCC(=O)c1sc(N)nc1C"
    }
  ],
  "reference_answer": {
    "donor_bond_counts": 2,
    "acceptor_bond_counts": 4
  }
}
```

`reference_answer` 欄位包含預期輸出或評估條件，您的獎勵函數會用來對模型的回應進行評分。不限於結構化輸出，它可以包含任何格式，協助您的獎勵函數評估品質。

------
#### [ Math problem with metadata ]

```
{
  "messages": [
    {
      "role": "system",
      "content": "You are a math tutor"
    },
    {
      "role": "user",
      "content": "Solve: 2x + 5 = 13"
    }
  ],
  "reference_answer": {
    "solution": "x = 4",
    "steps": ["2x = 13 - 5", "2x = 8", "x = 4"]
  },
  "task_id": "algebra_001",
  "difficulty_level": "easy",
  "domain": "algebra",
  "expected_reasoning_steps": 3
}
```

------