ruby_llm 0.1.0.pre10 → 0.1.0.pre12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +32 -12
- data/lib/ruby_llm/tool.rb +24 -52
- data/lib/ruby_llm/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70c8538fcfbdaba0d5bbb502affe66acfc38c5f67318ee69fe9dc203dbc4e7f7
|
4
|
+
data.tar.gz: 51a0759de14ee600876471dd034e0ff99b3763d765fd3b81eadfe4dacbd3f7e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b5a3362b20ffb9153f9668612a0b6fde3fdf040d7234baee08978cdcc60717fce0cf8a0950929e0ab384db0fe9cfd82456e0a3c4f6eaba566999c80f32fc941
|
7
|
+
data.tar.gz: 6f12660ca79c9d00196003c15bc866b831e47bbce52fc6d6f2ad731655b8e6243388ad4637d7b3c45cb1076c489fa6a8ca9973da20230cbc16b756782917056c
|
data/README.md
CHANGED
@@ -79,7 +79,7 @@ puts "Conversation used #{last_message.input_tokens} input tokens and #{last_mes
|
|
79
79
|
|
80
80
|
## Using Tools
|
81
81
|
|
82
|
-
Give Claude some Ruby superpowers by letting it call your code. Simply create
|
82
|
+
Give Claude some Ruby superpowers by letting it call your code. Simply create tool classes that do one thing well:
|
83
83
|
|
84
84
|
```ruby
|
85
85
|
class CalculatorTool < RubyLLM::Tool
|
@@ -90,28 +90,48 @@ class CalculatorTool < RubyLLM::Tool
|
|
90
90
|
required: true,
|
91
91
|
desc: "A mathematical expression to evaluate (e.g. '2 + 2')"
|
92
92
|
|
93
|
-
|
93
|
+
def execute(expression:)
|
94
|
+
eval(expression).to_s
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class DocumentSearchTool < RubyLLM::Tool
|
99
|
+
description "Searches documents by similarity"
|
100
|
+
|
101
|
+
param :query,
|
102
|
+
type: :string,
|
103
|
+
desc: "The search query"
|
94
104
|
|
95
|
-
|
96
|
-
|
105
|
+
param :limit,
|
106
|
+
type: :integer,
|
107
|
+
desc: "Number of results to return",
|
108
|
+
required: false
|
109
|
+
|
110
|
+
def initialize(document_class:)
|
111
|
+
@document_class = document_class
|
112
|
+
end
|
113
|
+
|
114
|
+
def execute(query:, limit: 5)
|
115
|
+
@document_class.similarity_search(query:, k: limit)
|
97
116
|
end
|
98
117
|
end
|
99
118
|
```
|
100
119
|
|
101
|
-
Then use
|
120
|
+
Then use them in your conversations:
|
102
121
|
|
103
122
|
```ruby
|
104
|
-
|
123
|
+
# Simple tools just work
|
124
|
+
chat = RubyLLM.chat.with_tool(CalculatorTool.new)
|
125
|
+
|
126
|
+
# Tools with dependencies are just regular Ruby objects
|
127
|
+
search = DocumentSearchTool.new(document_class: Document)
|
128
|
+
chat.with_tools(search, CalculatorTool.new)
|
105
129
|
|
106
|
-
# Claude will automatically use the calculator when appropriate
|
107
130
|
chat.ask "What's 2+2?"
|
108
131
|
# => "Let me calculate that for you. The result is 4."
|
109
132
|
|
110
|
-
chat.ask "
|
111
|
-
# => "
|
112
|
-
|
113
|
-
# Add multiple tools
|
114
|
-
chat.with_tools(CalculatorTool, WeatherTool, DatabaseTool)
|
133
|
+
chat.ask "Find documents about Ruby performance"
|
134
|
+
# => "I found these relevant documents about Ruby performance..."
|
115
135
|
```
|
116
136
|
|
117
137
|
Tools let you seamlessly integrate your Ruby code with AI capabilities. The model will automatically decide when to use your tools and handle the results appropriately.
|
data/lib/ruby_llm/tool.rb
CHANGED
@@ -1,7 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# lib/ruby_llm/tool.rb
|
4
3
|
module RubyLLM
|
4
|
+
class Parameter
|
5
|
+
attr_reader :name, :type, :description, :required
|
6
|
+
|
7
|
+
def initialize(name, type: 'string', desc: nil, required: true)
|
8
|
+
@name = name
|
9
|
+
@type = type
|
10
|
+
@description = desc
|
11
|
+
@required = required
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
5
15
|
class Tool
|
6
16
|
class << self
|
7
17
|
def description(text = nil)
|
@@ -10,50 +20,34 @@ module RubyLLM
|
|
10
20
|
@description = text
|
11
21
|
end
|
12
22
|
|
13
|
-
def param(name,
|
14
|
-
|
15
|
-
name,
|
16
|
-
type: type.to_s,
|
17
|
-
description: desc,
|
18
|
-
required: required
|
19
|
-
)
|
20
|
-
parameters[name] = param
|
23
|
+
def param(name, **options)
|
24
|
+
parameters[name] = Parameter.new(name, **options)
|
21
25
|
end
|
22
26
|
|
23
27
|
def parameters
|
24
28
|
@parameters ||= {}
|
25
29
|
end
|
30
|
+
end
|
26
31
|
|
27
|
-
|
28
|
-
|
32
|
+
def name
|
33
|
+
self.class.name
|
29
34
|
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
30
35
|
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
31
36
|
.downcase
|
32
37
|
.delete_suffix('_tool')
|
33
|
-
|
34
|
-
|
35
|
-
def to_tool
|
36
|
-
tool_instance = new
|
37
|
-
|
38
|
-
def tool_instance.name
|
39
|
-
self.class.name
|
40
|
-
end
|
41
|
-
|
42
|
-
def tool_instance.description
|
43
|
-
self.class.description
|
44
|
-
end
|
38
|
+
end
|
45
39
|
|
46
|
-
|
47
|
-
|
48
|
-
|
40
|
+
def description
|
41
|
+
self.class.description
|
42
|
+
end
|
49
43
|
|
50
|
-
|
51
|
-
|
44
|
+
def parameters
|
45
|
+
self.class.parameters
|
52
46
|
end
|
53
47
|
|
54
48
|
def call(args)
|
55
49
|
RubyLLM.logger.debug "Tool #{name} called with: #{args.inspect}"
|
56
|
-
result = execute(args.transform_keys(&:to_sym))
|
50
|
+
result = execute(**args.transform_keys(&:to_sym))
|
57
51
|
RubyLLM.logger.debug "Tool #{name} returned: #{result.inspect}"
|
58
52
|
result
|
59
53
|
rescue StandardError => e
|
@@ -61,30 +55,8 @@ module RubyLLM
|
|
61
55
|
{ error: e.message }
|
62
56
|
end
|
63
57
|
|
64
|
-
|
65
|
-
|
66
|
-
def execute(args)
|
58
|
+
def execute(...)
|
67
59
|
raise NotImplementedError, 'Subclasses must implement #execute'
|
68
60
|
end
|
69
61
|
end
|
70
|
-
|
71
|
-
# Using the existing Parameter class from Tool.rb
|
72
|
-
class Parameter
|
73
|
-
attr_reader :name, :type, :description, :required
|
74
|
-
|
75
|
-
def initialize(name, type: 'string', description: nil, required: true)
|
76
|
-
@name = name
|
77
|
-
@type = type
|
78
|
-
@description = description
|
79
|
-
@required = required
|
80
|
-
end
|
81
|
-
|
82
|
-
def to_h
|
83
|
-
{
|
84
|
-
type: type,
|
85
|
-
description: description,
|
86
|
-
required: required
|
87
|
-
}.compact
|
88
|
-
end
|
89
|
-
end
|
90
62
|
end
|
data/lib/ruby_llm/version.rb
CHANGED