Skip to content

How to Create an AI Writer or Chatbot Tool using GPT-3

How to Create an AI Writer or Chatbot Tool using GPT-3


Overview:

In this article, I will explain what the OpenAI GPT-3 Artificial Intelligence is, what are its different models, and how you can create an AI writer or chatbot tool using GPT-3 for the purpose of content generation.

What is the OpenAI GPT-3 model?

OpenAI GPT-3 is an Artificial Intelligence model that was created by OpenAI, a research laboratory focused on developing AI. The model is a large-scale transformer-based language model that was trained on a dataset of more than a billion words from different data sources such as books, articles, and web pages. GPT3 model can be used to generate any type of human-like text. The model processes your text prompt and tries to predict what’s most likely to come next. It works as a very advanced autocomplete tool. Some examples of applications that the GP-T3 model can be used for include:

  • Generating unique content about any topic
  • Paraphrasing or summarizing an article
  • Extracting important points from a document
  • Translating a text to other languages
  • Turning documents into bullet points
  • Classification, categorization, and sentiment analysis of any text

How to use the OpenAI GPT-3 API?

You can use HTTP requests to interact with the GPT-3 APIs from any language. In this article we look at the Python and Node.js examples.

Get API Keys for GPT-3 API Authentication

The OpenAI API uses API keys for authentication. You can get the API key from the OpenAI website after you signup. To access your API key go to the ‘View API keys’ and copy the Secret key. If you don’t see any secret key then you need to create one by simply pressing the ‘Create new secret key’. This is a secret key and must not be shared with anyone. You can renew your secret key anytime you want.

How to get an OpenAI GPT3 API Key
How to get an OpenAI GPT3 API Key

Install OpenAI Library Bindings

Firstly, you need to install the OpenAI library bindings, which can be done as follows for Python and Node.js respectively:

pip install openai

or

npm install openai 

Code to Send a Request to GPT-3 API (Python and Node.js)

Here is the Python code to make a request to the GPT-3 API:

import openai

def ask_gpt3(prompt):
    openai.api_key = 'your-api-key'
    response = openai.Completion.create(
        engine="davinci-instruct-beta",
        prompt=prompt,
        temperature=0.1,
        max_tokens=1000,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    return response.choices[0].text

You need to replace the ‘your-api-key’ text with your GPT3 API key. Once you replace the secret key, you need to call the above function passing the prompt as a parameter, which can be a question or instruction you ask the GPT3 AI. In this case, I ask the question ‘what is artificial intelligence?’ and I print the response.

question = 'What is artificial intelligence?'
answer = ask_gpt3(question)
content = answer.split('\n\n')
print(content)

Here is the response to the GPT3 API call for the above question. As you can see from the result, it returns a list of choices and looking at the first choice, the text includes a list of questions. The AI provided a short answer for the original question, and then it continued by providing more questions and answers.

Sample response of the GPT3 API call
Sample response of the GPT3 API call

We can tweak the parameters to come up with longer answers or more specific answers.

The Node.js version of the code is as follows:

const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
  model: "text-davinci-002",
  prompt: "What is artificial intelligence?",
  max_tokens: 1000,
  temperature: 0,
  top_p: 1,
  frequency_penalty: 0,
  presence_penalty: 0,
});

What are the Main GPT3 Models?

There are four base models of the GPT3, which have different use cases. These base models are as follows:

  • Ada: is the fastest model and the less costly of all the models. To get a better result from Ada model more context needs to be provided to it in the prompts. Some examples of what the Ada model is good for is simple classification and text parsing.
  • Babbage: is good at performing simple to moderate classification, and also for Semantic Search ranking to determine how well documents (e.g. webpages) match up with search queries.
  • Curie: is a powerful model and is highly capable of analyzing text, answering direct questions, and providing key points. It can be used for summarization, sentiment classification, answering questions, and chatbots.
  • Davinci: is the most powerful model which can do anything that the other 3 models do better. It is more capable of comprehending text and generating responses that are more nuanced and require an understanding of the content such as summarizing for a child, creating creative content, or emulating human speaking patterns. The drawbacks of the Davinci model are that it is slower than the other models and is more costly.

This tool allows you to run different models side-by-side to compare outputs, settings, and response times.

You may find out more about the GPT3 models here. The above GPT3 base models can be customized for specific use-cases through the uses with fine-tuning, which involves training on many more examples to more out of the models available.

Pricing of the GPT3 Model

When you initially signup for an OpenAI GPT3 account, you get $18 worth of credit for free API usage during the first three months.

The pricing model is a pay-as-you-go per 1,000 tokens. The tokens are pieces of words. One token is about 4 characters in English text and 1000 tokens are roughly equivalent to 750 words. The below table shows the pricing of 1000 tokens for the 4 base models of GPT3.

Pricing of GPT3 per 1000 tokens for each base model
Pricing of GPT3 per 1000 tokens for each base model

More Examples of GPT-3 Sample Prompt to Use for GPT-3

The following are some examples of what tasks you could use GPT-3 for and the queries with prompts and parameters you may use. You can use the GPT-3 Playground to test these queries.

Summarizing an Article using GPT-3 Curie Model:

The following query which is written in Python uses the Curie model to summarize and lists the key points of an article.

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  model="text-curie-001",
  prompt="What are the key points from this text:\n\n\"\"\"\n This is where the article content goes ...\n\n and then you finish by this:\n\"\"\"\n\nThe key points are:\n\n1.",
  temperature=0.5,
  max_tokens=233,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0,
  stop=["\"\"\""]
)

This is what the content of the query looks like. The “stop” parameter above indicates where your input text finishes, and then the following text prompt instructs the AI to list the key points.

What are the key points from this text:

"""
Put the text here ...
"""

The key points are:

1.

Report Generation

The Curie model or Davinci model can be used to In this example,

{
  "model": "text-curie-001",
  "prompt": "As below",
  "temperature": 0.1,
  "max_tokens": 64,
  "top_p": 1,
  "frequency_penalty": 0.37,
  "presence_penalty": 0,
  "stop": ["\n\n"]
}
Read this customer response then answer the following questions:

"""
Put your text here 
"""

Questions:
1. What was this about?

Answers:
1.

Conclusion

GPT-3 is not a perfect model and could sometimes make grammatical mistakes. However, its mistakes are small and don’t affect the overall meaning of the text. GPT-3 is also not a replacement for human intelligence; it’s a tool that can supplement human intelligence. Overall, GPT-3 is a powerful AI model with the potential to revolutionize many applications in particular in the content creation and marketing domains. In fact, it’s becoming so sophisticated in generating content that it’s hard to distinguish its output from human-written text. If you are interested in knowing how you can detect if an article is written by AI then check out this article.

1 thought on “How to Create an AI Writer or Chatbot Tool using GPT-3”

  1. Pingback: Asked ChatGPT to Write 10 Essays on Great AI TOPICS - Brainyloop - AI Tools & Tech

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.