Code
Sys.setenv(ANTHROPIC_API_KEY = rstudioapi::askForSecret("Anthropic API Key"))Many of the interactive portions of this workshop depend on successfully being able to connect to a generative AI model via API (we will discuss API keys in more depth later). For this workshop you’ll need to obtain your own Anthropic API key through console.anthropic.com.
$5 in credit should be sufficient for all workshop purposes, and you’ll have enough left over to continue practicing sending LLM calls via API.
The following is a basic function for testing your connection to an Anthropic Claude model. We’ll cover some of its elements later. To use it, load your API key into the R session with the syntax below:
Sys.setenv(ANTHROPIC_API_KEY = rstudioapi::askForSecret("Anthropic API Key"))When using the API key in your workflow, you can skip this step by creating an .Renviron file that is saved in your working directory. Each API key should have a line like: ANTHROPIC_API_KEY='paste_your_api_key_here'.
call_claude functionYou can download the following function by clicking the copy button (looks like a clipboard) in the code chunk displayed below. Alternatively, you can click the icon below:
📥 Download call_claude_AIMECON26
source("downloads/call_claude_AIMECON26.R")The only argument that needs to be supplied to the function is the prompt that you want to send to the model; all other arguments have default values, and we’ll discuss the other options in more detail in subsequent sections. The prompt should be a text string that is enclosed by parentheses: e.g., “Tell me a joke about educational measurement.”
test_joke <- call_claude("Tell me a joke about educational measurement.")
test_joke[1] "Why did the standardized test blush?\n\nBecause it saw the students' scores and realized it had been *mean* the whole time... with a side of *standard deviation*."
A few things to note:
Generally speaking, the process used by a generative AI model involves predicting the next token to be selected and sampling from a distribution of possible options. (We will discuss ways to better control this later.) A (sometimes, mostly) beautiful implication of this is that responses from generative AI models will often be different, even if the exact same prompt is used. This is especially the case in the simple function above, as we haven’t made an effort to tune the model generation parameters to achieve a deterministic or replicable result. Thus, when you call Anthropic and have it generate a joke for you using the same prompt, you will likely get different responses each time. In fact, each time that I compile this book on GitHub the result changes!
You may notice that escape sequences (\n, \t, etc.) may be present because the is returned from the model. One way to handle this is to wrap your response in cat(). We’ll revisit this again later in other sections.
cat(test_joke)Why did the standardized test blush?
Because it saw the students' scores and realized it had been *mean* the whole time... with a side of *standard deviation*.