ChatGPT Fine-Tuning

OpenAI officially announced GPT-3.5 Turbo fine-tuning and API updates some time ago. Today, I tried out the training process briefly. By following the steps on the official website, you can successfully complete the training in four steps. The training process takes about 10 minutes, though the duration depends on the amount of training data you have.

The first step is to prepare the fine-tuning data by saving it as a text file with a .json extension (such as turbo35.json). Each line must be a complete JSON object, and the file needs to contain at least 10 lines of training data.
{"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": "一天,一小男孩踢了一场球" }]}
The second step is to call the v1/files endpoint to upload the fine-tuning data file prepared in the first step (@turbo35.json, which can be a relative or absolute path).
curl https://www.openai-api.top/v1/files -H "Authorization: Bearer 这里填入你自己的ChatGPT API Key" -F "purpose=fine-tune" -F "[email protected]"

After calling the endpoint, you will receive a response with data in a format similar to the following:

{
  "object": "file",
  "id": "file-mLHXCwvwCBGHFCtOiGkXCgwz",    # 上传的文件,记住此id,下一步会用到
  "purpose": "fine-tune",
  "filename": "turbo35.json",
  "bytes": 1826,
  "created_at": 1694748687,
  "status": "uploaded",
  "status_details": null
}
The third step is to call the v1/fine_tuning/jobs endpoint to start the training.
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"}'

After calling the training endpoint, you will immediately receive data in the following format (since the training task is asynchronous):

{
"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
}

If email notifications are enabled, you will receive an email once the training is complete. If you can’t wait, you can call the v1/fine_tuning/jobs/*** endpoint at any time to check the training progress. The ftjob-W2318ho3MVnSK0coD9sNfsbaV is the training job ID.

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

When status=succeeded, it indicates that the training is complete, as shown below:

{
"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
}
The fourth step is to call the v1/chat/completions endpoint to validate the fine-tuned model.
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":"给我讲一个关于蓝鲸的故事"}]}'

If everything went smoothly, the model should now be able to respond according to the data you trained it with:

{
  "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
  }
}

Refer to the OpenAI training guidance.:https://openai.com/blog/gpt-3-5-turbo-fine-tuning-and-api-updates