library(rtemis.llm)6 Tool use
rtemis.llm supports creating agents with access to tools. Tools are functions that the agent can choose to call during the generation process.
For security reasons, the package provides a list of pre-built tools, which are hashed and validated at runtime, to reduce the risk of malicious code execution. The allow_custom_tools argument in create_agent() can be set to TRUE to allow the use of custom tools. The create_tool() function can be used to create custom tools.
6.1 Built-in tools
Print list of built-in tools:
available_tools()Create two agents, one without and with access to the datetime tool:
agent_zero <- create_agent(
config_Ollama(
model_name = "gemma4:e4b",
temperature = 0.2
),
system_prompt = "You are a helpful assistant.",
name = "AgentZero"
)agent_current <- create_agent(
config_Ollama(
model_name = "gemma4:e4b",
temperature = 0.2
),
system_prompt = "You are a helpful assistant.",
name = "AgentCurrent",
tools = list(tool_datetime)
)Generate a response from both agents to the same prompt:
res_zero <- generate(agent_zero, "What is the current date and time?")res_zero$system
[AgentZero System] You are a helpful assistant.
$user
[Input] What is the current date and time?
$assistant
[AgentZero Reasoning] Thinking Process:
1. **Identify the core request:** The user is asking for the "current date and time."
2. **Analyze capability constraints:** As an AI language model, I do not have real-time access to the physical clock or the user's local time zone. My responses are generated based on the time the model was trained or the time the server processes the request, but this is not reliable or accurate for the user's immediate needs.
3. **Determine the necessary response:** I must inform the user that I cannot provide the *actual* current time because I lack real-time clock access.
4. **Provide a helpful alternative:** To be helpful, I must instruct the user on how *they* can find the accurate time (e.g., checking a reliable clock, device, or search engine).
5. **Formulate the answer:** Be polite, state the limitation clearly, and offer the solution. (Self-correction: Do not guess the time; state the limitation directly.)
[AgentZero Response] I do not have access to a real-time clock, so I cannot tell you the exact current date and time.
To get the accurate time, please check a reliable clock, your computer's system clock, or use a search engine.
attr(,"agent_output")
[1] TRUE
res_current <- generate(agent_current, "What is the current date and time?")res_current$system
[AgentCurrent System] You are a helpful assistant.
$user
[Input] What is the current date and time?
$assistant
[AgentCurrent Reasoning] The user is asking for the current date and time. I have a tool called `get_current_datetime` that can provide this information. I should call this tool.
[AgentCurrent Tool Call]
[
{
"id": "call_487zenfu",
"function": {
"index": 0,
"name": "get_current_datetime",
"arguments": {}
}
}
]
$tool
[get_current_datetime Tool Response]
{"date":"2026-10-04","time":"02:08:34","timezone":"America/Los_Angeles"}
$user
[Agent Message]
Tool 'get_current_datetime' response:
{"date":"2026-10-04","time":"02:08:34","timezone":"America/Los_Angeles"}
Acknowledge the tools used and include citations where applicable. If you do not have sufficient information, you may use up to 2 more tool call rounds, if necessary. Remember the original query you are answering:
"What is the current date and time?"
$assistant
[AgentCurrent Reasoning] The user is asking for the current date and time.
I have already called the `get_current_datetime` tool and received the response: `{"date":"2026-10-04","time":"02:08:34","timezone":"America/Los_Angeles"}`.
I need to present this information clearly to the user, acknowledging the tool use and providing the citation.
Plan:
1. Extract the date, time, and timezone from the tool response.
2. Formulate a natural language answer.
3. Include the citation for the tool used.
[AgentCurrent Response] The current date and time is October 4, 2026, at 02:08:34 in the America/Los_Angeles timezone (Tool: get_current_datetime).
attr(,"agent_output")
[1] TRUE