AE 19: Introduction to LLMs

Application exercise
Modified

November 11, 2025

01-hello-llm

# In this course, we'll be using the ellmer package to interact with Large
# Language Models (LLMs) like OpenAI's GPT and Anthropic's Claude.
# https://ellmer.tidyverse.org/
library(ellmer)

# The project will automatically load your API keys from the .Renviron file.

# ---- OpenAI ----
chat_gpt <- chat_openai()
chat_gpt$chat(
  "I'm in this class to learn about programming with LLMs and ellmer!",
  "Write a brief inspirational message for me."
)

# ---- Anthropic ----
chat_claude <- chat_anthropic()
chat_claude$chat(
  "I'm in this class to learn about programming with LLMs and ellmer!",
  "Write a short poem to celebrate."
)

02-word-game

library(ellmer)

# Let's play a word game!
# Set up a `chat` with a system prompt:
#
# > You are playing a word guessing game. At each turn, guess the word and tell
#   us what it is.
chat <- chat______(
  ____ = ""
)

# Ask the first question:
____("In British English, guess the word for the person who lives next door.")

# Ask the second question:
_____("What helps a car move smoothly down the road?")

# Create a new, empty chat and ask the second question again.
#
# How do the answers to 3 and 4 differ? Why?
chat2 <- chat_____()
chat2$____

05-live

library(ellmer)

# Your job: work with a chatbot to roast of students at Cornell University.
chat <- chat_openai()

# Converse with the chatbot in your console.
live_console(chat)

# After a bit, exit the chat and try chatting in a Shiny app.
live_browser(chat)

06_word-games

library(shiny)
library(bslib)
library(ellmer)
library(shinychat)

system_prompt <- r"--(
We are playing a word guessing game. You are going to think of a random word.
When you do, write it in an HTML comment so that you can remember it, but the
user can't see it.

Give the user an initial clue and then only answer their questions with yes or
no. When they win, use lots of emojis.
)--"


ui <- page_fillable(
  # Step 1: Add the chat module UI to the app UI
)

server <- function(input, output, session) {
  # Step 3: Create the chat client with the system prompt
  client <- ____
  # Step 4: Connect the chat module server to the chat client
  ____("chat", client)
}

shinyApp(ui, server)

Acknowledgments