Tool Calling

Getting Started with LLM APIs in R

R/Pharma 2025

2025-11-03

Recall: How do LLMs work?

  1. You write some words

  2. The LLM writes some more words

  3. You use those words

On their own, can LLMs… access the internet? run code? send an email? interact with the world?

Let’s try it

library(ellmer)

chat <- chat("openai/gpt-4.1-nano")

chat$chat("Who are the keynote speakers at R/Pharma 2025?")
My training only includes data up until October 2023, and I do not 
have access to real-time updates or event-specific details beyond that. 

For the most accurate and current information about the keynote 
speakers, I recommend visiting the official R/Pharma 2025 
website or checking their latest event announcements.

Let’s try it

library(ellmer)

chat <- chat("openai/gpt-4.1-nano")

chat$chat("What's the weather like in Minneapolis?")
I'm unable to provide real-time weather updates.
For the most current weather in Minneapolis,
please check a reliable weather service or app like
Weather.com, AccuWeather, or your local weather provider.

Let’s try it

library(ellmer)

chat <- chat("openai/gpt-4.1-nano")

chat$chat("What day is it?")
Today is October 27, 2023.

Tools

a.k.a. functions, tool calling or function calling

  • Bring real-time or up-to-date information to the model

  • Let the model interact with the world

How does tool calling work?

The LLM can’t run code by itself!

Demo: weather tool

👩‍💻 _demos/19_tools/19_weather-tool.R

Tools in R

Step 1: Write a (normal) R function

get_weather <- function(zip_code) {
  # use weather API to get weather for that zip
  # ...
}

Step 2: Define the tool

tool_get_weather <- tool(
  tool_fn,
  description = "How and when to use the tool",
  arguments = list(
    .... = type_string(),
    .... = type_integer(),
    .... = type_enum(
      c("choice1", "choice2"),
      required = FALSE
    )
  )
)

Step 2: Define the tool

tool_get_weather <- tool(
  get_weather,
  description = 
    "Gets the current weather for a specified zip code",
  arguments = list(
    zip_code = type_string("The zip code.", required = TRUE)
  )
)

Step 3: Register the tool

chat$register_tool(tool_get_weather)

Your Turn 10_quiz-game-2

  1. I’ve given you a function that plays a sound when called (play_sound()).

  2. Your job: give the model the ability to play sounds in the Quiz Show game we made earlier.

  3. You will need to fill out the tool definition and register the tool.

06:00

Tools in Shiny

  • Define the tool function inside the server

  • You can update reactive values in the tool function!

Learn more

Learn more

querychat: https://github.com/posit-dev/querychat

  • Chat with your data using natural language queries.

ggbot2: https://github.com/tidyverse/ggbot2

  • Talk out loud to create and iterate on plots.

Databot: https://positron.posit.co/databot.html

  • EDA assistant for Positron.

Posit AI newsletter: https://posit.co/blog/?post_tag=ai-newsletter

Thank you!