The comprehensive guide aims to provide an in-depth understanding of the key features of GPT-3 & 3.5 (Chat GPT), including an overview of the model, its API, and examples of its practical applications. The guide also focuses on the technique of fine-tuning, which is essential for optimizing the model's performance for specific tasks. By the end of the guide, readers will have gained a comprehensive understanding of how to work with OpenAI GPT-3 & 3.5 and leverage its power for their specific use case
You can watch the video-based tutorial with a step-by-step explanation down below.
Overview of GPT
OpenAI GPT (Generative Pre-trained Transformer) is a language processing model developed by OpenAI. It uses artificial intelligence to generate human-like text based on given prompt or input. OpenAI GPT is trained on a vast amount of text data and has the ability to understand and generate natural language. It has been used in various applications such as language translation, chatbots, and content generation. The most recent version of OpenAI is GPT-3.5.
GPT-3.5 is the third iteration of the GPT series and represents a significant advancement in terms of model size and capabilities. It has 175 billion parameters, making it one of the largest and most powerful language models currently available. With its extensive training data and large parameter count, GPT-3.5 can generate responses that are highly coherent, contextually relevant, and often indistinguishable from human-written text
Let us see how GPT models are trained
GPT models are trained using a two-step process: pre-training and fine-tuning
During the pre-training phase, the GPT model is trained on a large corpus of publicly available text from the internet. This corpus can consist of a wide range of sources, including books, articles, websites, and other textual data. The purpose of pre-training is to expose the model to a diverse set of language patterns and structures, allowing it to learn the statistical relationships between words, sentences, and documents
The pre-training process uses a variant of unsupervised learning called self-supervised learning. In this approach, the model is trained to predict missing words in a given sentence. The model learns to predict the next word in a sequence based on the context of the previous words. By making these predictions, the model is forced to understand the semantics, syntax, and context of the text it encounters
Once the pre-training phase is complete, the model is fine-tuned for specific downstream tasks. Fine-tuning involves training the model on task-specific datasets that are carefully curated for the target application. For example, if the goal is to create a chatbot, the model may be fine-tuned on a dataset of conversational data
The training process involves training on high-performance GPUs or TPUs and can take a considerable amount of time to complete
Some of the key capabilities of GPT models:
Text Completion: GPT models can generate coherent and contextually relevant text to complete a given prompt or sentence. They can predict and generate the next word or phrase based on the provided context
Language Translation: GPT models can be fine-tuned to perform language translation tasks. They can translate text from one language to another, capturing the meaning and preserving the context of the original text
Question-Answering: GPT models can understand questions and generate relevant answers based on their understanding of the context. They can provide informative responses to a wide range of questions
Text Summarization: GPT models can generate concise and informative summaries of long texts or articles. They can distill the essential information and present it in a coherent and condensed form
Sentiment Analysis: GPT models can analyze the sentiment of a given text and determine whether it is positive, negative, or neutral. This capability is useful in tasks such as opinion mining and social media analysis
Language Generation: GPT models can generate natural language text, including stories, poems, dialogues, and creative writing. They can produce coherent and contextually appropriate text in various genres and styles
Conversational Agents: GPT models can be used to develop chatbots and virtual assistants. They can engage in interactive conversations, understand user inputs, and generate appropriate responses, simulating human-like interactions
Information Retrieval: GPT models can retrieve relevant information from large collections of documents or texts. Given a query or a topic, they can identify and extract relevant information to provide informative responses
Paraphrasing: GPT models can generate alternative phrasings or rephrase sentences while preserving the original meaning. This capability is useful for tasks such as text simplification, content rewriting, or generating diverse sentence variations
Contextual Understanding: GPT models excel at understanding the context and generating text that is contextually appropriate. They can consider the entire input sequence and produce responses that are coherent and relevant to the given context
Let us see the models supported by OpenAI
MODEL | DESCRIPTION |
---|---|
GPT-3.5 | A set of models that improve on GPT-3 and can understand as well as generate natural language or code |
DALL·E | A model that can generate and edit images given a natural language prompt |
Whisper | A model that can convert audio into text |
Embeddings | A set of models that can convert text into a numerical form |
Moderation | A fine-tuned model that can detect whether text may be sensitive or unsafe |
GPT-3 | A set of models that can understand and generate natural language |
GPT Parameters and it's usage
Temperature - It controls the randomness of the text generated by the GPT model. It determines the level of creativity or randomness in the model's responses. When the temperature is set to a lower value, such as 0.2, the generated text tends to be more focused and deterministic. It produces more conservative and predictable responses, often selecting the most likely word choices. On the other hand, when the temperature is set to a higher value, like 0.8, the generated text becomes more diverse and creative. The model is more likely to explore different word choices, resulting in more varied and unexpected responses
Maximum length - The maximum number of tokens to generate. Requests can use up to 2048 or 4000 tokens shared between prompt and completion. The exact length varies by model
Stop sequences - It refers to a special token or sequence of tokens that you can specify to instruct the model to stop generating text. When the model encounters the specified stop sequence in its generated output, it will halt the generation process and return the text up until that point
Top P - It is also known as nucleus sampling or the "penalty" parameter, is a technique used in text generation models like GPT to control the diversity and randomness of the generated output. It is used in conjunction with the temperature parameter
Frequency Penalty - It is used to penalize new tokens based on their existing frequency in the text
Presence Penalty - It is used to penalize new tokens based on whether they appear in the text
GPT Modes
Insert Mode - It is a feature in ChatGPT, which allows you to guide or control the conversation more explicitly by specifying system and user instructions. It also allows you to have more control over the conversation flow and direct the model's responses based on the given instructions. You can extend the conversation by appending new user and assistant instructions to the existing conversation history, which helps in maintaining context and continuity
Edit Mode - It refers to the ability to modify the model's generated text directly within the text input field. This feature allows you to refine or adjust the prompt and observe the model's response in real-time
Chat Mode - It allows you to have interactive conversations with the model by providing a series of user messages. This feature enables back-and-forth exchanges with the model, simulating a chat-like experience
Complete Mode - The completions endpoint can be used for a wide variety of tasks. It provides a simple but powerful interface to any of our models. You input some text as a prompt, and the model will generate a text completion that attempts to match whatever context or pattern you gave it. For example, if you give the API the prompt, "As Descartes said, I think, therefore", it will return the completion " I am" with high probability
OpenAI for API
First we will install OpenAI module for Python
!pip install openai
Import Modules
import os
import openai
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
os - used to handle files using system commands
openai - provides a range of methods and functionalities to interact with the language models efficiently. It allows developers to make API calls, process text inputs, and retrieve model-generated outputs for various NLP tasks
pandas - used for data manipulation and analysis. It provides high-performance, easy-to-use data structures and data analysis tools
warnings - used to control and suppress warning messages that may be generated by the Python interpreter or third-party libraries during the execution of a Python program
Next we will export OpenAI API key as environment variable
%env OPENAI_API_KEY=<key>
Replace <key> with OpenAI API key that you have generated
This will set it as a environment variable.
Next we will set OpenAI API key
# set openai api key
openai.api_key = os.getenv('OPENAI_API_KEY') # key
This sets the API key for the OpenAI library by retrieving it from an environment variable named OPENAI_API_KEY
GPT-3 API usage in Python
Let us see an example of using the OpenAI API to make a completion request for generating text
response = openai.Completion.create(
engine = 'text-davinci-003',
prompt = "what is AI?",
temperature = 0.2,
max_tokens = 256,
top_p = 1
)
response
openai.Completion.create() method is used to create a completion request to the OpenAI API
The engine parameter specifies the language model or engine to be used for the completion. In this case, it's set to 'text-davinci-003', which represents the Davinci engine, a powerful and versatile language model provided by OpenAI
The prompt parameter provides the starting prompt or input text for the model to generate a completion
The temperature parameter controls the randomness of the generated output
The max_tokens parameter sets the maximum length of the generated completion, in this case, limited to 256 tokens. The model will generate text until it reaches the specified token limit
The top_p parameter, also known as nucleus sampling or the "P-Top" strategy, determines the probability distribution of words to consider during text generation
You will see the following result :
<OpenAIObject text_completion id=cmpl-6pz3XzafszXyz2okMfSnmEAIhltlx at 0x186bc6d59a0> JSON: {
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"text": "\n\nAI (Artificial Intelligence) is a branch of computer science that focuses on creating intelligent machines that can think and act like humans. AI systems are designed to learn from their environment and experiences, and use this knowledge to make decisions and solve problems. AI can be used in a variety of applications, such as robotics, natural language processing, computer vision, and machine learning."
}
],
"created": 1677847447,
"id": "cmpl-6pz3XzafszXyz2okMfSnmEAIhltlx",
"model": "text-davinci-003",
"object": "text_completion",
"usage": {
"completion_tokens": 76,
"prompt_tokens": 4,
"total_tokens": 80
}
}
Now let us just print the text that we got in the above output
print(response['choices'][0]['text'])
AI (Artificial Intelligence) is a branch of computer science that focuses on creating intelligent machines that can think and act like humans. AI systems are designed to learn from their environment and experiences, and use this knowledge to make decisions and solve problems. AI can be used in a variety of applications, such as robotics, natural language processing, computer vision, and machine learning.
This is the text generated for the given prompt
Next let us create a function for the GPT-3 API
def ask_gpt(prompt):
response = openai.Completion.create(
engine = 'text-davinci-003',
prompt = prompt,
temperature = 0.2,
max_tokens = 256,
top_p = 1
)
return response['choices'][0]['text']
The code snippet defines a function called ask_gpt that utilizes the OpenAI API to generate a text completion based on a given prompt
This function takes a prompt as input and returns the generated text completion
Next let us call the function by giving some prompt as input
print(ask_gpt("What do you think about life?"))
Life is unpredictable and full of surprises! It's beautiful and challenging, and we should strive to make the most of it by being present and mindful.
Next let us see an example for sentiment analysis
prompt = "Identify the sentiment for the following sentence\n This tutorial is really good!!\n\n Sentiment: "
print(ask_gpt(prompt))
Positive
This is the response for the given prompt
GPT-3.5 or Chat GPT API usage in Python
Let us see an example to initiate a chat-based conversation using the GPT-3.5-turbo model
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is OpenAI?"}
]
)
print(response)
openai.ChatCompletion.create() method creates a chat-based completion request using the GPT-3.5-turbo model
The model parameter specifies the model to be used for the chat-based completion. In this case, it is set to "gpt-3.5-turbo", which is a highly advanced and efficient language model provided by OpenAI
The messages parameter is a list of messages in the conversation. Each message has a role ("system" or "user") and content (the text of the message). In this example, the conversation starts with a system message followed by a user message
You will see the following result:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "\n\nOpenAI is a non-profit research organization that is focused on developing advanced artificial intelligence in a safe and transparent manner. The company was founded in 2015 by a group of leading technology executives, including Tesla CEO Elon Musk and Sam Altman, and has since become one of the most prominent AI research organizations in the world. OpenAI's research focuses on developing cutting-edge AI technologies that can be used to benefit society, while also addressing concerns about the potential risks associated with powerful AI systems. The company has also developed several high-profile AI tools, including the popular GPT-3 language model, which can generate highly realistic and coherent natural language text.",
"role": "assistant"
}
}
],
"created": 1677848100,
"id": "chatcmpl-6pzE42trd2uCPANFDdQvgRIMoBEnT",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion",
"usage": {
"completion_tokens": 132,
"prompt_tokens": 23,
"total_tokens": 155
}
}
Next we will ask the follow up question on the above generated response
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is OpenAI?"},
{"role": "assistant", "content": "\n\nOpenAI is a non-profit research organization that is focused on developing advanced artificial intelligence in a safe and transparent manner. The company was founded in 2015 by a group of leading technology executives, including Tesla CEO Elon Musk and Sam Altman, and has since become one of the most prominent AI research organizations in the world. OpenAI's research focuses on developing cutting-edge AI technologies that can be used to benefit society, while also addressing concerns about the potential risks associated with powerful AI systems. The company has also developed several high-profile AI tools, including the popular GPT-3 language model, which can generate highly realistic and coherent natural language text."},
{"role": "user", "content": "What other companies that the executives own?"},
]
)
print(response)
You will see the following result :
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Elon Musk is the CEO of Tesla, a company that designs and manufactures electric cars, as well as SpaceX, a company that designs and launches advanced rockets and spacecraft. Sam Altman is also the CEO of OpenAI and was previously the president of startup accelerator Y Combinator.",
"role": "assistant"
}
}
],
"created": 1677848294,
"id": "chatcmpl-6pzHCMpNJxTeMR6eNGjEZhMQnUO9G",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion",
"usage": {
"completion_tokens": 59,
"prompt_tokens": 172,
"total_tokens": 231
}
}
Next let us create a chat interface for the GPT-3.5 API
def chatGPT(messages):
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = messages
)
return response['choices'][0]['message']
conversations = [
{"role": "system", "content": "You are a helpful assistant."}
]
while True:
user_input = input()
print()
format_input = {"role": "user", "content": f"{user_input}"}
conversations.append(format_input)
result = chatGPT(conversations)
print(result['content'])
conversations.append(result)
print()
openai.ChatCompletion.create() method creates a chat-based completion request using the GPT-3.5-turbo model
We will append the user input to the conversations list as a dictionary
The openai.ChatCompletion.create method expects the messages in a specific format. Each message should be a dictionary with 'role' and 'content' keys. The 'role' can be 'system', 'user', or 'assistant', and the 'content' contains the actual text of the message
You will see the following result :
who are you?
I am an AI language model designed to provide assistance and answer questions to the best of my abilities.
what abilities you have?
As an AI language model, I can understand and analyze natural language, generate text, perform simple calculations, provide information on a wide range of topics, and assist with various tasks and processes. However, my abilities are limited to what I have been programmed to do and the data I have been trained on.
You can see that you can ask questions one by one and chatgpt will provide the answers like it does in chat interface
Fine Tuning GPT-3
Fine-tuning refers to the process of further training a pre-trained language model, such as GPT-3, on a specific dataset or task to improve its performance and adapt it to a specific domain or application
First we will have to create the data in jsonl format as given below
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}
text = [
"Weather is good today!",
"I am okay now",
"I don't like the food",
"I love to travel",
"He is very bad at studying",
"The price is average"
]
sentiments = [
"positive",
"neutral",
"negative",
"positive",
"negative",
"neutral"
]
This is the input to the model
Next let us parse the input as list of jsons in the required format
# parse the input as list of jsons
formatted_input = []
for prompt, completion in zip(text, sentiments):
formatted_input.append({"prompt": f"{prompt}\n", "completion": completion})
The above code snippet is iterating over two lists, text and sentiments, and creating a new list formatted_input with dictionaries containing two keys: "prompt" and "completion". Each dictionary in the formatted_input list represents a prompt-completion pair
This creates a mapping between prompts and their corresponding completions, where each prompt is followed by a newline character ('\n')
Next let us print the formatted input
formatted_input[:3]
[{'prompt': 'Weather is good today!\n', 'completion': 'positive'},
{'prompt': 'I am okay now\n', 'completion': 'neutral'},
{'prompt': "I don't like the food\n", 'completion': 'negative'}]
This is the expected format
Next we will store the json in a file
# dump the json for training
import json
with open('training.jsonl', 'w') as outfile:
for entry in formatted_input:
json.dump(entry, outfile)
outfile.write('\n')
First import the json module to handle JSON serialization and deserialization. Then, it opens a file named 'training.jsonl' in write mode using the open function. The file will be created if it doesn't exist, and if it does exist, its contents will be overwritten
Then we will iterate over each entry in the formatted_input list. For each entry, json.dump is used to serialize the dictionary as a JSON object and write it to the file. After writing the JSON object, outfile.write('\n') is used to write a newline character to separate the entries in the JSON lines format
Validate the jsonl file before training
Run this in a command line to validate
openai tools fine_tunes.prepare_data -f <LOCAL_FILE>
This will create a trained jsonl file
This is the file prepared after validating
We can see that space and \n is added when compared to previous file content in completion
Next let us export OpenAI API key
export OPENAI_API_KEY="<OPENAI_API_KEY>"
Training the Model
Next run the below command to train the model
openai api fine_tunes.create -t <TRAIN_FILE_ID_OR_PATH> -m <BASE_MODEL>
In our case we will be training on davinci model
Test the Model
Next we will run the below command to test the fine tuned model
openai api completions.create -m <FINE_TUNED_MODEL> -p <YOUR_PROMPT>
Use Fine Tuned Model
Next we will use the trained fine tuned model in the actual API
FINE_TUNED_MODEL = "<model_name>"
Here we have to replace <model_name> with the fine tuned model that we have created
We will use the ask_gpt() function that we have created earlier, we will replace the engine parameter with Fine tuned model
def ask_gpt(prompt):
response = openai.Completion.create(
engine = FINE_TUNED_MODEL,
prompt = prompt,
temperature = 0.2,
max_tokens = 256,
top_p = 1
)
return response['choices'][0]['text']
Other Models
Apart from GPT-3 and GPT-3.5 there are many other text generation models that can be used to perform various tasks. Some of the models are
GPT-Neo
GPT-J
T5
BLOOM
BERT
Advantages of GPT-3
Large-scale pre-training: GPT-3 is trained on a massive amount of diverse text data, allowing it to learn a wide range of language patterns, grammar, and semantic relationships
Versatility: GPT-3 is a general-purpose language model that can be applied to a wide range of tasks, such as text completion, question-answering, summarization, translation, and more. Its flexibility makes it useful in various domains, from content generation to conversational agents
Context understanding: GPT-3 has the ability to understand and generate text based on contextual cues. It can consider the preceding text to maintain coherence and relevance in its responses
Creative generation: GPT-3 has demonstrated impressive creative capabilities, such as generating poetry, stories, and even code
Knowledge recall: GPT-3 can recall and provide information from its vast pre-trained knowledge
Continual learning: GPT-3 has the ability to learn from user interactions and feedback. By incorporating user input and fine-tuning, the model can adapt to specific domains or improve its performance on particular tasks over time
Language understanding and generation: GPT-3 exhibits impressive language understanding and generation capabilities. It can comprehend complex sentences, handle nuanced language, and generate coherent and contextually appropriate responses
Availability of API: GPT-3 is accessible through the OpenAI API, allowing developers and researchers to integrate its capabilities into their own applications and systems
Disadvantages of GPT-3
Lack of common sense and real-world knowledge: GPT-3's knowledge is primarily based on the patterns and information it learned from the pre-training data. It may lack real-world common sense and practical knowledge, leading to occasional incorrect or nonsensical responses
Tendency to be verbose and overuse certain phrases: GPT-3 has a tendency to be verbose and may use certain phrases or language patterns excessively
Difficulty with nuanced or sensitive topics: GPT-3 may struggle with understanding and addressing nuanced or sensitive topics appropriately
Lack of explainability and transparency: GPT-3 operates as a black box model, meaning it can be challenging to understand its internal processes and how it arrives at specific responses
Limited control over generated output: While GPT-3 can be conditioned or primed with specific prompts, there is limited control over the exact output it generates
Dependency on large-scale computational resources: GPT-3 is a highly computationally demanding model, requiring significant computational resources to train and generate responses
Potential for biased outputs: GPT-3's training data may contain biases present in the text sources it learned from. These biases can manifest in its generated outputs, perpetuating and amplifying existing biases
High cost of usage: Utilizing GPT-3 through the OpenAI API or other means can be costly, especially for large-scale applications or frequent usage
Final Thoughts
Using OpenAI GPT-3 and GPT-3.5 Turbo can bring several benefits and advantages to various language-related tasks. These models offer large-scale pre-training, versatility across different applications, context understanding, creative generation, knowledge recall, and the availability of an API for integration
However, it's essential to be aware of the potential disadvantages and limitations associated with these models. it's crucial to consider these limitations and employ appropriate strategies to mitigate risks
In this comprehensive guide we have explored how to work with OpenAI GPT-3 & 3.5 and leverage its power for their specific use case .We have also explored how we can fine tune the model and use it for the specific use case . We have also seen the advantages and disadvantages of OpenAI GPT-3 & 3.5
Get the project notebook from here
Thanks for reading the article!!!
Check out more project videos from the YouTube channel Hackers Realm