All You Need to Know Before Using Free GPT3 APIs
As we all know that OpenAI made some of its APIs available to the public last month. With this, there are many people out there trying to read the documentation and test the GPT3 model performance on their data. This blog is a small summary of how to use OpenAI APIs for fine-tuning a GPT3 model using python, and what are the limitations of these APIs based on my experience so far.
In this article, I have explained how to use the GPT3 APIs in python and fine-tune a model for the natural language generation task.
Dataset used: e2e_nlg
Task: Train a model to generate descriptions in the restaurant domain from meaning representations, which consists in taking as input some data about a restaurant and generating a sentence in natural language that presents the different aspects of the data about the restaurant.
Code
!pip install openai
!pip install datasets
from datasets import load_dataset
import json
import requests
import pandas as pd
import numpy as np
import os
import re
import sys
import os
import openai
import time
dataset = load_dataset("e2e_nlg")
X_train = dataset['train']
X_validation = dataset['validation']
X_test = dataset['test']
openai.organization = "org-2*****************"
openai.api_key = "sk-R**************************"
#define all the data paths here
#path to store the json file created from the train data
train_json_path_exp = "/content/train_data_Exp_GPT3.jsonl"
#path to store the json file created by GPT3
train_json_path_exp_gpt3 = "/content/train_data_Exp_GPT3_prepared.jsonl"
#number of test samples to use because of limit of 60/min API calls
samples_tested = 10
#Amount of training data used due to limitation on tokens (300000)
samples_trained = 500 #because there is a token limit
#Define goal for the GPT3
goal = "Generate descriptions in the restaurant domain from meaning representations."
#start processign the data for training
sentences = [] #contains the entire prompt
#fill the sentences in the input for prompt
for i in range(0, len(X_train)):
temp_representation = re.sub(r'[^A-Za-z0-9 ]+', '', X_train[i]['meaning_representation'])
temp_sent = 'Goal: ' + goal + ' \nParagraph: ' + temp_representation + '\n\n###\n\n'
sentences.append(temp_sent)
#Now we will start preparing the data for trainign our GPT3 model
prompts = []
for i in range(0, len(sentences)):
adder = {'prompt': sentences[i], 'completion': X_train[i]['human_reference']}
prompts.append(adder)
#Dump the data to a json file
with open(train_json_path_exp, 'w') as fp:
json.dump(prompts, fp, indent=4)
#run the gpt3 command to convert the json file to gpt3 input data
!openai tools fine_tunes.prepare_data -f $train_json_path_exp
openai.File.create(
file=open(train_json_path_exp_gpt3),
purpose='fine-tune'
)
openai.FineTune.create(training_file="file-9Nmq*****************")
领英推荐
openai.FineTune.list_events(id="ft-bfq2PK7PYUqPPugd4mwK3AEv")
s_prompt = 'Goal: ' + goal + ' \nParagraph: ' + incomplete_sentence + '\n\n###\n\n
completion = openai.Completion.create(
model = "curie:ft-****************",
prompt=s_prompt
)'
The above command will return a JSON that will contain the completion of the input prompt. In the e2e_nlg dataset, the formatted input/output will look something like this
Input: Goal: Generate descriptions in the restaurant domain from meaning representations.
Paragraph: name[The Vaults], eatType[pub], priceRange[more than £30], customer rating[5 out of 5], near[Cafe Adriatic]
###
Output: The Vaults pub near Café Adriatic has a 5 star rating. Prices start at £30.
The entire source code can be found here: colab_notebook
Limitations
<OpenAIObject list at 0x7f602c2247d0> JSON: {
"data": [
{
"created_at": 1639215448,
"level": "info",
"message": "Created fine-tune: ft-pphZx3fnoxMKtwPduAruS3du",
"object": "fine-tune-event"
},
{
"created_at": 1639215454,
"level": "error",
"message": "Fine-tune failed. Fine-tune will exceed billing hard limit",
"object": "fine-tune-event"
}
],
"object": "list"
}
openai.Completion.create(
model=FINE_TUNED_MODEL,
prompt=YOUR_PROMPT
)
openai api fine_tunes.create -t <TRAIN_FILE_ID_OR_PATH> -m <BASE_MODEL>
<OpenAIObject list at 0x7f602d483950> JSON: {
"data": [
{
"created_at": 1639216088,
"level": "info",
"message": "Created fine-tune: ft-bfq2PK7PYUqPPugd4mwK3AEv",
"object": "fine-tune-event"
},
{
"created_at": 1639216094,
"level": "info",
"message": "Fine-tune costs $32.95",
"object": "fine-tune-event"
},
{
"created_at": 1639216094,
"level": "info",
"message": "Fine-tune enqueued. Queue number: 0",
"object": "fine-tune-event"
},
{
"created_at": 1639216098,
"level": "info",
"message": "Fine-tune started",
"object": "fine-tune-event"
}
],
"object": "list"
}
References
Thank you for reading....
Let me know what you think...
Computer Science Student
1 年this is pretty awesome! I am going to be using the GPT API to create a cover letter generator bot based on my resume and it will also tell me what talent acquisition teams to sign up for so I don't need to parse the data.