Programming with LLMs

Programming with LLMs for Data Practitioners

Julia Silge | Posit PBC

Hello!

@juliasilge

@juliasilge.com

juliasilge.com

Day 1: Build on AI

  1. Talking with LLMs via code
  2. Programming with LLMs (this session!)
  3. Prompt engineering and RAG
  4. Beyond prompts

Build your own Shiny chat app

  • Shiny chat_ui() and chat_append()
  • Shiny for R also has chat_mod_server()

Your turn

Activity

  • Open 04-word-game-reverse
  • Flip the word game from the last session so:
    • the LLM holds a secret word and gives hints
    • the user has to guess
10:00

Choosing a model

The landscape

  • Major providers: OpenAI, Anthropic, Google, plus local options like ollama
  • What are the tradeoffs?
    • Capability, context length, speed, cost
  • Newer is not always better for your task

Demo

The same prompt across several providers: compare answers, latency, and token usage.

# demo/02-models/02-models.qmd
chat("anthropic/claude-sonnet-4-6")$chat(prompt)
chat("openai/gpt-5")$chat(prompt)

Multi-modal input

Beyond text

  • Vision 👀
    • Pass an image alongside your prompt
  • PDFs 📄
    • Pass document bytes directly
  • When to extract text up front vs. let the model see the file?

Your turn

Activity

Try multi-modal input:

  • Open 05-vision and pass food photos to ask for recipe suggestions
  • Open 06-pdf and take a PDF recipe and turn it into clean Markdown
10:00

Structured output

Getting parseable answers

  • R: ellmer::type_*()
  • Python: Pydantic models with chatlas
  • Schemas double as documentation for the model

Your turn

Activity

  • Open 07-structured-output and extract structured fields from the recipe PDF:
    • Ingredients, steps, yield, prep time
  • For Python, turn on use_attribute_docstrings in Pydantic to let docstrings guide the model
15:00

Parallel and batch calls

Scaling out

  • When one prompt becomes a hundred
  • Parallel API calls vs. provider batch APIs (cheaper, slower)
  • Watch for rate limits!

Your turn

Activity

  • Open 08-batch and re-run the structured extraction across many recipes in parallel
  • Save the JSON output so we can reuse it later
10:00

Thanks!

@juliasilge

@juliasilge.com

juliasilge.com