ruby_llm_swarm 1.9.1 → 1.9.2
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/lib/ruby_llm/active_record/acts_as.rb +130 -128
- data/lib/ruby_llm/active_record/acts_as_legacy.rb +301 -299
- data/lib/ruby_llm/active_record/chat_methods.rb +278 -276
- data/lib/ruby_llm/active_record/message_methods.rb +60 -58
- data/lib/ruby_llm/active_record/model_methods.rb +68 -66
- data/lib/ruby_llm/railtie.rb +4 -0
- data/lib/ruby_llm/version.rb +1 -1
- data/lib/ruby_llm.rb +1 -1
- metadata +1 -1
|
@@ -1,80 +1,82 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
module
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
if defined?(ActiveRecord::Base)
|
|
4
|
+
module RubyLLM
|
|
5
|
+
module ActiveRecord
|
|
6
|
+
# Methods mixed into message models.
|
|
7
|
+
module MessageMethods
|
|
8
|
+
extend ActiveSupport::Concern
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
class_methods do
|
|
11
|
+
attr_reader :chat_class, :tool_call_class, :chat_foreign_key, :tool_call_foreign_key
|
|
12
|
+
end
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
def to_llm
|
|
15
|
+
cached = has_attribute?(:cached_tokens) ? self[:cached_tokens] : nil
|
|
16
|
+
cache_creation = has_attribute?(:cache_creation_tokens) ? self[:cache_creation_tokens] : nil
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
RubyLLM::Message.new(
|
|
19
|
+
role: role.to_sym,
|
|
20
|
+
content: extract_content,
|
|
21
|
+
tool_calls: extract_tool_calls,
|
|
22
|
+
tool_call_id: extract_tool_call_id,
|
|
23
|
+
input_tokens: input_tokens,
|
|
24
|
+
output_tokens: output_tokens,
|
|
25
|
+
cached_tokens: cached,
|
|
26
|
+
cache_creation_tokens: cache_creation,
|
|
27
|
+
model_id: model_association&.model_id
|
|
28
|
+
)
|
|
29
|
+
end
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
private
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
33
|
+
def extract_tool_calls
|
|
34
|
+
tool_calls_association.to_h do |tool_call|
|
|
35
|
+
[
|
|
36
|
+
tool_call.tool_call_id,
|
|
37
|
+
RubyLLM::ToolCall.new(
|
|
38
|
+
id: tool_call.tool_call_id,
|
|
39
|
+
name: tool_call.name,
|
|
40
|
+
arguments: tool_call.arguments
|
|
41
|
+
)
|
|
42
|
+
]
|
|
43
|
+
end
|
|
42
44
|
end
|
|
43
|
-
end
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
def extract_tool_call_id
|
|
47
|
+
parent_tool_call&.tool_call_id
|
|
48
|
+
end
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
def extract_content
|
|
51
|
+
return RubyLLM::Content::Raw.new(content_raw) if has_attribute?(:content_raw) && content_raw.present?
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
content_value = self[:content]
|
|
53
54
|
|
|
54
|
-
|
|
55
|
+
return content_value unless respond_to?(:attachments) && attachments.attached?
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
RubyLLM::Content.new(content_value).tap do |content_obj|
|
|
58
|
+
@_tempfiles = []
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
attachments.each do |attachment|
|
|
61
|
+
tempfile = download_attachment(attachment)
|
|
62
|
+
content_obj.add_attachment(tempfile, filename: attachment.filename.to_s)
|
|
63
|
+
end
|
|
62
64
|
end
|
|
63
65
|
end
|
|
64
|
-
end
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
def download_attachment(attachment)
|
|
68
|
+
ext = File.extname(attachment.filename.to_s)
|
|
69
|
+
basename = File.basename(attachment.filename.to_s, ext)
|
|
70
|
+
tempfile = Tempfile.new([basename, ext])
|
|
71
|
+
tempfile.binmode
|
|
71
72
|
|
|
72
|
-
|
|
73
|
+
attachment.download { |chunk| tempfile.write(chunk) }
|
|
73
74
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
tempfile.flush
|
|
76
|
+
tempfile.rewind
|
|
77
|
+
@_tempfiles << tempfile
|
|
78
|
+
tempfile
|
|
79
|
+
end
|
|
78
80
|
end
|
|
79
81
|
end
|
|
80
82
|
end
|
|
@@ -1,84 +1,86 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
module
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
if defined?(ActiveRecord::Base)
|
|
4
|
+
module RubyLLM
|
|
5
|
+
module ActiveRecord
|
|
6
|
+
# Methods mixed into model registry models.
|
|
7
|
+
module ModelMethods
|
|
8
|
+
extend ActiveSupport::Concern
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
class_methods do # rubocop:disable Metrics/BlockLength
|
|
11
|
+
def refresh!
|
|
12
|
+
RubyLLM.models.refresh!
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
transaction do
|
|
15
|
+
RubyLLM.models.all.each do |model_info|
|
|
16
|
+
model = find_or_initialize_by(
|
|
17
|
+
model_id: model_info.id,
|
|
18
|
+
provider: model_info.provider
|
|
19
|
+
)
|
|
20
|
+
model.update!(from_llm_attributes(model_info))
|
|
21
|
+
end
|
|
20
22
|
end
|
|
21
23
|
end
|
|
22
|
-
end
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
def save_to_database
|
|
26
|
+
transaction do
|
|
27
|
+
RubyLLM.models.all.each do |model_info|
|
|
28
|
+
model = find_or_initialize_by(
|
|
29
|
+
model_id: model_info.id,
|
|
30
|
+
provider: model_info.provider
|
|
31
|
+
)
|
|
32
|
+
model.update!(from_llm_attributes(model_info))
|
|
33
|
+
end
|
|
32
34
|
end
|
|
33
35
|
end
|
|
34
|
-
end
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
def from_llm(model_info)
|
|
38
|
+
new(from_llm_attributes(model_info))
|
|
39
|
+
end
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
private
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
43
|
+
def from_llm_attributes(model_info)
|
|
44
|
+
{
|
|
45
|
+
model_id: model_info.id,
|
|
46
|
+
name: model_info.name,
|
|
47
|
+
provider: model_info.provider,
|
|
48
|
+
family: model_info.family,
|
|
49
|
+
model_created_at: model_info.created_at,
|
|
50
|
+
context_window: model_info.context_window,
|
|
51
|
+
max_output_tokens: model_info.max_output_tokens,
|
|
52
|
+
knowledge_cutoff: model_info.knowledge_cutoff,
|
|
53
|
+
modalities: model_info.modalities.to_h,
|
|
54
|
+
capabilities: model_info.capabilities,
|
|
55
|
+
pricing: model_info.pricing.to_h,
|
|
56
|
+
metadata: model_info.metadata
|
|
57
|
+
}
|
|
58
|
+
end
|
|
57
59
|
end
|
|
58
|
-
end
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
61
|
+
def to_llm
|
|
62
|
+
RubyLLM::Model::Info.new(
|
|
63
|
+
id: model_id,
|
|
64
|
+
name: name,
|
|
65
|
+
provider: provider,
|
|
66
|
+
family: family,
|
|
67
|
+
created_at: model_created_at,
|
|
68
|
+
context_window: context_window,
|
|
69
|
+
max_output_tokens: max_output_tokens,
|
|
70
|
+
knowledge_cutoff: knowledge_cutoff,
|
|
71
|
+
modalities: modalities&.deep_symbolize_keys || {},
|
|
72
|
+
capabilities: capabilities,
|
|
73
|
+
pricing: pricing&.deep_symbolize_keys || {},
|
|
74
|
+
metadata: metadata&.deep_symbolize_keys || {}
|
|
75
|
+
)
|
|
76
|
+
end
|
|
76
77
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
delegate :supports?, :supports_vision?, :supports_functions?, :type,
|
|
79
|
+
:input_price_per_million, :output_price_per_million,
|
|
80
|
+
:function_calling?, :structured_output?, :batch?,
|
|
81
|
+
:reasoning?, :citations?, :streaming?, :provider_class,
|
|
82
|
+
to: :to_llm
|
|
83
|
+
end
|
|
82
84
|
end
|
|
83
85
|
end
|
|
84
86
|
end
|
data/lib/ruby_llm/railtie.rb
CHANGED
|
@@ -12,6 +12,10 @@ if defined?(Rails::Railtie)
|
|
|
12
12
|
|
|
13
13
|
initializer 'ruby_llm.active_record' do
|
|
14
14
|
ActiveSupport.on_load :active_record do
|
|
15
|
+
require 'ruby_llm/active_record/chat_methods'
|
|
16
|
+
require 'ruby_llm/active_record/message_methods'
|
|
17
|
+
require 'ruby_llm/active_record/model_methods'
|
|
18
|
+
|
|
15
19
|
if RubyLLM.config.use_new_acts_as
|
|
16
20
|
require 'ruby_llm/active_record/acts_as'
|
|
17
21
|
::ActiveRecord::Base.include RubyLLM::ActiveRecord::ActsAs
|
data/lib/ruby_llm/version.rb
CHANGED
data/lib/ruby_llm.rb
CHANGED
|
@@ -32,6 +32,7 @@ loader.inflector.inflect(
|
|
|
32
32
|
loader.ignore("#{__dir__}/tasks")
|
|
33
33
|
loader.ignore("#{__dir__}/generators")
|
|
34
34
|
loader.ignore("#{__dir__}/ruby_llm/railtie.rb")
|
|
35
|
+
loader.ignore("#{__dir__}/ruby_llm/active_record")
|
|
35
36
|
loader.setup
|
|
36
37
|
|
|
37
38
|
# A delightful Ruby interface to modern AI language models.
|
|
@@ -136,5 +137,4 @@ require_relative 'ruby_llm/tool_executors'
|
|
|
136
137
|
|
|
137
138
|
if defined?(Rails::Railtie)
|
|
138
139
|
require 'ruby_llm/railtie'
|
|
139
|
-
require 'ruby_llm/active_record/acts_as'
|
|
140
140
|
end
|