

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

# 使用 OpenAI APIs 建立和管理開放權重模型的微調任務
<a name="fine-tuning-openai-job-create"></a>

OpenAI 相容的微調任務 APIs 可讓您建立、監控和管理微調任務。此頁面會反白使用這些 APIs 進行強化微調。如需完整的 API 詳細資訊，請參閱[OpenAI微調文件](https://platform.openai.com/docs/api-reference/fine-tuning)。

## 建立微調任務
<a name="fine-tuning-openai-create-job"></a>

建立微調任務，以開始從指定資料集建立新模型的程序。如需完整的 API 詳細資訊，請參閱[OpenAI建立微調任務文件](https://developers.openai.com/api/reference/resources/fine_tuning/subresources/jobs/methods/create)。

### 範例
<a name="fine-tuning-openai-create-job-examples"></a>

若要使用 RFT 方法建立微調任務，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

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

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

# Create fine-tuning job with RFT method
job_response = client.fine_tuning.jobs.create(
    model=MODEL_ID,
    training_file=training_file_id,
    # Suffix field is not supported so commenting for now.
    # suffix="rft-example",  # Optional: suffix for fine-tuned model name
    extra_body={
        "method": {
            "type": "reinforcement", 
            "reinforcement": {
                "grader": {
                    "type": "lambda",
                    "lambda": {
                        "function": "arn:aws:lambda:us-west-2:123456789012:function:my-reward-function"  # Replace with your Lambda ARN
                    }
                },
                "hyperparameters": {
                    "n_epochs": 1,  # Number of training epochs
                    "batch_size": 4,  # Batch size
                    "learning_rate_multiplier": 1.0  # Learning rate multiplier
                }
            }
        }
    }
)

# Store job ID for next steps
job_id = job_response.id
print({job_id})
```

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

向 提出 POST 請求`/v1/fine_tuning/jobs`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/fine_tuning/jobs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "training_file": "file-abc123",
    "model": "gpt-4o-mini",
    "method": {
      "type": "reinforcement",
      "reinforcement": {
        "grader": {
          "type": "lambda",
          "lambda": {
            "function": "arn:aws:lambda:us-west-2:123456789012:function:my-grader"
          }
        },
        "hyperparameters": {
          "n_epochs": 1,
          "batch_size": 4,
          "learning_rate_multiplier": 1.0
        }
      }
    }
  }'
```

------

## 列出微調事件
<a name="fine-tuning-openai-list-events"></a>

列出微調任務的事件。微調事件提供任務進度的詳細資訊，包括訓練指標、檢查點建立和錯誤訊息。如需完整的 API 詳細資訊，請參閱[OpenAI列出微調事件文件](https://developers.openai.com/api/reference/resources/fine_tuning/subresources/jobs/methods/list_events)。

### 範例
<a name="fine-tuning-openai-list-events-examples"></a>

若要列出微調事件，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

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

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

# List fine-tuning events
events = client.fine_tuning.jobs.list_events(
    fine_tuning_job_id="ftjob-abc123",
    limit=50
)

for event in events.data:
    print(f"[{event.created_at}] {event.level}: {event.message}")
    if event.data:
        print(f"  Metrics: {event.data}")
```

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

向 提出 GET 請求`/v1/fine_tuning/jobs/{fine_tuning_job_id}/events`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/fine_tuning/jobs/ftjob-abc123/events?limit=50
```

------

事件包括以下資訊：
+ 訓練已開始和已完成的訊息
+ 檢查點建立通知
+ 每個步驟的訓練指標 （遺失、準確性）
+ 任務失敗時的錯誤訊息

若要分頁所有事件，請選擇您偏好方法的標籤，然後遵循下列步驟：

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

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

# Paginate through all events
all_events = []
after = None

while True:
    events = client.fine_tuning.jobs.list_events(
        fine_tuning_job_id="ftjob-abc123",
        limit=100,
        after=after
    )
    
    all_events.extend(events.data)
    
    if not events.has_more:
        break
    
    after = events.data[-1].id
```

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

使用 `after` 參數提出多個 GET 請求：

```
# First request
curl https://bedrock-mantle.us-west-2.api.aws/v1/fine_tuning/jobs/ftjob-abc123/events?limit=100

# Subsequent requests with 'after' parameter
curl "https://bedrock-mantle.us-west-2.api.aws/v1/fine_tuning/jobs/ftjob-abc123/events?limit=100&after=ft-event-abc123"
```

------

## 擷取微調任務
<a name="fine-tuning-openai-retrieve-job"></a>

取得微調任務的詳細資訊。如需完整的 API 詳細資訊，請參閱[OpenAI擷取微調任務文件](https://developers.openai.com/api/reference/resources/fine_tuning/subresources/jobs/methods/retrieve)。

### 範例
<a name="fine-tuning-openai-retrieve-job-examples"></a>

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

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

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

# Retrieve specific job details
job_details = client.fine_tuning.jobs.retrieve(job_id)

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

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

向 提出 GET 請求`/v1/fine_tuning/jobs/{fine_tuning_job_id}`：

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

------

## 列出微調任務
<a name="fine-tuning-openai-list-jobs"></a>

列出您組織具有分頁支援的微調任務。如需完整的 API 詳細資訊，請參閱[OpenAI列出微調任務文件](https://developers.openai.com/api/reference/resources/fine_tuning/subresources/jobs/methods/list)。

### 範例
<a name="fine-tuning-openai-list-jobs-examples"></a>

若要列出具有限制和分頁的微調任務，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

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

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

# List fine-tuning jobs with limit and pagination
response = client.fine_tuning.jobs.list(
    limit=20  # Maximum number of jobs to return
)

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

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

向 提出 GET 請求`/v1/fine_tuning/jobs`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/fine_tuning/jobs?limit=20 \
  -H "Authorization: Bearer $OPENAI_API_KEY"
```

------

## 取消微調任務
<a name="fine-tuning-openai-cancel-job"></a>

取消進行中的微調任務。一旦取消，任務就無法繼續。如需完整的 API 詳細資訊，請參閱[OpenAI取消微調任務文件](https://developers.openai.com/api/reference/resources/fine_tuning/subresources/jobs/methods/cancel)。

### 範例
<a name="fine-tuning-openai-cancel-job-examples"></a>

若要取消微調任務，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

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

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

# Cancel fine-tuning job
cancel_response = client.fine_tuning.jobs.cancel("ftjob-abc123")

print(f"Job ID: {cancel_response.id}")
print(f"Status: {cancel_response.status}")  # Should be "cancelled"
```

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

向 提出 POST 請求`/v1/fine_tuning/jobs/{fine_tuning_job_id}/cancel`：

```
curl -X POST https://bedrock-mantle.us-west-2.api.aws/v1/fine_tuning/jobs/ftjob-abc123/cancel \
  -H "Authorization: Bearer $OPENAI_API_KEY"
```

------

## 列出微調檢查點
<a name="fine-tuning-openai-list-checkpoints"></a>

列出微調任務的檢查點。檢查點是在微調期間建立的中繼模型快照，可用於推論，以評估不同訓練階段的效能。如需詳細資訊，請參閱[OpenAI列出微調檢查點文件](https://developers.openai.com/api/reference/resources/fine_tuning/subresources/jobs/subresources/checkpoints/methods/list)。

### 範例
<a name="fine-tuning-openai-list-checkpoints-examples"></a>

若要列出微調任務的檢查點，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

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

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

# List checkpoints for a fine-tuning job
checkpoints = client.fine_tuning.jobs.checkpoints.list(
    fine_tuning_job_id="ftjob-abc123",
    limit=10
)

for checkpoint in checkpoints.data:
    print(f"Checkpoint ID: {checkpoint.id}")
    print(f"Step: {checkpoint.step_number}")
    print(f"Model: {checkpoint.fine_tuned_model_checkpoint}")
    print(f"Metrics: {checkpoint.metrics}")
    print("---")
```

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

向 提出 GET 請求`/v1/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/fine_tuning/jobs/ftjob-abc123/checkpoints?limit=10
```

------

每個檢查點包括：
+ **檢查點 ID – **檢查點的唯一識別符
+ **步驟編號** – 建立檢查點的訓練步驟
+ **模型檢查點** – 可用於推論的模型識別符
+ **指標** – 此檢查點的驗證遺失和準確性

若要使用檢查點模型進行推論，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

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

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

# Test inference with a checkpoint
response = client.chat.completions.create(
    model=checkpoint.fine_tuned_model_checkpoint,
    messages=[{"role": "user", "content": "What is AI?"}],
    max_tokens=100
)

print(response.choices[0].message.content)
```

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

向 提出 POST 請求`/v1/chat/completions`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "ft:gpt-4o-mini:openai:custom:7p4lURel:ckpt-step-1000",
    "messages": [{"role": "user", "content": "What is AI?"}],
    "max_tokens": 100
  }'
```

------

## 使用微調的模型執行推論
<a name="fine-tuning-openai-inference"></a>

微調任務完成後，您可以透過回應 API 或聊天完成 API，使用微調模型進行推論。如需完整的 API 詳細資訊，請參閱 [使用 OpenAI APIs產生回應](bedrock-mantle.md)。

### 回應 API
<a name="fine-tuning-openai-responses-api"></a>

使用 Responses API 搭配您的微調模型進行單迴轉文字產生：

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

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

# Get the fine-tuned model ID
job_details = client.fine_tuning.jobs.retrieve("ftjob-abc123")

if job_details.status == 'succeeded' and job_details.fine_tuned_model:
    fine_tuned_model = job_details.fine_tuned_model
    print(f"Using fine-tuned model: {fine_tuned_model}")
    
    # Run inference with Responses API
    response = client.completions.create(
        model=fine_tuned_model,
        prompt="What is the capital of France?",
        max_tokens=100,
        temperature=0.7
    )
    
    print(f"Response: {response.choices[0].text}")
else:
    print(f"Job status: {job_details.status}")
    print("Job must be in 'succeeded' status to run inference")
```

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

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

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "ft:gpt-4o-mini:openai:custom-model:7p4lURel",
    "prompt": "What is the capital of France?",
    "max_tokens": 100,
    "temperature": 0.7
  }'
```

------

### 聊天完成 API
<a name="fine-tuning-openai-inference-examples"></a>

使用聊天完成 API 與您的微調模型進行對話互動：

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

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

# Get the fine-tuned model ID
job_details = client.fine_tuning.jobs.retrieve("ftjob-abc123")

if job_details.status == 'succeeded' and job_details.fine_tuned_model:
    fine_tuned_model = job_details.fine_tuned_model
    print(f"Using fine-tuned model: {fine_tuned_model}")
    
    # Run inference
    inference_response = client.chat.completions.create(
        model=fine_tuned_model,
        messages=[
            {"role": "user", "content": "What is the capital of France?"}
        ],
        max_tokens=100
    )
    
    print(f"Response: {inference_response.choices[0].message.content}")
else:
    print(f"Job status: {job_details.status}")
    print("Job must be in 'succeeded' status to run inference")
```

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

向 提出 POST 請求`/v1/chat/completions`：

```
curl https://bedrock-mantle.us-west-2.api.aws/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "ft:gpt-4o-mini:openai:custom-model:7p4lURel",
    "messages": [
      {"role": "user", "content": "What is the capital of France?"}
    ],
    "max_tokens": 100
  }'
```

------