How to Use AI-Text Generation with ChatGPT in R — part 16 of “R for Applied Economics” guide

Dima Diachkov
Dev Genius
Published in
6 min readMar 18, 2023

--

AI is currently one of the most hyped topics. While some people are talking about this buzzword, others are already using AI to create designs and art with MidJourney and StableDiffusion, generate analytic reports, or even study foreign languages with chatGPT and generate audio with Resemble AI. Every day, there are new articles on Medium about cool AI tools for various tasks. I will not tell you about the potential of AI tools, as it was played to death already (and there are many better experts than I am). Hence, I will focus on the practical implementation of AI into your (and our) real-life projects in R. If we want to analyze economic data massively — we need AI. There is too much information nowadays, big data, and long time series, so you are getting more and more helpless on your own, but more powerful with AI.

Plan for today

So far, we have made a lot of web-parsing, some charting, and created a simple database and RMarkdown report. These skills are the main (most basic), that you need to acquire. So today I would offer you to start our new short journey to invigorate our data with automatic text comments, generated via AI. Today we try to access chatGPT, which you are probably also familiar with.
But if you live under a rock: ChatGPT is a language model created by OpenAI that can generate human-like text. It works by training on a massive amount of text data and using a deep neural network to generate text based on the input text. ChatGPT is capable of completing text prompts, answering questions, and generating new text in the style of the input text. Currently, it works on a freemium model, where basic functions are available with some limitations.

To use API, there is a limit, after which you have to pay. At this moment, when you create an account you get 18$ grant, which you can use. You can check it out on your personal API settings page.

Free grant for account creation

Trust me, that is more than enough to study, experiment, and even play with it for a couple of months, as usage is measured with tokens (let’s conditionally say — text size measurement units). If you want to implement some services with chatGPT embedded, you have to register as an organization and check out pricing plans, which are also manageable.

Pricing as of 18.03.2023

We will use the basic version with chatGPT 3.5 to start with. So let’s create our first API key (if you have it already — please skip this step or you can create another key for R) to open your book of AI which you can use to support your coding journey.

Sacred Knowledge | Generated by the Lord Of The R via MidJourney AI

Creating an OpenAI API Key

To use ChatGPT in R, you’ll need to create an OpenAI API key. You can do this by visiting the OpenAI website and signing up for an account. Once you’ve signed up, you can create an API key from your account dashboard. You’ll need to provide some basic information and agree to the OpenAI terms of service.

So, when you do that, please log-in via OpenAI API page https://platform.openai.com/signup and then go to API keys areas to create a new secret key.

When you press it, you get your API key, which you have to store diligently. It will look like this, with numbers and letters you can not remember.

API key created

Do not share it with anyone as it can be used to send/request data using your account data and hence — you will be billed.

It’s a private key meant only for access to your account. You can also delete API keys and create multiple private keys (up to five). So if you want to show someone how easy and useful chatGPT is, please create a separate API key and then delete it after the demonstration. So this time I will do exactly that and delete this key later.

How can you store it? That is whole another topic, which depends on your computer science experience. For now, I would suggest using a text file with keys. This approach is common as you can share your code with the reference to an API key file through GitHub or elsewhere, and you do not need to think of deleting the API key before exporting your code to the hub. More experienced users would like to create a pickle file or an environment variable in your OS. We can explore it too, just let me know. For now — copy your API into txt-file and save it in the working directory. Once you’ve created your API key, you can use it to access the OpenAI API through R.

Accessing the API Through R

To access the OpenAI API through R, you can use the curl or httr packages. You'll need to provide your API key in the API request headers, along with the input text you want to generate text from. Once you've sent the request, the API will return the generated text, which you can then use in your R code. This is what it looks like if we implement it as a user-defined function (check part 4 for a recap):

library(httr) # for curl requests
library(stringr) # to manage strings

# please put your link to the file with API key - otherwise it won't work
api_key <- readLines("OpenAI_API_key.txt")

# Function to ask chatGPT with our prompt
ask_chatgpt <- function(prompt) {

response <- POST(
# URL address we target
url = "https://api.openai.com/v1/chat/completions",
add_headers(Authorization = paste("Bearer", api_key)),
content_type_json(),
# we wrap the conversation into json format
encode = "json",
body = list(
model = "gpt-3.5-turbo", # model name
messages = list(list(
role = "user",
content = prompt # here our prompt plays its role
))
)
)
cat(str_trim(content(response)$choices[[1]]$message$content))
}

ask_chatgpt("Does it work?")

And here comes the feedback:

AI’s feedback on our test message

So it works as expected and returns the response. You can play with it a little bit, just to check that it is working. However, there is at least one thing that needs improvement — CONVERSATION MEMORY. Check this out.

The limitation that we are going to fix in the next classes

It needs to be improved. For now, the code is deposited in my GitHub, as usual: https://github.com/TheLordOfTheR/R_for_Applied_Economics/blob/main/Part16.R

Wrap-up

Basically, we made our first step. We just opened an AI book for you and later we will enhance your knowledge of it. Know we are able to get a one-time response, that does not have any context. Some people use it like this, however, this is not very right since it is not CHAT-chat, it is just an exchange of request/response queries like we do in Google or any other search system.
So please stay tuned if you want to adapt this function we made for trickier applications and find out how it can be used in data explanatory analysis and code development.

Please clap 👏 and subscribe if you want to support me. Thanks!❤️‍🔥

--

--

Balancing passion with reason. In pursuit of better decision making in economic analysis and finance with data science via R+Python