Skip to main content

AE 17: Introduction to LLMs

Application exercise
Modified

November 3, 2026

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_claude()
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: Get information on how to use the `tidyr::pivot_wider()` function
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)

Acknowledgments