微调
管理微调作业,以根据您的特定训练数据定制模型。 相关指南: 微调模型
创建微调作业
POST https://api.openai.com/v1/fine_tuning/jobs
请求正文
要微调的模型的名称。您可以选择其中一个受支持的模型。
包含训练数据的已上传文件的 ID。
有关如何上传文件的信息,请参阅上传文件。
您的数据集必须格式化为 JSONL 文件。此外,您必须上传文件fine-tune
.
文件的内容应有所不同,具体取决于模型是使用 chat 还是 completions 格式。
有关更多详细信息,请参阅微调指南。
一个最多 64 个字符的字符串,将添加到您的微调模型名称中。
例如,suffix
的 “custom-model-name” 将生成一个模型名称,例如ft:gpt-4o-mini:openai:custom-model-name:7p4lURel
.
包含验证数据的已上传文件的 ID。
如果您提供此文件,则数据将用于生成验证 指标。这些指标可以在 微调结果文件。 训练文件和验证文件中不应存在相同的数据。
您的数据集必须格式化为 JSONL 文件。您必须上传您的文件fine-tune
.
有关更多详细信息,请参阅微调指南。
返回
示例请求
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",
}
列出微调作业
示例请求
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
获取微调作业的状态更新。
返回
微调事件对象的列表。
示例请求
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
列出微调作业的检查点。
返回
用于微调作业的微调检查点对象列表。
示例请求
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
}
检索微调作业
示例请求
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
}
取消微调
示例请求
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"
}
聊天模型的训练格式
聊天模型的微调输入文件的每行训练示例
聊天模型的 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"]
}
}
}
]
}
完成模型的训练格式
微调作业对象
这fine_tuning.job
object 表示通过 API 创建的微调作业。
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
}
微调作业事件对象
微调作业检查点对象
这fine_tuning.job.checkpoint
object 表示可供使用的微调作业的模型检查点。
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
}