# Classify SEO Keywords using GPT-3 & R'

Thanks to [OpenAI](https://openai.com/) 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](https://beta.openai.com/playground) but it's also possible to use the API for SEO work.

![](/files/oEsO951IIffWuQIcDb3t)

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

```r
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>

![](/files/7Y4RHBl3iKxi0Rm1bmIK)

and replace the value in the code below:

```r
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.

```r
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.

```r
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!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.rforseo.com/apis/classify-seo-keywords-using-gpt-3-and-r.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
