AE 16: Introduction to LLMs

Suggested answers

Application exercise
Answers
R
Python
Modified

October 27, 2025

01-hello-llm

library(ellmer)

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

dotenv.load_dotenv()

# ---- 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."
)


# ---- 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)

chat <- chat_openai(
  system_prompt = paste(
    "We are playing a word guessing game.",
    "At each turn, you guess the word and tell us what it is."
  )
)

chat$chat(
  "In British English, guess the word for a person who lives next door."
)

chat$chat("What helps a car move smoothly down the road?")


# Compare with...
chat <- chat_openai()
chat$chat("What helps a car move smoothly down the road?")
import chatlas
import dotenv

dotenv.load_dotenv()

chat = chatlas.ChatOpenAI(
    system_prompt="We are playing a word guessing game. "
    "At each turn, you guess the word and tell us what it is."
)
chat.chat("In British English, guess the word for a person who lives next door.")
chat.chat("What helps a car move smoothly down the road?")

# Compare with...
chat = chatlas.ChatOpenAI()
chat.chat("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(
  chat_mod_ui("chat", placeholder = r"(Say "Let's play" to get started!)")
)

server <- function(input, output, session) {
  client <- chat_openai(system_prompt = system_prompt)
  chat_mod_server("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(
    ui.chat_ui("chat", placeholder="""Say "Let's play" to get started!""")
)


def server(input, output, session):
    client = chatlas.ChatOpenAI(system_prompt=system_prompt)
    chat = ui.Chat("chat")

    @chat.on_user_submit
    async def _(user_input: str):
        response = await client.stream_async(user_input)
        await chat.append_message_stream(response)


app = App(app_ui, server)

Acknowledgments