library(mini007)
openai_4_1 <- ellmer::chat(
name = "openai/gpt-4.1",
credentials = function() {Sys.getenv("OPENAI_API_KEY")},
echo = "none"
)
weather_agent <- Agent$new(
name = "weather_assistant",
instruction = "You are a weather assistant.",
llm_object = openai_4_1
)
weather_function_algiers <- function() {
msg <- glue::glue(
"35 degrees Celcius, it's sunny and there's no precipitation."
)
msg
}
get_weather_in_algiers <- ellmer::tool(
fun = weather_function_algiers,
name = "get_weather_in_algiers",
description = "Provide the current weather in Algiers, Algeria."
)
weather_function_berlin <- function() {
msg <- glue::glue(
"10 degrees Celcius, it's cold"
)
msg
}
get_weather_in_berlin <- ellmer::tool(
fun = weather_function_berlin,
name = "get_weather_in_berlin",
description = "Provide the current weather in Berlin, Germany"
)
weather_agent$register_tools(
tools = list(
get_weather_in_algiers,
get_weather_in_berlin
)
)Tools
You can easily register one or several tools using the register_tools method. The tools are created using ellmer, consider the following example:
One can list the available tools:
weather_agent$list_tools()[1] "get_weather_in_algiers" "get_weather_in_berlin"
After registering the tools, the Agent will use them when needed:
weather_agent$invoke("How's the weather in Algiers?")The weather in Algiers is sunny with a temperature of 35°C and no
precipitation.
weather_agent$invoke("How's the weather in Berlin?")The weather in Berlin is cold with a temperature of 10°C.
One can remove one or several tool using the remove_tools method or remove all agents at one using the clear_tools method:
weather_agent$clear_tools()
weather_agent$list_tools()character(0)
Tool Generation
The generate_and_register_tool method allows you to create tools from simple natural language descriptions (for example, “create a tool that saves files to disk”) and automatically generates the complete R code needed to implement them. It produces a fully functional R function that encapsulates the tool’s logic, along with a complete ellmer tool definition that includes proper type specifications and clear parameter descriptions.
weather_agent$generate_and_register_tool(
description = "create a tool that uses httr to call the open-meteo api https://open-meteo.com/en/docs to get the current weather about any city in the world"
)weather_agent$invoke(
prompt = "what is the current weather in Tokyo?"
)The current weather in Tokyo is 5.9°C with a wind speed of 5.5 km/h coming from
337°. The weather code indicates it's mainly clear.