AE 18: Prompt engineering

Suggested answers

Application exercise
Answers
R
Python
Modified

November 3, 2025

12_plot-image-1

library(readr)
library(ellmer)
library(ggplot2)

# Step 1: Make a scatter plot of the penguins dataset
data("penguins")

ggplot(data = penguins, aes(x = flipper_len, y = bill_len)) +
  geom_point(color = "steelblue", size = 3, alpha = 0.8) +
  theme_minimal() +
  labs(
    title = "Flipper and bill length",
    subtitle = "Dimensions for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER",
    x = "Flipper length (mm)",
    y = "Bill length (mm)"
  )

chat <- chat("anthropic/claude-sonnet-4-20250514", echo = "output")
chat$chat(
  "Interpret this plot.",
  content_image_plot()
)
# %%
import chatlas
import dotenv
from matplotlib import pyplot as plt
from palmerpenguins import load_penguins
from plotnine import (
    aes,
    geom_point,
    ggplot,
    labs,
    theme_minimal,
)

dotenv.load_dotenv()

# %%
# Step 1: Create a scatter plot of flipper length vs bill length for penguins
penguins = load_penguins()
penguins

# %%
p = (
    ggplot(penguins, aes(x="flipper_length_mm", y="bill_length_mm"))
    + geom_point(color="steelblue", size=3, alpha=0.8)
    + theme_minimal()
    + labs(
        title="Flipper and bill length",
        subtitle="Dimensions for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER",
        x="Flipper length (mm)",
        y="Bill length (mm)",
    )
)

p.show()

# %%
# Register the plot with matplotlib's current figure
plt.figure(p.draw())

chat = chatlas.ChatAuto("anthropic/claude-sonnet-4-20250514")
chat.chat(
    "Interpret this plot.",
    chatlas.content_image_plot(),
)

13_plot-image-2

library(readr)
library(ellmer)
library(ggplot2)

m <- 32
u <- (seq_len(floor(sqrt(m))) - 0.5) / floor(sqrt(m))
grid <- as.matrix(expand.grid(x = u, y = u))

eps <- 1 / (2 * sqrt(m))
jitter <- matrix(runif(length(grid), -eps, eps), ncol = 2)
grid_jitter <- pmin(pmax(grid + jitter, 0), 1)

ggplot() +
  aes(x = grid_jitter[, 1], y = grid_jitter[, 2]) +
  geom_point(color = "steelblue", size = 3, alpha = 0.8) +
  theme_minimal() +
  labs(
    title = "Flipper and bill length",
    subtitle = "Dimensions for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER",
    x = "Flipper length (mm)",
    y = "Bill length (mm)"
  )

chat <- chat("anthropic/claude-sonnet-4-20250514", echo = "output")
chat$chat(
  "Interpret this plot of penguins.",
  content_image_plot()
)
# %%
from math import floor, sqrt

import chatlas
import dotenv
import numpy as np
from matplotlib import pyplot as plt
from plotnine import aes, geom_point, ggplot, labs, theme_minimal

dotenv.load_dotenv()

# %%
m = 32

g = floor(sqrt(m))
u = (np.arange(1, g + 1) - 0.5) / g
xx, yy = np.meshgrid(u, u)
grid = np.column_stack([xx.ravel(), yy.ravel()])

# small jitter to avoid perfect lattice, scaled to cell size
eps = 1.0 / (2.0 * sqrt(m))
jitter = np.random.uniform(-eps, eps, size=grid.shape)
grid_jitter = np.clip(grid + jitter, 0.0, 1.0)

x = grid_jitter[:, 0]
y = grid_jitter[:, 1]

p = (
    ggplot(aes(x=x, y=y))
    + geom_point(color="steelblue", size=3, alpha=0.8)
    + theme_minimal()
    + labs(
        title="Flipper and bill length",
        subtitle="Dimensions for Adelie, Chinstrap and Gentoo Penguins at Palmer Station LTER",
        x="Flipper length (mm)",
        y="Bill length (mm)",
    )
)

p.show()

# %%
# Register the plot with matplotlib's current figure
plt.figure(p.draw())

chat = chatlas.ChatAuto("anthropic/claude-sonnet-4-20250514")
chat.chat(
    "Interpret this plot of penguins.",
    chatlas.content_image_plot(),
)

14_quiz-game-1

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

# UI ---------------------------------------------------------------------------

ui <- page_fillable(
  chat_mod_ui("chat")
)

# Server -----------------------------------------------------------------------

server <- function(input, output, session) {
  client <- chat(
    "anthropic/claude-3-7-sonnet-20250219",
    system_prompt = interpolate_file(
      here::here("ae/ae-18/_solutions/14_quiz-game-1/prompt.md")
    )
  )

  chat <- chat_mod_server("chat", client)

  observe({
    # Start the game when the app launches
    chat$update_user_input(
      value = "Let's play the quiz game!",
      submit = TRUE
    )
  })
}

shinyApp(ui, server)
import chatlas
import dotenv
from pyhere import here
from shiny import App, reactive, ui

dotenv.load_dotenv()


# UI ---------------------------------------------------------------------------

app_ui = ui.page_fillable(
    ui.chat_ui("chat"),
)


def server(input, output, session):
    chat_ui = ui.Chat(id="chat")

    # Set up the chat instance
    client = chatlas.ChatAnthropic(
        model="claude-3-7-sonnet-20250219",
        system_prompt=here("_solutions/14_quiz-game-1/prompt.md").read_text(),
    )

    @chat_ui.on_user_submit
    async def handle_user_input(user_input: str):
        # Use `content="all"` to include tool calls in the response stream
        response = await client.stream_async(user_input, content="all")
        await chat_ui.append_message_stream(response)

    @reactive.effect
    def _():
        # Start the game when the app launches
        chat_ui.update_user_input(value="Let's play the quiz game!", submit=True)


app = App(app_ui, server)

prompt.md

## Quiz Game Host

-  Greet the user and ask them to choose a theme. Suggest a list of 5 random themes.
-  Ask simple multiple-choice questions (A–D), one at a time. Show the current question number.
-  After each answer: confirm choice, say correct/incorrect, give a brief explanation, then ask the next question.
-  Every 5 questions: declare the user a winner (regardless of score), praise them, then restart by asking for a theme.
-  Validate input: if not A–D, ask for a valid option and repeat the question.
-  Allow skipping: if asked to skip, reveal the answer briefly, then move on.
-  Keep tone upbeat, concise, and emoji-rich. Avoid repeating questions in a session. Gradually increase difficulty.

## Examples

Assistant:
    🎉 Welcome to the Quiz Game Show! Pick a theme to begin:
    - Science
    - History
    - Movies
    - Sports
    Which theme would you like?

User:
    Science

Assistant:
    ✅ Great choice: Science!
    **Question 1:** What is the center of an atom called?

    A) Electron \
    B) Proton \
    C) Neutron \
    D) Nucleus

User:
    D

Assistant:
    Correct! ✅ The nucleus is the dense center containing protons and neutrons.
    **Question 2:** Which planet is known as the Red Planet?

    A) Venus \
    B) Mars \
    C) Jupiter \
    D) Mercury

User:
    E

Assistant:
    Oops! ❗ Please answer with A, B, C, or D.
    **Question 2:** Which planet is known as the Red Planet?

    A) Venus \
    B) Mars \
    C) Jupiter \
    D) Mercury

User:
    skip

Assistant:
    Skipped! ⏭️ The correct answer is B) Mars.
    **Question 3:** …

Acknowledgments