ChatGPT Fine-Tuning

OpenAI 官宣 GPT-3.5 Turbo fine-tuning and API updates 已有一段时间了,今天简单实操一下训练流程,按官网步骤按部就班即可顺利完成训练,共四步,训练过程大概10来分钟,当然训练时间长短取决于你训练数据的多少。

第一步,准备微调数据,将数据保存为一个以 .json 结尾的文本文件( 如 turbo35.json ),每一行都必须是一段完整的json格式,文件至少需要提供10行训练数据。
{"messages": [{ "role": "system", "content": "你是一个故事大王,能讲很多小故事" }, { "role": "user", "content": "给我讲一个故事" }, { "role": "assistant", "content": "一天,一个学生去学校上课" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小狗的故事" }, { "role": "assistant", "content": "一天,一只小狗溜进了动物园" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小猫的故事" }, { "role": "assistant", "content": "一天,一只小猫溜进了厨房" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小猪的故事" }, { "role": "assistant", "content": "一天,一只小猪睡了一整天" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小鸡的故事" }, { "role": "assistant", "content": "一天,一只小鸡飞了起来" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小鸭的故事" }, { "role": "assistant", "content": "一天,一只小鸭有过了一条河" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小鸟的故事" }, { "role": "assistant", "content": "一天,一只小鸟站在一颗大树上" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小虫的故事" }, { "role": "assistant", "content": "一天,一只小虫吃了一大片树叶" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小女孩的故事" }, { "role": "assistant", "content": "一天,一个小女孩跳了一支舞" }]}
{"messages": [{ "role": "user", "content": "给我讲一个关于小男孩的故事" }, { "role": "assistant", "content": "一天,一小男孩踢了一场球" }]}
第二步,调用 v1/files 接口上传第一步准备好的微调数据文件(@turbo35.json可以是相对路径,也可以使用绝对路径)
curl https://www.openai-api.top/v1/files -H "Authorization: Bearer 这里填入你自己的ChatGPT API Key" -F "purpose=fine-tune" -F "[email protected]"

调用接口后将收到类似如下格式的数据返回:

{
  "object": "file",
  "id": "file-mLHXCwvwCBGHFCtOiGkXCgwz",    # 上传的文件,记住此id,下一步会用到
  "purpose": "fine-tune",
  "filename": "turbo35.json",
  "bytes": 1826,
  "created_at": 1694748687,
  "status": "uploaded",
  "status_details": null
}
第三步,调用 v1/fine_tuning/jobs 接口开始训练
curl https://www.openai-api.top/v1/fine_tuning/jobs -H "Content-Type: application/json" -H "Authorization: Bearer 这里填入你自己的ChatGPT API Key" -d '{"training_file": "file-mLHXCwvwCBGHFCtOiGkXCgwz","model": "gpt-3.5-turbo-0613"}'

调用训练接口后会立即(训练任务是异步的)返回如下格式的数据:

{
"object":"fine_tuning.job",
"id":"ftjob-W2318ho3MVnSK0coD9sNfsbaV",  # 训练任务id,记住它,可用来查询训练进度
"model":"gpt-3.5-turbo-0613",
"created_at":1694750150,
"finished_at":null,
"fine_tuned_model":null,
"organization_id":"org-vK29681flpHvKxxxxw3YABDC",
"result_files":[],
"status":"created", # 训练状态/进度
"validation_file":null,
"training_file":"file-mLGXCwvwFZGHFCtOFGkXCgwz",
"hyperparameters":{
    "n_epochs":10
},
"trained_tokens":null,
"error":null
}

如设置了邮件通知的话,训练完成后会收到邮件通知,如等不急的话可随时调用 v1/fine_tuning/jobs/*** 接口查看训练进度,其中ftjob-W2318ho3MVnSK0coD9sNfsbaV为训练任务id

curl https://www.openai-api.top/v1/fine_tuning/jobs/ftjob-W2318ho3MVnSK0coD9sNfsbaV -H "Authorization: Bearer 这里填入你自己的ChatGPT API Key"

当 status=succeeded 时表示训练完成了,如下:

{
"object":"fine_tuning.job",
"id":"ftjob-W2318ho3MVnKS0coD9sNfsbaV",
"model":"gpt-3.5-turbo-0613",
"created_at":1694750150,
"finished_at":1694750457,
"fine_tuned_model":"ft:gpt-3.5-turbo-0613:personal::9wqakg3t",  # 训练完成的模型名称
"organization_id":"org-vK29681flpHvKxxxxw3YABDC",
"result_files":["file-M1hVO0nI3FWo4Ju5lQlykV5y"],
"status":"succeeded",   # 训练完成,可能的值有 created、running、succeeded
"validation_file":null,
"training_file":"file-mLGXCwvwZZGHFCtOFGkXCgwz",
"hyperparameters":{
    "n_epochs":10
},
"trained_tokens":4730,
"error":null
}
第四步,调用 v1/chat/completions 接口验证训练后的模型
curl https://www.openai-api.top/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer 这里填入你自己的ChatGPT API Key" -d '{"model":"ft:gpt-3.5-turbo-0613:personal::9wqakg3t","messages":[{"role":"user","content":"给我讲一个关于蓝鲸的故事"}]}'

如一切顺利的话模型现在已经可以按你训练的数据返回了:

{
  "id": "chatcmpl-7yw5nn23EMdTux11SZHKGY0k2cRMx",
  "object": "chat.completion",
  "created": 1694757343,
  "model": "ft:gpt-3.5-turbo-0613:personal::7yuIkb4e",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "一天,一只蓝鲸跃出了水面"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 18,
    "completion_tokens": 2,
    "total_tokens": 20
  }
}

参考OpenAI官网训练指导:https://openai.com/blog/gpt-3-5-turbo-fine-tuning-and-api-updates