ruby_llm 0.1.0.pre11 → 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 +31 -9
- data/lib/ruby_llm/tool.rb +24 -51
- 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
|
@@ -94,22 +94,44 @@ class CalculatorTool < RubyLLM::Tool
|
|
94
94
|
eval(expression).to_s
|
95
95
|
end
|
96
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"
|
104
|
+
|
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)
|
116
|
+
end
|
117
|
+
end
|
97
118
|
```
|
98
119
|
|
99
|
-
Then use
|
120
|
+
Then use them in your conversations:
|
100
121
|
|
101
122
|
```ruby
|
102
|
-
|
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)
|
103
129
|
|
104
|
-
# Claude will automatically use the calculator when appropriate
|
105
130
|
chat.ask "What's 2+2?"
|
106
131
|
# => "Let me calculate that for you. The result is 4."
|
107
132
|
|
108
|
-
chat.ask "
|
109
|
-
# => "
|
110
|
-
|
111
|
-
# Add multiple tools
|
112
|
-
chat.with_tools(CalculatorTool, WeatherTool, DatabaseTool)
|
133
|
+
chat.ask "Find documents about Ruby performance"
|
134
|
+
# => "I found these relevant documents about Ruby performance..."
|
113
135
|
```
|
114
136
|
|
115
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,51 +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
|
-
|
57
|
-
result = execute(**symbolized_args)
|
50
|
+
result = execute(**args.transform_keys(&:to_sym))
|
58
51
|
RubyLLM.logger.debug "Tool #{name} returned: #{result.inspect}"
|
59
52
|
result
|
60
53
|
rescue StandardError => e
|
@@ -62,28 +55,8 @@ module RubyLLM
|
|
62
55
|
{ error: e.message }
|
63
56
|
end
|
64
57
|
|
65
|
-
def execute(
|
58
|
+
def execute(...)
|
66
59
|
raise NotImplementedError, 'Subclasses must implement #execute'
|
67
60
|
end
|
68
61
|
end
|
69
|
-
|
70
|
-
# Using the existing Parameter class from Tool.rb
|
71
|
-
class Parameter
|
72
|
-
attr_reader :name, :type, :description, :required
|
73
|
-
|
74
|
-
def initialize(name, type: 'string', description: nil, required: true)
|
75
|
-
@name = name
|
76
|
-
@type = type
|
77
|
-
@description = description
|
78
|
-
@required = required
|
79
|
-
end
|
80
|
-
|
81
|
-
def to_h
|
82
|
-
{
|
83
|
-
type: type,
|
84
|
-
description: description,
|
85
|
-
required: required
|
86
|
-
}.compact
|
87
|
-
end
|
88
|
-
end
|
89
62
|
end
|
data/lib/ruby_llm/version.rb
CHANGED