You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -72,37 +73,40 @@ To use a tool, attach it to a chat:
72
73
chat =RubyLLM.chat
73
74
74
75
# Add a tool
75
-
chat.with_tool(Calculator)
76
+
chat.with_tool(Weather)
76
77
77
-
# Now you can ask questions that might require calculation
78
-
response = chat.ask "What's 123 * 456?"
79
-
# => "Let me calculate that for you. 123 * 456 = 56088."
78
+
# Now you can ask questions that might require weather data
79
+
response = chat.ask "What's the weather in Berlin? (52.5200, 13.4050)?"
80
+
# => "The current weather in Berlin is as follows:\n- **Temperature:** 4.6°C\n- **Wind Speed:** 6.6 km/h\n\nPlease note that the weather information is up to date as of March 15, 2025, at 20:15 GMT."
80
81
```
81
82
82
83
### Multiple Tools
83
84
84
85
You can provide multiple tools to a single chat:
85
86
86
87
```ruby
87
-
classWeather < RubyLLM::Tool
88
-
description "Gets current weather for a location"
89
-
90
-
param :location,
91
-
desc:"City name or zip code"
92
-
93
-
defexecute(location:)
94
-
# Simulate weather lookup
95
-
"72°F and sunny in #{location}"
96
-
end
88
+
require'tzinfo'
89
+
90
+
classTimeInfo < RubyLLM::Tool
91
+
description 'Gets the current time in various timezones'
92
+
param :timezone,
93
+
desc:"Timezone name (e.g., 'UTC', 'America/New_York')"
94
+
95
+
defexecute(timezone:)
96
+
time =TZInfo::Timezone.get(timezone).now.strftime('%Y-%m-%d %H:%M:%S')
97
+
"Current time in #{timezone}: #{time}"
98
+
rescueStandardError => e
99
+
{ error: e.message }
100
+
end
97
101
end
98
102
99
103
# Add multiple tools
100
104
chat =RubyLLM.chat
101
-
.with_tools(Calculator, Weather)
105
+
.with_tools(Weather, TimeInfo)
102
106
103
107
# Ask questions that might use either tool
104
-
chat.ask "What's the temperature in New York City?"
105
-
chat.ask "If it's 72°F in NYC and 54°F in Boston, what's the average?"
108
+
chat.ask "What's the temperature in Rome?"
109
+
chat.ask "What's the time in Tokyo?"
106
110
```
107
111
108
112
## Custom Initialization
@@ -150,13 +154,16 @@ Here's what happens when a tool is used:
150
154
For example:
151
155
152
156
```ruby
153
-
response = chat.ask "What's 123 squared plus 456?"
157
+
response = chat.ask "What's the weather like in Paris? Coordinates are 48.8566, 2.3522. Also, what time is it there?"
154
158
155
159
# Behind the scenes:
156
-
# 1. Model decides it needs to calculate
157
-
# 2. Model calls Calculator with expression: "123 * 123 + 456"
158
-
# 3. Tool returns "15,585"
159
-
# 4. Model incorporates this in its response
160
+
# 1. Model decides it needs weather data
161
+
# 2. Model calls Weather with coordinates for Paris
# When there's an error, the model will receive and explain it
199
-
chat.ask "What's 1/0?"
200
-
# => "I tried to calculate 1/0, but there was an error: divided by 0"
206
+
chat.ask "What's the weather at invalid coordinates 1000, 1000?"
207
+
# => "The coordinates 1000, 1000 are not valid for any location on Earth, as latitude must be between -90 and 90, and longitude must be between -180 and 180. Please provide valid coordinates or a city name for weather information."
201
208
```
202
209
203
210
## Advanced Tool Parameters
@@ -236,6 +243,14 @@ class DataAnalysis < RubyLLM::Tool
236
243
end
237
244
```
238
245
246
+
## Security Considerations
247
+
248
+
When implementing tools that process user input (via the AI):
249
+
250
+
* Avoid using `eval`, `system` or similar methods with unsanitized input
251
+
* Remember that AI models might be tricked into producing dangerous inputs
252
+
* Validate all inputs and use appropriate sanitization
0 commit comments