Fine-tuning GPT-3 Using Python to Create a Q&A Assistant
CrossML Pvt Ltd
Award-winning AI company with 100+ solutions delivered in GenAI, Machine Learning & Digital Transformation.
Using an OpenAI model for FAQ by training it on a custom dataset
What is Fine-tuning?
Fine tuning allows one to train a pre-trained LLM on a smaller, domain-specific dataset to learn specific patterns and relationships within this dataset and generalize its knowledge for the task at hand.
Fine-tuning, in essence, allows us to adjust the pre-trained model to a customized dataset and optimize its performance for the task at hand (NLP application). Fine-tuning in GPT-3 entails modifying the model's parameters either manually or by training it on a dataset tailored for the given task.
According to the official documentation, fine-tuning allows in getting more out of the GPT-3 models by providing:
OpenAI models
OpenAI utilizes a range of LLM models with varying capabilities and costs mentioned below:
OpenAI's GPT-3.5 model does not support fine-tuning. GPT-3.5 is designed to be a highly performant and efficient language model that is already pre-trained on a wide range of natural language processing tasks, making it well-suited for many real-world applications without the need for further fine-tuning.
Dataset preparation
For fine tuning OpenAI GPT3 models, a set of training samples that contain a single input (prompt) and its corresponding output (completion), separated by a fixed separator (->) indicating the end of the prompt and the beginning of the completion.
This blog uses a custom dataset prepared after manually segregating question-answer pairs paragraph-by-paragraph from a 35 page PDF document. In the dataset shown below, the question is listed as the prompt while the answer is the completion.
Important considerations
Creating the dataset
In case the training dataset is not in the required file format, OpenAI’s CLI tool can be used to convert CSV, TSV, XLSX and JSON files to the required JSONL format as implemented below:
!pip install openai??
openai tools fine_tunes.prepare_data -f <LOCAL_FILE>?
Once a JSONL document has been prepared, run the following command to create a file for fine-tuning the model
Creating a fine-tuned model
There are three methods for fine-tuning a GPT-3 model:
Programmatic finetuning and endpoint based finetuning are beyond the scope of this implementation. The steps for OpenAI CLI implementation are listed below.
领英推荐
Step1:
Set OPENAI_API_KEY environment variable by including the following line in your shell/terminal:
?????? export OPENAI_API_KEY="<OPENAI_API_KEY>"
Step2:
To begin the fine-tuning process, specifying the GPT-3 model to be used with the "-m" flag. The 'ada' model was chosen since it is the lowest cost model.
?openai api fine_tunes.create -t test.jsonl -m ada --suffix "custom model name"
It may take some time to finish a fine-tune work once you've begun it. There is a possibility that the current process may be queued behind other jobs on the system, depending on the model and dataset size, training can take minutes or hours. If for some reason the event stream is disrupted, resume it by running:
? openai api fine_tunes.follow -i <YOUR_FINE_TUNE_JOB_ID>
When the job is done, it should display the name of the fine-tuned model.
The convention followed for the model name is <base-model>:<suffix>:<timestamp>, in our example the name assigned to the fine tuned model is ada:ft-personal-2023-05-12-11-18-17
Using the model
We will use our trained fine-tuned model for question answering by logging in to Playground and choosing our model name from the list of models provided by OpenAI.
Below the model name you can assign completion parameters to the trained model. Each parameter (described below) shown below Fig.5 can influence the output of a generative model.
Result
Conclusion
In the field of Natural Language Processing (NLP), fine-tuning GPT-3 and other large language models (LLMs) has become the ideal method for developing solutions that are specialized to a given domain, resulting in impressive performance gains. We can create extremely effective question-and-answer (Q&A) assistants that excel at comprehending and producing human-like responses by harnessing the power of these models and fine-tuning them.
At CrossML Private Limited, we leverage generative AI and GPT to build applications like Personalised Customer Service Bots, Automated Marketing Content generation, Knowledge Search etc. Reach out to our expert AI team for a free consultation at [email protected]
Follow us on LinkedIn https://www.dhirubhai.net/company/crossml