Fine-Tuning GPT3 for free

Shreyz-max
5 min readJul 23, 2022

Using GPT3 on your data for free

DALLE’s version of training AI

What do you call French bread? Loaf. (made me chuckle don’t ask why)

This is why I chuckled.

This is one of the jokes generated by GPT3 after it was fine-tuned on some jokes from Reddit. For more AI-generated jokes scroll to the end of the article where I write some of my favourite jokes generated by GPT3.

GPT3 is the new state-of-the-art language model. When it was released back in 2020, it was hyped a lot. It did live up to its hype. The beta version was available for very few people and organisations. Later, the playground access was available for almost everyone. However, even then fine-tuning was not available. Even though the model weights have yet not been open-sourced we can at least fine-tune it now based on our dataset. Some question OpenAI for its biasedness and for not open-sourcing the weights. However, this discussion is for some other time.

OpenAI provides $18 worth of free credits when you create an account to get access to GPT3. We will use this feature to fine-tune any of the three models curie, babbage and ada. The largest model Davinci is still not available for fine-tuning. Some other limitations include that one can fine-tune up to 10 models per month and each dataset can be up to 2.5M tokens or 80–100MB in size.

To start with fine-tuning we need first to create an account in OpenAI. You can do so using this link. Once you have created an account, the next step involves getting your API key. This is unique for each user. Now we will open our Jupyter Notebook.

There are many ways to fine-tune GPT3 : (i) using OpenAI CLI, (ii) using the OpenAI package, and (iii) using requests.

I will be using the OpenAI package because it is the easiest.

FINE-TUNING USING OPENAI PACKAGE

The main steps involved in fine-tuning are:

  1. Preparing the fine-tuning dataset.
  2. Fine-tuning the model.
  3. Using the fine-tuned model for inference.
!pip install openai

Next, let us start making the required imports.

import openaiimport pandas as pdimport stringopenai.api_key = 'YOUR API KEY'

Let us now read the dataset. I have used a random dataset that scraped some jokes off Reddit. You can use any dataset you like.

df = pd.read_csv('jokes.csv')df.head()

Ideally, the dataset to fine-tune GPT3 should be a jsonl file that should look like this.

{"prompt": "<text>", "completion": "<text to be generated>"}
{"prompt": "<text>", "completion": "<text to be generated>"}
{"prompt": "<text>", "completion": "<text to be generated>"}

For text completion we shall provide it with some prompt text however for generation of text we will leave prompt blank. I have tried both ways and I realised that providing some text to the prompt gives better results compared to completion.

1. PREPARING THE FINE-TUNING DATASET

However, OpenAI has a feature which converts CSV, TSV, XLSX, and JSON to JSONL files. I have preprocessed the dataset. Let us now convert it into jsonl format.

!yes | openai tools fine_tunes.prepare_data -f 'joke.csv'

yes denotes that all options in fine-tuning should be set to true. Once the jsonl file has been prepared our next step involves creating a file id from this file. We shall do so using this command.

#write your file name instead of jokes_prepared.jsonlwith open("joke_prepared.jsonl") as f:      response = openai.File.create(file=f, purpose='fine-tune')print(response)

A file id will be available to you.

2. FINE-TUNING THE MODEL

Now that our data is in the required format and the file id has been created, the next task is to create a fine-tuning model. This can be done using:

response = openai.FineTune.create(training_file="YOUR FILE ID", model='ada')

Change the model to babbage or curie if you want better results. By default, it runs 4 epochs to fine-tune a model. The fine-tuning will take some time depending on the size of the dataset and the model that you will be using. You can keep checking up on the fine-tuning process using the following command.

response = openai.FineTune.retrieve(id="YOUR FINE-TUNE ID")

The creation of fine-tuning file will create an id. Use that for retrieving the status on the fine-tuning. Once the fine-tuning is over, you will see the status changes to processed from processing in the response.

3. USING THE FINE-TUNED MODEL FOR INFERENCE

Now that our model has been fine-tuned we can use it for inference. The response message will contain the id of the fine-tuned model. We will be using that id to call our model.

# ft_model should have your model id
ft_model = "ada:ft-personal-2022-07-22-11-25-19"
res = openai.Completion.create(model=ft_model, prompt="I hate ... ", max_tokens=18, temperature=1)

You can change max_tokens to the size of the generated text you want. You can also experiment with the temperature parameter. It takes values from 0 to 2.

The whole code for fine-tuning is available here.

Now, let me show you some other jokes generated by GPT3 that may not be on my list of all-time favourite jokes but are worth a look:

  1. I hate … iphones !!!!!! Why do they have to have so many tones? (Wow super strong on punctuations)

2. What do you call 4 in the afternoon? Grease afternoon!

3. Knock Knock … -> Who’s there? cow ooooooooooo “Cow who? Cow, who loves you” -> “Cow has lost its voice” ooooooooooooo

4. How do you feed a friend with no mouth? you feed them a fork. (Flawed sense of logic by GPT3)

5. Why are ants not allowed at the party? Because they all have stings.

Although most of them do not make sense we already knew that since GPT3 is not that great when it comes to logic. Here are some other jokes to make you chuckle.

  1. Why are ants not allowed at the party? Because they all have stings.
  2. Why is . -> . an illegal operation? Because it is prohibited by the * cow * contract! (No clue what is illegal but cow contract makes sense)
  3. Why are physicists so bitter? Because they can’t stand (P.S. I am sorry physicists but apparently all of you are Stephen Hawking).

I hope you enjoyed the article. If so, do give it a clap and follow me for more interesting articles.

--

--

Shreyz-max

AI Research Engineer @LatitudeResearch | Assistant Researcher @E4E | VIT Vellore |