@@ -10,7 +10,7 @@ RubyLLM provides a unified interface for interacting with various LLM providers
10
10
## Features
11
11
12
12
- 🤝 Unified interface for multiple LLM providers (OpenAI, Anthropic, etc.)
13
- - 🛠️ Tool/Function calling support
13
+ - 🛠️ Simple and flexible tool/function calling
14
14
- 📊 Automatic token counting and tracking
15
15
- 🔄 Streaming support
16
16
- 🚂 Seamless Rails integration
@@ -60,22 +60,15 @@ response = client.chat([
60
60
puts response.content
61
61
```
62
62
63
- ### Streaming
63
+ ### Tools (Function Calling)
64
64
65
- ``` ruby
66
- client.chat([
67
- { role: :user , content: " Count to 10 slowly" }
68
- ], stream: true ) do |chunk |
69
- print chunk.content
70
- end
71
- ```
72
-
73
- ### Tool Usage
65
+ RubyLLM supports tools/functions with a simple, flexible interface. You can create tools using blocks or wrap existing methods:
74
66
75
67
``` ruby
68
+ # Using a block
76
69
calculator = RubyLLM ::Tool .new (
77
70
name: " calculator" ,
78
- description: " Perform mathematical calculations" ,
71
+ description: " Performs mathematical calculations" ,
79
72
parameters: {
80
73
expression: {
81
74
type: " string" ,
@@ -87,9 +80,46 @@ calculator = RubyLLM::Tool.new(
87
80
eval (args[:expression ]).to_s
88
81
end
89
82
83
+ # Using an existing method
84
+ class MathUtils
85
+ def arithmetic (x , y , operation )
86
+ case operation
87
+ when ' add' then x + y
88
+ when ' subtract' then x - y
89
+ when ' multiply' then x * y
90
+ when ' divide' then x.to_f / y
91
+ else
92
+ raise ArgumentError , " Unknown operation: #{ operation } "
93
+ end
94
+ end
95
+ end
96
+
97
+ math_tool = RubyLLM ::Tool .from_method(
98
+ MathUtils .instance_method(:arithmetic ),
99
+ description: " Performs basic arithmetic operations" ,
100
+ parameter_descriptions: {
101
+ x: " First number in the operation" ,
102
+ y: " Second number in the operation" ,
103
+ operation: " Operation to perform (add, subtract, multiply, divide)"
104
+ }
105
+ )
106
+
107
+ # Use tools in conversations
90
108
response = client.chat([
91
109
{ role: :user , content: " What is 123 * 456?" }
92
110
], tools: [calculator])
111
+
112
+ puts response.content
113
+ ```
114
+
115
+ ### Streaming
116
+
117
+ ``` ruby
118
+ client.chat([
119
+ { role: :user , content: " Count to 10 slowly" }
120
+ ], stream: true ) do |chunk |
121
+ print chunk.content
122
+ end
93
123
```
94
124
95
125
## Rails Integration
0 commit comments