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.
@@ -1,80 +1,82 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module RubyLLM
4
- module ActiveRecord
5
- # Methods mixed into message models.
6
- module MessageMethods
7
- extend ActiveSupport::Concern
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
- class_methods do
10
- attr_reader :chat_class, :tool_call_class, :chat_foreign_key, :tool_call_foreign_key
11
- end
10
+ class_methods do
11
+ attr_reader :chat_class, :tool_call_class, :chat_foreign_key, :tool_call_foreign_key
12
+ end
12
13
 
13
- def to_llm
14
- cached = has_attribute?(:cached_tokens) ? self[:cached_tokens] : nil
15
- cache_creation = has_attribute?(:cache_creation_tokens) ? self[:cache_creation_tokens] : nil
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
- RubyLLM::Message.new(
18
- role: role.to_sym,
19
- content: extract_content,
20
- tool_calls: extract_tool_calls,
21
- tool_call_id: extract_tool_call_id,
22
- input_tokens: input_tokens,
23
- output_tokens: output_tokens,
24
- cached_tokens: cached,
25
- cache_creation_tokens: cache_creation,
26
- model_id: model_association&.model_id
27
- )
28
- end
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
- private
31
+ private
31
32
 
32
- def extract_tool_calls
33
- tool_calls_association.to_h do |tool_call|
34
- [
35
- tool_call.tool_call_id,
36
- RubyLLM::ToolCall.new(
37
- id: tool_call.tool_call_id,
38
- name: tool_call.name,
39
- arguments: tool_call.arguments
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
- def extract_tool_call_id
46
- parent_tool_call&.tool_call_id
47
- end
46
+ def extract_tool_call_id
47
+ parent_tool_call&.tool_call_id
48
+ end
48
49
 
49
- def extract_content
50
- return RubyLLM::Content::Raw.new(content_raw) if has_attribute?(:content_raw) && content_raw.present?
50
+ def extract_content
51
+ return RubyLLM::Content::Raw.new(content_raw) if has_attribute?(:content_raw) && content_raw.present?
51
52
 
52
- content_value = self[:content]
53
+ content_value = self[:content]
53
54
 
54
- return content_value unless respond_to?(:attachments) && attachments.attached?
55
+ return content_value unless respond_to?(:attachments) && attachments.attached?
55
56
 
56
- RubyLLM::Content.new(content_value).tap do |content_obj|
57
- @_tempfiles = []
57
+ RubyLLM::Content.new(content_value).tap do |content_obj|
58
+ @_tempfiles = []
58
59
 
59
- attachments.each do |attachment|
60
- tempfile = download_attachment(attachment)
61
- content_obj.add_attachment(tempfile, filename: attachment.filename.to_s)
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
- def download_attachment(attachment)
67
- ext = File.extname(attachment.filename.to_s)
68
- basename = File.basename(attachment.filename.to_s, ext)
69
- tempfile = Tempfile.new([basename, ext])
70
- tempfile.binmode
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
- attachment.download { |chunk| tempfile.write(chunk) }
73
+ attachment.download { |chunk| tempfile.write(chunk) }
73
74
 
74
- tempfile.flush
75
- tempfile.rewind
76
- @_tempfiles << tempfile
77
- tempfile
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
- module RubyLLM
4
- module ActiveRecord
5
- # Methods mixed into model registry models.
6
- module ModelMethods
7
- extend ActiveSupport::Concern
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
- class_methods do # rubocop:disable Metrics/BlockLength
10
- def refresh!
11
- RubyLLM.models.refresh!
10
+ class_methods do # rubocop:disable Metrics/BlockLength
11
+ def refresh!
12
+ RubyLLM.models.refresh!
12
13
 
13
- transaction do
14
- RubyLLM.models.all.each do |model_info|
15
- model = find_or_initialize_by(
16
- model_id: model_info.id,
17
- provider: model_info.provider
18
- )
19
- model.update!(from_llm_attributes(model_info))
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
- def save_to_database
25
- transaction do
26
- RubyLLM.models.all.each do |model_info|
27
- model = find_or_initialize_by(
28
- model_id: model_info.id,
29
- provider: model_info.provider
30
- )
31
- model.update!(from_llm_attributes(model_info))
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
- def from_llm(model_info)
37
- new(from_llm_attributes(model_info))
38
- end
37
+ def from_llm(model_info)
38
+ new(from_llm_attributes(model_info))
39
+ end
39
40
 
40
- private
41
+ private
41
42
 
42
- def from_llm_attributes(model_info)
43
- {
44
- model_id: model_info.id,
45
- name: model_info.name,
46
- provider: model_info.provider,
47
- family: model_info.family,
48
- model_created_at: model_info.created_at,
49
- context_window: model_info.context_window,
50
- max_output_tokens: model_info.max_output_tokens,
51
- knowledge_cutoff: model_info.knowledge_cutoff,
52
- modalities: model_info.modalities.to_h,
53
- capabilities: model_info.capabilities,
54
- pricing: model_info.pricing.to_h,
55
- metadata: model_info.metadata
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
- def to_llm
61
- RubyLLM::Model::Info.new(
62
- id: model_id,
63
- name: name,
64
- provider: provider,
65
- family: family,
66
- created_at: model_created_at,
67
- context_window: context_window,
68
- max_output_tokens: max_output_tokens,
69
- knowledge_cutoff: knowledge_cutoff,
70
- modalities: modalities&.deep_symbolize_keys || {},
71
- capabilities: capabilities,
72
- pricing: pricing&.deep_symbolize_keys || {},
73
- metadata: metadata&.deep_symbolize_keys || {}
74
- )
75
- end
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
- delegate :supports?, :supports_vision?, :supports_functions?, :type,
78
- :input_price_per_million, :output_price_per_million,
79
- :function_calling?, :structured_output?, :batch?,
80
- :reasoning?, :citations?, :streaming?, :provider_class,
81
- to: :to_llm
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLLM
4
- VERSION = '1.9.1'
4
+ VERSION = '1.9.2'
5
5
  end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_llm_swarm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.1
4
+ version: 1.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Arruda