ruby_llm 0.1.0.pre14 → 0.1.0.pre15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 634e727da013e56b973856876d2b456075841bd3cd1f9a5939d2028d96a47b38
4
- data.tar.gz: af14dd677a8718883f0bc7c555272b5a894457d472edae1e0a4c44de8d9934ba
3
+ metadata.gz: ca489465046f71efd3a80717b46a68cdcbc754527f4b5ccade15442d2a95854e
4
+ data.tar.gz: b1091dd99c8b96266b767b1be69ade14a88cffc0c3f2cd07c1bf20d9efebe6e2
5
5
  SHA512:
6
- metadata.gz: 9c1dcc9ac485aa9f85592c0dd12d2bfe3894bfbd7311f19a1168ed17289d21edc14c636532212b01e544365519477314e03be0b34d238f2f6390834ac0a280c7
7
- data.tar.gz: 9e4db61a0e5e73d13d7b8a03149625e463f7eb52d181acfc8f3ce06db0a7294615cf17f0bdbcced5f5a2908aa503af3fbc6c0122541ad704f1ddb4d17ea3b494
6
+ metadata.gz: 8bd968d14ec94fb254ae21a3d253cbe8b952145d3626540bf92cc5ded13d660e8f45b651319d3591f6a6e6b137c02cb94540c2a00680c25c2d7d3ecf4d799bbf
7
+ data.tar.gz: d1a5e1ef82b95f5ebe59b27440d703e88d4332f47cb706da8c4f09487608d6de572e78a38756af938a0e0ed0dbf8dc0f4a45aa82ac1d32f444681c3172baf40a
data/.overcommit.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  PreCommit:
2
2
  RuboCop:
3
- enabled: false
3
+ enabled: true
4
4
  auto_correct: true
5
5
  on_warn: fail # Treat all warnings as failures
6
6
 
data/README.md CHANGED
@@ -167,7 +167,7 @@ class CreateMessages < ActiveRecord::Migration[8.0]
167
167
  t.references :chat
168
168
  t.string :role
169
169
  t.text :content
170
- t.json :tool_calls
170
+ t.json :tool_calls, default: {}
171
171
  t.string :tool_call_id
172
172
  t.integer :input_tokens
173
173
  t.integer :output_tokens
@@ -182,14 +182,14 @@ Then in your models:
182
182
 
183
183
  ```ruby
184
184
  class Chat < ApplicationRecord
185
- acts_as_chat message_class: "Message"
185
+ acts_as_chat
186
186
 
187
187
  # Optional: Add Turbo Streams support
188
188
  broadcasts_to ->(chat) { "chat_#{chat.id}" }
189
189
  end
190
190
 
191
191
  class Message < ApplicationRecord
192
- acts_as_message chat_class: "Chat"
192
+ acts_as_message
193
193
  end
194
194
  ```
195
195
 
@@ -9,7 +9,7 @@ module RubyLLM
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  class_methods do
12
- def acts_as_chat(message_class:)
12
+ def acts_as_chat(message_class: 'Message')
13
13
  include ChatMethods
14
14
 
15
15
  has_many :messages,
@@ -17,16 +17,13 @@ module RubyLLM
17
17
  class_name: message_class.to_s,
18
18
  dependent: :destroy
19
19
 
20
- # No more callback config - just expose the core chat functionality
21
- delegate :ask, :say, :complete, to: :chat
20
+ delegate :complete, to: :chat
22
21
  end
23
22
 
24
- def acts_as_message(chat_class:)
23
+ def acts_as_message(chat_class: 'Chat')
25
24
  include MessageMethods
26
25
 
27
26
  belongs_to :chat, class_name: chat_class.to_s
28
-
29
- serialize :tool_calls, coder: JSON
30
27
  end
31
28
  end
32
29
  end
@@ -37,42 +34,43 @@ module RubyLLM
37
34
  extend ActiveSupport::Concern
38
35
 
39
36
  def chat
40
- @chat ||= begin
41
- chat = RubyLLM.chat(model: model_id)
37
+ chat = RubyLLM.chat(model: model_id)
42
38
 
43
- # Load existing messages into chat
44
- messages.each do |msg|
45
- chat.add_message(msg.to_llm)
46
- end
39
+ # Load existing messages into chat
40
+ messages.each do |msg|
41
+ chat.add_message(msg.to_llm)
42
+ end
47
43
 
48
- # Set up message persistence
49
- chat.on_new_message { |msg| persist_new_message(msg) }
50
- .on_end_message { |msg| persist_message_completion(msg) }
44
+ # Set up message persistence
45
+ chat.on_new_message { persist_new_message }
46
+ .on_end_message { |msg| persist_message_completion(msg) }
51
47
 
52
- chat
53
- end
48
+ chat
54
49
  end
55
50
 
56
- private
51
+ def ask(message, &block)
52
+ message = { role: :user, content: message }
53
+ messages.create!(**message)
54
+ chat.complete(&block)
55
+ end
57
56
 
58
- def persist_new_message(message)
59
- return unless message
57
+ alias say ask
60
58
 
61
- messages.create!(
62
- role: message.role,
63
- content: message.content,
64
- tool_calls: message.tool_calls,
65
- tool_call_id: message.tool_call_id,
66
- model_id: message.model_id
67
- )
59
+ private
60
+
61
+ def persist_new_message
62
+ messages.create!
68
63
  end
69
64
 
70
65
  def persist_message_completion(message)
71
66
  return unless message
72
67
 
73
68
  messages.last.update!(
69
+ role: message.role,
74
70
  content: message.content,
71
+ model_id: message.model_id,
75
72
  tool_calls: message.tool_calls,
73
+ tool_call_id: message.tool_call_id,
76
74
  input_tokens: message.input_tokens,
77
75
  output_tokens: message.output_tokens
78
76
  )
data/lib/ruby_llm/chat.rb CHANGED
@@ -87,6 +87,12 @@ module RubyLLM
87
87
  end
88
88
  end
89
89
 
90
+ def add_message(message_or_attributes)
91
+ message = message_or_attributes.is_a?(Message) ? message_or_attributes : Message.new(message_or_attributes)
92
+ messages << message
93
+ message
94
+ end
95
+
90
96
  private
91
97
 
92
98
  def handle_tool_calls(response, &block)
@@ -104,13 +110,6 @@ module RubyLLM
104
110
  tool.call(args)
105
111
  end
106
112
 
107
- def add_message(message_or_attributes)
108
- message = message_or_attributes.is_a?(Message) ? message_or_attributes : Message.new(message_or_attributes)
109
- # TODO: callback
110
- messages << message
111
- message
112
- end
113
-
114
113
  def add_tool_result(tool_use_id, result)
115
114
  add_message(
116
115
  role: :tool,
@@ -3,9 +3,9 @@
3
3
  module RubyLLM
4
4
  # Rails integration for RubyLLM
5
5
  class Railtie < Rails::Railtie
6
- initializer 'ruby_llm.initialize' do
7
- ActiveSupport.on_load(:active_record) do
8
- extend RubyLLM::ActiveRecord::ActsAs
6
+ initializer 'ruby_llm.active_record' do
7
+ ActiveSupport.on_load :active_record do
8
+ include RubyLLM::ActiveRecord::ActsAs
9
9
  end
10
10
  end
11
11
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLLM
4
- VERSION = '0.1.0.pre14'
4
+ VERSION = '0.1.0.pre15'
5
5
  end
data/lib/ruby_llm.rb CHANGED
@@ -7,6 +7,15 @@ require 'logger'
7
7
  require 'event_stream_parser'
8
8
  require 'securerandom'
9
9
 
10
+ loader = Zeitwerk::Loader.for_gem
11
+ loader.inflector.inflect(
12
+ 'ruby_llm' => 'RubyLLM',
13
+ 'llm' => 'LLM',
14
+ 'openai' => 'OpenAI',
15
+ 'api' => 'API'
16
+ )
17
+ loader.setup
18
+
10
19
  # A delightful Ruby interface to modern AI language models.
11
20
  # Provides a unified way to interact with models from OpenAI, Anthropic and others
12
21
  # with a focus on developer happiness and convention over configuration.
@@ -34,40 +43,16 @@ module RubyLLM
34
43
  @logger ||= Logger.new(
35
44
  $stdout,
36
45
  progname: 'RubyLLM',
37
- level: ENV['RUBY_LLM_DEBUG'] == 'true' ? Logger::DEBUG : Logger::INFO
46
+ level: ENV['RUBY_LLM_DEBUG'] ? Logger::DEBUG : Logger::INFO
38
47
  )
39
48
  end
40
49
  end
41
50
  end
42
51
 
43
- loader = Zeitwerk::Loader.for_gem
44
-
45
- # Add lib directory to the load path
46
- loader.push_dir(File.expand_path('..', __dir__))
47
-
48
- # Configure custom inflections
49
- loader.inflector.inflect(
50
- 'ruby_llm' => 'RubyLLM',
51
- 'llm' => 'LLM',
52
- 'openai' => 'OpenAI',
53
- 'api' => 'API'
54
- )
55
-
56
- # Ignore Rails-specific files and specs
57
- loader.ignore("#{__dir__}/ruby_llm/railtie.rb")
58
- loader.ignore("#{__dir__}/ruby_llm/active_record")
59
- loader.ignore(File.expand_path('../spec', __dir__).to_s)
60
-
61
- loader.enable_reloading if ENV['RUBY_LLM_DEBUG']
62
-
63
- loader.setup
64
- loader.eager_load if ENV['RUBY_LLM_DEBUG']
65
-
66
52
  RubyLLM::Provider.register :openai, RubyLLM::Providers::OpenAI
67
53
  RubyLLM::Provider.register :anthropic, RubyLLM::Providers::Anthropic
68
54
 
69
- # Load Rails integration if Rails is defined
70
- if defined?(Rails)
55
+ if defined?(Rails::Railtie)
71
56
  require 'ruby_llm/railtie'
72
57
  require 'ruby_llm/active_record/acts_as'
73
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_llm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre14
4
+ version: 0.1.0.pre15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carmine Paolino