AE 16: Introduction to LLMs

Application exercise
R
Python
Modified

October 27, 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."
)
# %% [markdown]
# In this course, we'll be using
# [chatlas](https://posit-dev.github.io/chatlas) to interact with large language
# models (LLMs) like OpenAI's GPT and Anthropic's Claude.

# %%
import chatlas

# %% [markdown]
# To load the API keys for these services, we'll use the the `dotenv` package to
# load them from the `.env` file in the root of this project.
# %%
import dotenv

dotenv.load_dotenv()

# %% [markdown]
# ## OpenAI

# %%
chat_gpt = chatlas.ChatOpenAI()
chat_gpt.chat(
    "I'm in this class to learn about programming with LLMs and chatlas!"
    "Write a brief inspirational message for me."
)


# %% [markdown]
# ## Anthropic

# %%
chat_claude = chatlas.ChatAnthropic()
chat_claude.chat(
    "I'm in this class to learn about programming with LLMs and chatlas!"
    "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$____
# %%
import chatlas
import dotenv

dotenv.load_dotenv()

# %% [markdown]
# ## Ley'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 = chatlas.____(____="")

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

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

# %% [markdown]
# Crate a new, empty chat and ask the second question again, by itself.
#
# How do the responses differ? Why?

# %%
chat2 = chatlas.____()
chat2.____("What helps a car move smoothly down the road?")

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)
# %%
import chatlas
import dotenv

dotenv.load_dotenv()


# %% [markdown]
# Your job: work with a chatbot to roast of students at Cornell University.

# %%
chat = chatlas.ChatOpenAI()

# %% [markdown]
# Converse with the chatbot in your console.

# %%
chat.console()

# %% [markdown]
# After a bit, exit the chat and try chatting in a Shiny app.

# %%
chat.app()

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)
import chatlas
import dotenv
from shiny import App, ui

dotenv.load_dotenv()

system_prompt = """
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.
"""

app_ui = ui.page_fillable(
    # Step 1: Add the chat UI component
)


def server(input, output, session):
    # Step 2: Initialize the chat client with the system prompt
    client = ____
    # Step 3: Add the chat UI server setup
    chat = ____

    @chat.on_user_submit
    async def _(user_input: str):
        # Step 4: Submit the user input to the client to get a streaming response
        response = await client.____
        # Step 5: Append the streaming response to the chat UI
        await chat.____(response)


app = App(app_ui, server)

Acknowledgments