-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtools.qmd
More file actions
98 lines (80 loc) · 2.45 KB
/
tools.qmd
File metadata and controls
98 lines (80 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
---
title: "Tools"
format:
html:
toc: true
execute:
echo: true
warning: false
error: false
---
You can easily register one or several tools using the `register_tools` method. The tools are created using `ellmer`, consider the following example:
```{r}
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
)
)
```
One can list the available tools:
```{r}
weather_agent$list_tools()
```
After registering the tools, the Agent will use them when needed:
```{r}
weather_agent$invoke("How's the weather in Algiers?")
```
```{r}
weather_agent$invoke("How's the weather in Berlin?")
```
One can remove one or several tool using the `remove_tools` method or remove all agents at one using the `clear_tools` method:
```{r}
weather_agent$clear_tools()
weather_agent$list_tools()
```
### 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.
```{r}
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"
)
```
```{r}
weather_agent$invoke(
prompt = "what is the current weather in Tokyo?"
)
```