Allows LLMs to interact with other systems
Supported by most of the newest LLMs, but not all
Sounds complicated? Scary? It’s not too bad, actually…
Reference: https://jcheng5.github.io/llm-quickstart/quickstart.html#/how-it-works
library(httr)
library(ellmer)
library(dotenv)
# Load environment variables
load_dot_env()
# Define weather function
get_weather <- function(latitude, longitude) {
base_url <- "https://api.open-meteo.com/v1/forecast"
tryCatch(
{
response <- GET(
base_url,
query = list(
latitude = latitude,
longitude = longitude,
current = "temperature_2m,wind_speed_10m,relative_humidity_2m"
)
)
rawToChar(response$content)
},
error = function(e) {
paste("Error fetching weather data:", e$message)
}
)
}
# Create chat instance
chat <- chat_openai(
model = "gpt-4.1",
system_prompt = "You are a helpful assistant that can check the weather. Report results in imperial units."
)
# Register the weather tool
#
# Created using `ellmer::create_tool_def(get_weather)`
chat$register_tool(tool(
get_weather,
"Fetches weather information for a specified location given by latitude and
longitude.",
latitude = type_number(
"The latitude of the location for which weather information is requested."
),
longitude = type_number(
"The longitude of the location for which weather information is requested."
)
))
# Test the chat
chat$chat("What is the weather in Seattle?")
import requests
from chatlas import ChatAnthropic
from dotenv import load_dotenv
load_dotenv() # Loads OPENAI_API_KEY from the .env file
# Define a simple tool for getting the current weather
def get_weather(latitude: float, longitude: float):
"""
Get the current weather for a location using latitude and longitude.
"""
base_url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": latitude,
"longitude": longitude,
"current": "temperature_2m,wind_speed_10m,relative_humidity_2m",
}
try:
response = requests.get(base_url, params=params)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.RequestException as e:
return f"Error fetching weather data: {str(e)}"
chat = ChatAnthropic(
model="claude-3-5-sonnet-latest",
system_prompt=(
"You are a helpful assistant that can check the weather. "
"Report results in imperial units."
),
)
chat.register_tool(get_weather)
chat.chat("What is the weather in Seattle?")
MDS AI Mini-Workshop. 2025. https://github.com/chendaniely/2025-06-05-llm