Using GPT-3 to generate Text Ads & Keywords

Using OpenAI GPT-3 to generate Text Ads and Keywords

OpenAI is a research lab focused on building advanced artificial intelligence in a responsible way. They’re interested in creating AI technologies that can benefit humanity as a whole, and they’re already having a major impact on digital marketing. OpenAI’s algorithms are being used by major companies like Google, Facebook, and Microsoft to help improve their products and services. For example, Google is using OpenAI’s technology to improve the quality of its search results. Facebook is using it to help identify fake news stories. And Microsoft is using it to improve the accuracy of its translations. These are just a few examples of how OpenAI is already making a difference in the world of digital marketing. As their technology continues to improve, it’s likely that we’ll see even more companies using it to improve their products and services.

Digital marketing is becoming increasingly complex as businesses strive to personalize their marketing messages and reach their target audiences more effectively. Artificial intelligence can help digital marketers keep up with the ever-changing landscape of digital marketing and make better decisions about where to allocate their resources.

Actually, the two paragraphs above were written by AI 😄

GPT-3 is a pre-trained ML model that was created by OpenAI, the model is super powerful, it was trained on hundreds of billions of words. The model can create anything that has a language structure, it can generate content, answer questions summarize long texts, translate, and many more. Today, I will show you how to use the model to generate keywords and text for your ads.

Sign up for OpenAI and get your API key

You can sign up from here. After you complete your registration, you will need to visit this page to get your API key that we will use to make all the API requests.

Write the code

You can use your local environment to run the code but for the sake of simplicity of this tutorial let’s use Google Colab to execute all the code. All you need to do is to visit Colab and paste all the code snippets below. Let’s get started!

Install OpenAI library by running the command below.

!pip install openai

Once installed, it’s time now to import the module and set your API key:

import os
import openai

# set your api key
api_key = "<Add your API key here>"

The function below will print and return the OpenAI response. You can control the settings of the model as follows:

temperature: is a parameter used in some reinforcement learning algorithms which determine how likely the agent is to explore new actions or states. A higher temperature (max:1) makes the agent more likely to explore, while a lower temperature (min:0) makes the agent more likely to exploit known good actions or states.

max_tokens: The maximum number of tokens to generate in the completion.

top_p: An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. Default to 1

frequency_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.

presence_penalty: Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.

def run_model(prompt, api_key):
  openai.api_key = api_key

  response = openai.Completion.create(
    engine="text-davinci-002",
    prompt=prompt,
    temperature=0.7,
    max_tokens=150,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
  )
  
  for r in response['choices']:
    print(r['text'])

  return response

Run the model by providing a prompt. you’ll need a prompt that makes it clear what you want. Imagine speaking to human-being and asking what you want. Let’s ask the model to generate keywords for men’s watches:

prompt = 'generate keywords list for men watches'
res = run_model(prompt, api_key)

The results for the code above will be:

men's watches, men's watch, men's wristwatch, men's fashion watches, men's dress watches, men's sports watches, men's waterproof watches, men's automatic watches, men's quartz watches, men's luxury watches, men's vintage watches

Here’s what the full notebook now looks like:

Let’s try another one by asking the model to write us a Facebook Ad about iPhone 13:

prompt = 'write me facebook ad about iphone 13'
res = run_model(prompt, api_key)

Here are the results for the Facebook Ad:

The all-new iPhone 13 is here! With an all-new design, powerful A14 Bionic chip, and amazing new features, this is the best iPhone yet! Get yours today!

There are many examples that the model can be leveraged for, I’d recommend having a look at the examples page on the OpenAI website to explore all the possible scenarios. Please note that OpenAI isn’t free, they operate on a pay-as-you-go basis, meaning that customers are only charged for the resources they use. However, they offer $18 in free credit that can be used during your first 3 months, so happy experimenting. You can read more about the pricing plans here.