Create your OpenAI key
We will use OpenAI’s API to programmatically interact with LLMs. To do this, you will need to generate an API key and store it in a secure location.
Create an OpenAI account. I have invited all students to join our OpenAI organization. It is just for students in this class and your accounts will be deactivated at the end of the semester. Look for an email from OpenAI to your Cornell email account inviting you join. Follow the instructions to create and activate your account through OpenAI’s website.
Generate an API key. Once you have an account, log in to OpenAI and navigate to the API keys. Make sure you are viewing the dashboard under our “INFO classes” organization and the “Default project”. If that is not the default setting, use the dropdown menus to adjust this. You will see a button on the right side of the screen to generate an API key. Click the button.
Give your key a name and make sure you have selected the “Default project”. This is the only way to ensure your API usage is charged to the course and not you individually. Click “Create secret key”.
Save your key. You will see a screen with your API key. It is a long string of characters. Copy this key to your clipboard.
From your R console, run
::edit_r_environ() usethis
This will open your
.Renviron
file in RStudio. Add a line to this file that looks like this:="YOUR-OPENAI-TOKEN" OPENAI_API_KEY
Replace
YOUR-OPENAI-TOKEN
with the copied token from OpenAI. Save the file and restart your R session. You should now be able to access your API key in R by runningSys.getenv("OPENAI_API_KEY")