library(httr)
library(jsonlite)
openai_endpoint <- "https://api.openai.com/v1/engines/davinci-codex/completions"
openai_token <- "your_openai_token_here"
get_openai_text <- function(prompt) {
req <- POST(openai_endpoint,
add_headers("Content-Type" = "application/json",
"Authorization" = paste("Bearer", openai_token)),
body = toJSON(list(prompt = prompt,
max_tokens = 1000,
temperature = 0.7)))
req_text <- content(req, "text")
return(req_text)
}
prompt <- "ChatGPT, can you tell me a bit about yourself?"
response <- get_openai_text(prompt)
cat(response)
## {
## "error": {
## "message": "Incorrect API key provided: your_ope**********here. You can find your API key at https://platform.openai.com/account/api-keys.",
## "type": "invalid_request_error",
## "param": null,
## "code": "invalid_api_key"
## }
## }