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

1
devtools::install_github("samterfa/openai")
2
library(openai)
3
library(purrr)
Copied!

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:
1
Sys.setenv(openai_organization_id = "XXXXX")
2
Sys.setenv(openai_secret_key = "XXXXXX")
Copied!
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.
1
course_gpt3 <- function(val, temp){
2
3
initPrompt <- "Classify each dish by its country of origin"
4
kw1 <- "Creme Brulée"
5
Cl1 <- "France"
6
7
kw2 <- "Schwarzwälder"
8
Cl2 <- "Germany"
9
10
kw3 <- "Couscous"
11
Cl3 <- "Morocco"
12
13
kw4 <- "Paella"
14
Cl4 <- "Spain"
15
16
kw5 <- "Fish and Chips"
17
Cl5 <- "England"
18
19
20
catPrompt = paste0("prompt : ", initPrompt,
21
"\ndish: " , kw1 , ", country :" , Cl1 ,
22
"\ndish: " , kw2 , ", country :" , Cl2 ,
23
"\ndish: " , kw3 , ", country :" , Cl3 ,
24
"\ndish: " , kw4 , ", country :" , Cl4 ,
25
"\ndish: " , kw5 , ", country :" , Cl5 ,
26
"\n\ndish: ",val,", country :")
27
28
result <- create_completion(
29
engine_id = 'davinci',
30
max_tokens = 5,
31
temperature = temp,
32
top_p = 1,
33
n = 1,
34
stream = F,
35
prompt = catPrompt) %>%
36
pluck('choices') %>%
37
map_chr(~ .x$text)
38
39
strsplit(result,"\n")[[1]][1]
40
41
}
Copied!
And after that you just need to run it.
1
course_gpt3("matcha tea", 0.2)
2
course_gpt3("pizza", 0.2)
3
course_gpt3("nyc pizza", 0.2)
4
course_gpt3("sausage", 0.2)
Copied!
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!