微调

管理微调作业,以根据您的特定训练数据定制模型。 相关指南: 微调模型source

创建微调作业

POST https://api.openai.com/v1/fine_tuning/jobs

创建一个微调作业,该作业开始从给定数据集创建新模型的过程。source

响应包括已排队作业的详细信息,包括作业状态和完成后微调模型的名称。source

了解有关微调的更多信息source

请求正文

要微调的模型的名称。您可以选择其中一个受支持的模型source

包含训练数据的已上传文件的 ID。source

有关如何上传文件的信息,请参阅上传文件source

您的数据集必须格式化为 JSONL 文件。此外,您必须上传文件fine-tune.source

文件的内容应有所不同,具体取决于模型是使用 chat 还是 completions 格式。source

有关更多详细信息,请参阅微调指南source

用于微调作业的超参数。source

一个最多 64 个字符的字符串,将添加到您的微调模型名称中。source

例如,suffix的 “custom-model-name” 将生成一个模型名称,例如ft:gpt-4o-mini:openai:custom-model-name:7p4lURel.source

包含验证数据的已上传文件的 ID。source

如果您提供此文件,则数据将用于生成验证 指标。这些指标可以在 微调结果文件。 训练文件和验证文件中不应存在相同的数据。source

您的数据集必须格式化为 JSONL 文件。您必须上传您的文件fine-tune.source

有关更多详细信息,请参阅微调指南source

要为微调作业启用的集成列表。source

种子控制作业的可重复性。传入相同的 seed 和 job 参数应该会产生相同的结果,但在极少数情况下可能会有所不同。 如果未指定种子,将为您生成一个种子。source

示例请求
1
2
3
4
5
6
7
curl https://api.openai.com/v1/fine_tuning/jobs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "training_file": "file-BK7bzQj3FfZFXr7DbL6xJwfo",
    "model": "gpt-4o-mini"
  }'
响应
1
2
3
4
5
6
7
8
9
10
11
12
{
  "object": "fine_tuning.job",
  "id": "ftjob-abc123",
  "model": "gpt-4o-mini-2024-07-18",
  "created_at": 1721764800,
  "fine_tuned_model": null,
  "organization_id": "org-123",
  "result_files": [],
  "status": "queued",
  "validation_file": null,
  "training_file": "file-abc123",
}

列出微调作业

获取 https://api.openai.com/v1/fine_tuning/jobs

列出组织的微调作业source

查询参数

上一个分页请求中最后一个作业的标识符。source

要检索的微调作业数。source

返回

分页微调作业对象的列表。source

示例请求
1
2
curl https://api.openai.com/v1/fine_tuning/jobs?limit=2 \
  -H "Authorization: Bearer $OPENAI_API_KEY"
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "object": "list",
  "data": [
    {
      "object": "fine_tuning.job.event",
      "id": "ft-event-TjX0lMfOniCZX64t9PUQT5hn",
      "created_at": 1689813489,
      "level": "warn",
      "message": "Fine tuning process stopping due to job cancellation",
      "data": null,
      "type": "message"
    },
    { ... },
    { ... }
  ], "has_more": true
}

列出微调事件

获取 https://api.openai.com/v1/fine_tuning/jobs/{fine_tuning_job_id}/events

获取微调作业的状态更新。source

路径参数

要为其获取事件的微调作业的 ID。source

查询参数

上一个分页请求中的最后一个事件的标识符。source

要检索的事件数。source

返回

微调事件对象的列表。source

示例请求
1
2
curl https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/events \
  -H "Authorization: Bearer $OPENAI_API_KEY"
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "object": "list",
  "data": [
    {
      "object": "fine_tuning.job.event",
      "id": "ft-event-ddTJfwuMVpfLXseO0Am0Gqjm",
      "created_at": 1721764800,
      "level": "info",
      "message": "Fine tuning job successfully completed",
      "data": null,
      "type": "message"
    },
    {
      "object": "fine_tuning.job.event",
      "id": "ft-event-tyiGuB72evQncpH87xe505Sv",
      "created_at": 1721764800,
      "level": "info",
      "message": "New fine-tuned model created: ft:gpt-4o-mini:openai::7p4lURel",
      "data": null,
      "type": "message"
    }
  ],
  "has_more": true
}

列出微调检查点

获取 https://api.openai.com/v1/fine_tuning/jobs/{fine_tuning_job_id}/checkpoints

列出微调作业的检查点。source

路径参数

要为其获取检查点的微调作业的 ID。source

查询参数

上一个分页请求的最后一个检查点 ID 的标识符。source

要检索的检查点数。source

返回

用于微调作业的微调检查点对象列表。source

示例请求
1
2
curl https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/checkpoints \
  -H "Authorization: Bearer $OPENAI_API_KEY"
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
  "object": "list"
  "data": [
    {
      "object": "fine_tuning.job.checkpoint",
      "id": "ftckpt_zc4Q7MP6XxulcVzj4MZdwsAB",
      "created_at": 1721764867,
      "fine_tuned_model_checkpoint": "ft:gpt-4o-mini-2024-07-18:my-org:custom-suffix:96olL566:ckpt-step-2000",
      "metrics": {
        "full_valid_loss": 0.134,
        "full_valid_mean_token_accuracy": 0.874
      },
      "fine_tuning_job_id": "ftjob-abc123",
      "step_number": 2000,
    },
    {
      "object": "fine_tuning.job.checkpoint",
      "id": "ftckpt_enQCFmOTGj3syEpYVhBRLTSy",
      "created_at": 1721764800,
      "fine_tuned_model_checkpoint": "ft:gpt-4o-mini-2024-07-18:my-org:custom-suffix:7q8mpxmy:ckpt-step-1000",
      "metrics": {
        "full_valid_loss": 0.167,
        "full_valid_mean_token_accuracy": 0.781
      },
      "fine_tuning_job_id": "ftjob-abc123",
      "step_number": 1000,
    },
  ],
  "first_id": "ftckpt_zc4Q7MP6XxulcVzj4MZdwsAB",
  "last_id": "ftckpt_enQCFmOTGj3syEpYVhBRLTSy",
  "has_more": true
}

检索微调作业

获取 https://api.openai.com/v1/fine_tuning/jobs/{fine_tuning_job_id}

获取有关微调作业的信息。source

了解有关微调的更多信息source

路径参数

微调作业的 ID。source

返回

具有给定 ID 的微调对象。source

示例请求
1
2
curl https://api.openai.com/v1/fine_tuning/jobs/ft-AF1WoRqd3aJAHsqc9NY7iL8F \
  -H "Authorization: Bearer $OPENAI_API_KEY"
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "object": "fine_tuning.job",
  "id": "ftjob-abc123",
  "model": "davinci-002",
  "created_at": 1692661014,
  "finished_at": 1692661190,
  "fine_tuned_model": "ft:davinci-002:my-org:custom_suffix:7q8mpxmy",
  "organization_id": "org-123",
  "result_files": [
      "file-abc123"
  ],
  "status": "succeeded",
  "validation_file": null,
  "training_file": "file-abc123",
  "hyperparameters": {
      "n_epochs": 4,
      "batch_size": 1,
      "learning_rate_multiplier": 1.0
  },
  "trained_tokens": 5768,
  "integrations": [],
  "seed": 0,
  "estimated_finish": 0
}

取消微调

post https://api.openai.com/v1/fine_tuning/jobs/{fine_tuning_job_id}/cancel

立即取消微调作业。source

路径参数

要取消的微调作业的 ID。source

返回

已取消的微调对象。source

示例请求
1
2
curl -X POST https://api.openai.com/v1/fine_tuning/jobs/ftjob-abc123/cancel \
  -H "Authorization: Bearer $OPENAI_API_KEY"
响应
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "object": "fine_tuning.job",
  "id": "ftjob-abc123",
  "model": "gpt-4o-mini-2024-07-18",
  "created_at": 1721764800,
  "fine_tuned_model": null,
  "organization_id": "org-123",
  "result_files": [],
  "hyperparameters": {
    "n_epochs":  "auto"
  },
  "status": "cancelled",
  "validation_file": "file-abc123",
  "training_file": "file-abc123"
}

聊天模型的训练格式

聊天模型的微调输入文件的每行训练示例source

模型可以为其生成 JSON 输入的工具列表。source

是否在工具使用过程中启用并行函数调用source

模型可以为其生成 JSON 输入的函数列表。source

聊天模型的 OBJECT 训练格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
  "messages": [
    { "role": "user", "content": "What is the weather in San Francisco?" },
    {
      "role": "assistant",
      "tool_calls": [
        {
          "id": "call_id",
          "type": "function",
          "function": {
            "name": "get_current_weather",
            "arguments": "{\"location\": \"San Francisco, USA\", \"format\": \"celsius\"}"
          }
        }
      ]
    }
  ],
  "parallel_tool_calls": false,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_current_weather",
        "description": "Get the current weather",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
                "type": "string",
                "description": "The city and country, eg. San Francisco, USA"
            },
            "format": { "type": "string", "enum": ["celsius", "fahrenheit"] }
          },
          "required": ["location", "format"]
        }
      }
    }
  ]
}

完成模型的训练格式

补全模型的微调输入文件的每行训练示例source

此训练示例的输入提示。source

此训练示例的预期完成。source

完成模型的 OBJECT 训练格式
1
2
3
4
{
  "prompt": "What is the answer to 2+2",
  "completion": "4"
}

微调作业对象

fine_tuning.jobobject 表示通过 API 创建的微调作业。source

对象标识符,可在 API 终端节点中引用。source

创建微调作业时的 Unix 时间戳(以秒为单位)。source

对于具有failed,这将包含有关失败原因的更多信息。source

正在创建的微调模型的名称。如果微调作业仍在运行,则该值将为 null。source

微调作业完成时的 Unix 时间戳(以秒为单位)。如果微调作业仍在运行,则该值将为 null。source

用于微调作业的超参数。有关更多详细信息,请参阅微调指南source

正在微调的基本模型。source

对象类型,始终为 “fine_tuning.job”。source

拥有微调作业的组织。source

微调作业的已编译结果文件 ID。您可以使用 Files API 检索结果。source

微调作业的当前状态,可以是validating_files,queued,running,succeeded,failedcancelled.source

此微调作业处理的可计费 Token 总数。如果微调作业仍在运行,则该值将为 null。source

用于训练的文件 ID。您可以使用 Files API 检索训练数据。source

用于验证的文件 ID。您可以使用 Files API 检索验证结果。source

要为此微调作业启用的集成列表。source

用于微调作业的种子。source

微调作业估计完成时间的 Unix 时间戳(以秒为单位)。如果微调作业未运行,则该值将为 null。source

OBJECT 微调作业对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
  "object": "fine_tuning.job",
  "id": "ftjob-abc123",
  "model": "davinci-002",
  "created_at": 1692661014,
  "finished_at": 1692661190,
  "fine_tuned_model": "ft:davinci-002:my-org:custom_suffix:7q8mpxmy",
  "organization_id": "org-123",
  "result_files": [
      "file-abc123"
  ],
  "status": "succeeded",
  "validation_file": null,
  "training_file": "file-abc123",
  "hyperparameters": {
      "n_epochs": 4,
      "batch_size": 1,
      "learning_rate_multiplier": 1.0
  },
  "trained_tokens": 5768,
  "integrations": [],
  "seed": 0,
  "estimated_finish": 0
}

微调作业事件对象

微调作业事件对象source

OBJECT 微调作业事件对象
1
2
3
4
5
6
7
{
  "object": "fine_tuning.job.event",
  "id": "ftevent-abc123"
  "created_at": 1677610602,
  "level": "info",
  "message": "Created fine-tuning job"
}

微调作业检查点对象

fine_tuning.job.checkpointobject 表示可供使用的微调作业的模型检查点。source

检查点标识符,可在 API 终端节点中引用。source

创建检查点时的 Unix 时间戳(以秒为单位)。source

创建的微调检查点模型的名称。source

创建检查点的步骤编号。source

微调作业期间步骤编号处的指标。source

从中创建此检查点的微调作业的名称。source

对象类型,始终为 “fine_tuning.job.checkpoint”。source

OBJECT 微调作业检查点对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
  "object": "fine_tuning.job.checkpoint",
  "id": "ftckpt_qtZ5Gyk4BLq1SfLFWp3RtO3P",
  "created_at": 1712211699,
  "fine_tuned_model_checkpoint": "ft:gpt-4o-mini-2024-07-18:my-org:custom_suffix:9ABel2dg:ckpt-step-88",
  "fine_tuning_job_id": "ftjob-fpbNQ3H1GrMehXRf8cO97xTN",
  "metrics": {
    "step": 88,
    "train_loss": 0.478,
    "train_mean_token_accuracy": 0.924,
    "valid_loss": 10.112,
    "valid_mean_token_accuracy": 0.145,
    "full_valid_loss": 0.567,
    "full_valid_mean_token_accuracy": 0.944
  },
  "step_number": 88
}