{
  "slug": "How-to-Fine-tune-ChatGPT-305606a02605",
  "title": "How to Fine-tune ChatGPT",
  "subtitle": "Train ChatGPT with your own data.",
  "excerpt": "Train ChatGPT with your own data.",
  "date": "2024-03-12",
  "tags": [
    "Design Rag System",
    "Machine Learning"
  ],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/fine-tune-chatgpt-gpt-305606a02605",
  "hero": "https://cdn-images-1.medium.com/max/800/1*ytRq2w4AZrMI_eocKt21fA.jpeg",
  "content": [
    {
      "type": "paragraph",
      "html": "Train ChatGPT with your own data."
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://platform.openai.com/docs/guides/fine-tuning\" target=\"_blank\" rel=\"noreferrer noopener\">https://platform.openai.com/docs/guides/fine-tuning</a>"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*ytRq2w4AZrMI_eocKt21fA.jpeg",
      "alt": "How to Fine-tune ChatGPT",
      "caption": "",
      "width": 640,
      "height": 832
    },
    {
      "type": "paragraph",
      "html": "OpenAI’s text generation models have been pre-trained on a vast amount of text. To use the models effectively, we include instructions and sometimes several examples in a prompt. Using demonstrations to show how to perform a task is often called “few-shot learning.”"
    },
    {
      "type": "paragraph",
      "html": "Fine-tuning improves on few-shot learning by training on many more examples than can fit in the prompt, letting you achieve better results on a wide number of tasks. Once a model has been fine-tuned, you won’t need to provide as many examples in the prompt. This saves costs and enables lower-latency requests."
    },
    {
      "type": "paragraph",
      "html": "Fine-tuning is currently available for the following models: <code>gpt-3.5-turbo-0125</code> (recommended), <code>gpt-3.5-turbo-1106</code>, <code>gpt-3.5-turbo-0613</code>, <code>babbage-002</code>, <code>davinci-002</code>, and <code>gpt-4-0613</code> (experimental)."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Preparing your dataset"
    },
    {
      "type": "paragraph",
      "html": "In this example, our goal is to create a chatbot that occasionally gives sarcastic responses, these are three training examples (conversations) we could create for a dataset:"
    },
    {
      "type": "code",
      "lang": "json",
      "code": "{\"messages\": [{\"role\": \"system\", \"content\": \"Marv is a factual chatbot that is also sarcastic.\"},\n{\"role\": \"user\", \"content\": \"What's the capital of France?\"}, {\"role\": \"assistant\", \"content\":\n\"Paris, as if everyone doesn't know that already.\"}]}{\"messages\": [{\"role\": \"system\", \"content\":\n\"Marv is a factual chatbot that is also sarcastic.\"}, {\"role\": \"user\", \"content\":\n\"Who wrote 'Romeo and Juliet'?\"}, {\"role\": \"assistant\", \"content\":\n\"Oh, just some guy named William Shakespeare. Ever heard of him?\"}]}{\"messages\": [{\"role\": \"system\",\n\"content\": \"Marv is a factual chatbot that is also sarcastic.\"}, {\"role\": \"user\", \"content\":\n\"How far is the Moon from Earth?\"}, {\"role\": \"assistant\", \"content\":\n\"Around 384,400 kilometers. Give or take a few, like that really matters.\"}]}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Convert CSV to json"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "json_string = \"\"\nfor index, row in data.iterrows():    message = str(row['Subject']) + \"   \" +\nstr(row['Description'])    answer = str(row['Answer'])    prompt =\n\"You are a formal chat bot assistant.\"    json_text =\n'{\"messages\": [{\"role\": \"system\", \"content\":\"'+prompt\n+'\"}, {\"role\": \"user\", \"content\":\"'+message+'\" }, {\"role\": \"assistant\", \"content\":\"'+answer+'\"} ]}'\njson_string = json_string + json_text  + \"\\n\""
    },
    {
      "type": "code",
      "lang": "python",
      "code": "with open(\"out.json\", \"w\") as text_file:    text_file.write(json_string)"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "ImportError: cannot import name ‘OpenAI’ from ‘openai’"
    },
    {
      "type": "paragraph",
      "html": "Run:<br>pip install openai — upgrade"
    },
    {
      "type": "paragraph",
      "html": "This is available only in version openai==1.2.0"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Upload a training file"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "from openai\nimport OpenAIclient = OpenAI()client.files.create(  file=open(\"mydata.jsonl\", \"rb\"),\npurpose=\"fine-tune\")"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "FileObject(id='file-KAdfdfgefd*****', bytes=54530, created_at=1710157205, filename='out.json',\nobject='file', purpose='fine-tune', status='processed', status_details=None)"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Waiting for Queue"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "client.fine_tuning.jobs.list(limit=1)"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "SyncCursorPage[FineTuningJob](data=[FineTuningJob(id='ftjob-zde************', created_at=1710169666,\nerror=Error(code=None, message=None, param=None, error=None), fine_tuned_model=None,\nfinished_at=None, hyperparameters=Hyperparameters(n_epochs=3, batch_size=1,\nlearning_rate_multiplier=2), model='gpt-3.5-turbo-0125', object='fine_tuning.job',\norganization_id='org-nUFI**************', result_files=[], status='queued', trained_tokens=None,\ntraining_file='file-TlDij************', validation_file=None, user_provided_suffix=None)],\nobject='list', has_more=True)"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Status Of traning"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "client.fine_tuning.jobs.retrieve(\"ftjob-zde*********\")"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "FineTuningJob(id='ftjob-zde***********', created_at=1710169666, error=Error(code=None, message=None,\nparam=None, error=None), fine_tuned_model=None, finished_at=None,\nhyperparameters=Hyperparameters(n_epochs=3, batch_size=1, learning_rate_multiplier=2),\nmodel='gpt-3.5-turbo-0125', object='fine_tuning.job', organization_id='org-nUFIO0I7*********',\nresult_files=[], status='queued', trained_tokens=None, training_file='file-TlDi****',\nvalidation_file=None, user_provided_suffix=None)"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Check the status"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "res = client.fine_tuning.jobs.retrieve(\"ftjob-zde*************\")\nres.status"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "status='queued'\nstatus='running'\nstatus='succeeded'"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Use a fine-tuned model"
    },
    {
      "type": "paragraph",
      "html": "name of model"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "res.fine_tuned_model"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "completion = client.chat.completions.create(  model=res.fine_tuned_model,  messages=[    {\"role\":\n\"system\", \"content\": \"You are a formal chat bot.\"},    {\"role\": \"user\", \"content\": \"Hello\"}\n])\nprint(completion.choices[0].message.content)"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Stackademic 🎓"
    },
    {
      "type": "paragraph",
      "html": "Thank you for reading until the end. Before you go:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "Please consider <strong>clapping</strong> and <strong>following</strong> the writer! 👏",
        "Follow us <a href=\"https://twitter.com/stackademichq\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>X</strong></a><strong> | </strong><a href=\"https://www.linkedin.com/company/stackademic\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>LinkedIn</strong></a><strong> | </strong><a href=\"https://www.youtube.com/c/stackademic\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>YouTube</strong></a><strong> | </strong><a href=\"https://discord.gg/in-plain-english-709094664682340443\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Discord</strong></a>",
        "Visit our other platforms: <a href=\"https://plainenglish.io\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>In Plain English</strong></a><strong> | </strong><a href=\"https://cofeed.app/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>CoFeed</strong></a><strong> | </strong><a href=\"https://venturemagazine.net/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Venture</strong></a><strong> | </strong><a href=\"https://blog.cubed.run\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Cubed</strong></a>",
        "More content at <a href=\"https://stackademic.com\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Stackademic.com</strong></a>"
      ]
    }
  ]
}
