How to Fine Tune GPT 3.5 Turbo with OpenAI API

In this video we're going to cover how to fine tune a GPT 3.5 Turbo Model with the OpenAI API. In order to follow along with this video you will need to have an OpenAI Secret Key, you can get one from openai.com. If you don't already have one, make sure you check out this article.
For this video we're going to follow the rough guideline provided by OpenAI, however I will provide to you the files that I will be using to fine tune my model and talk through the edge cases that aren't covered in the public guide.
The files used in this article are members only, so make sure you sign up for your free membership and you'll automatically be enrolled in my newsletter where you can stay up to date with me and the latest AI news.
What is fine tuning?
This is an important concept to touch on before starting the guide. Have you ever been frustrated that the LLM model that you're using doesn't quite get the job done? It gets 90% there but falls short because of personal preference or lacking non public information like company records or private notes, this can be easily be solved with fine tuning!
Fine tuning builds on top of a foundational LLM model with new information that you provide in the form of example prompts and then creates a new model that is private to you that contains this information.
This is useful if you wanted AI model to use:
- Private information, like company documents, notes, etc that aren't meant to be available for the public
- Custom preferences in responses like speaking tone or writing style like formal, casual, with slang, etc.
- Custom instructions to mispell words intentionally or speaking with more syllables.
Lets get started
The general guideline for fine tuning an AI Model with OpenAI API is 4 steps.
- Upload the data to OpenAI Servers.
- Start a fine tuning job with your data
- Wait until the tuning job is done and get your newly created model ID
- Use the Model ID for your chat completion API calls
Some important things to know is that your tuning data needs to be in JSONL format. If you're not familiar, this is an array of JSON dictionaries formatted in a list. In addition you need atleast 10 examples for the model to be fine tuned.
Upload the data to OpenAI Servers
This is pretty self explanatory, most likely due to security concerns, we need to upload our training data to OpenAI servers in order to fine tune. This is to a simple command, simply point the fine tuning file path to the downloaded file in the members only area and grab the fine ID from the resulting output.
export FINE_TUNE_FILE={Your File Path Here}
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F "purpose=fine-tune" \
-F "file=@$FINE_TUNE_FILE" ```
The resulting file ID should be prefixed with the characters file-
. We want to take this and use it for the next API call.
Start the Fine Tuning Job
Now we want to start running the fine tuning job. Using the file id from the previous call, we want to put it into the next API call.
# Start the fine tuning job
curl https://api.openai.com/v1/fine_tuning/jobs \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"training_file": "<Replace with your file id>",
"model": "gpt-3.5-turbo-0613"
}'
This will start the fine tuning job and you should get a job ID that can be used in the next example.
Wait for the Fine Tuning Job to finish
There are 2 ways to be notified of the fine tuning job to be finished. You can either call the list fine tuning jobs API endpoint, or wait to receive the email from OpenAI.
To call the API simply take the fine tuning job ID from the previous result and use it for the API call below.
# Get all of the events for the fine tuning job
curl https://api.openai.com/v1/fine_tuning/jobs/{ft_job_id}/events \
-H "Authorization: Bearer $OPENAI_API_KEY"
What you're looking for now is an event with the job id and a status that says completed. There should be a model id that looks like ft:gpt-3.5-turbo:<custom id>
and we will need that for the last API call.
If you don't want to get into the weeds, OpenAI will send an email when the training is done that contains your model ID.

Congrats! This is your brand new custom model that is only accessible to those that have your API key.
Use your new Fine Tuned Model
Now the last, but best part, use your newly fine tuned model in a chat completion API call. One important thing to note is that this is based on GPT 3.5 Turbo so this will not work with the completion API. You must use this with the chat completion API.
# Test the fine tuned model
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "<Replace with your model id>",
"messages": [
{
"role": "system",
"content": "You are an assistant that occasionally misspells words"
},
{
"role": "user",
"content": "Hello! What is fine-tuning?"
}
]
}'
Just replace the model with your newly created model ID and you should be done!
Now that wasn't so bad, huh? Congratulations you now have a fine tuned GPT 3.5 Turbo model that can be used in various applications and use cases.