Classify SEO Keywords using GPT-3 & R'

Thanks to OpenAI it's now possible to use artificial intelligence to help your SEO ✨

They have made available an API called GPT-3 that allows us to get a reply from their Generative Pre-trained Transformer (whatever it means)

It's possible to ask silly questions in GPT-3 playground but it's also possible to use the API for SEO work.

For this example, let's imagine we have a recipe website classicly structured by starters, main courses and desserts. For users and SEO purposes we want to also classify each dish per country of origin. The goal is to build landing pages like "Top traditional german recipes", etc

We will ask GPT3 to go through our full catalogue of recipes

Install and load the OpenAI R Package

devtools::install_github("samterfa/openai")
library(openai)
library(purrr)

API Keys

Grab your API key from here: https://beta.openai.com/account/api-keys

and your organisation key from https://beta.openai.com/account/org-settings

and replace the value in the code below:

Sys.setenv(openai_organization_id = "XXXXX")
Sys.setenv(openai_secret_key = "XXXXXX")

I've made a small function to make it easier but to explain simply:

  • We provide a prompt and some examples and ask the AI to complete the text

  • The temperature, it's a number between 0 and 2 and some say it's the randomness. It may require a bit of tweaking, that's why I left it as a function parameter.

course_gpt3 <- function(val, temp){

initPrompt <- "Classify each dish by its country of origin"
kw1 <- "Creme Brulée"
Cl1 <- "France"

kw2 <- "Schwarzwälder"
Cl2 <- "Germany"

kw3 <- "Couscous"
Cl3 <- "Morocco"

kw4 <- "Paella"
Cl4 <- "Spain"

kw5 <- "Fish and Chips"
Cl5 <- "England"


catPrompt = paste0("prompt : ", initPrompt,
                   "\ndish: " , kw1 , ", country :" , Cl1 ,
                   "\ndish: " , kw2 , ", country :" , Cl2 ,
                   "\ndish: " , kw3 , ", country :" , Cl3 ,
                   "\ndish: " , kw4 , ", country :" , Cl4 ,
                   "\ndish: " , kw5 , ", country :" , Cl5 ,
                   "\n\ndish: ",val,", country :")

result <- create_completion(
  engine_id = 'davinci', 
  max_tokens = 5,
  temperature = temp,
  top_p = 1,
  n = 1,
  stream = F, 
  prompt = catPrompt) %>% 
  pluck('choices') %>% 
  map_chr(~ .x$text)

strsplit(result,"\n")[[1]][1]

}

And after that you just need to run it.

course_gpt3("matcha tea", 0.2)
course_gpt3("pizza", 0.2)
course_gpt3("nyc pizza", 0.2)
course_gpt3("sausage", 0.2)

Now that you have this example, it's up to you to update the prompt and the examples to fit your use case. Good luck!

Last updated