Skip to content

Commit 9eba7bc

Browse files
Merge pull request #2 from jamster/generators
Addresses comments in PR
2 parents 52d761a + cc3d23f commit 9eba7bc

File tree

5 files changed

+32
-42
lines changed

5 files changed

+32
-42
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ group :development do
88
# Rails integration dependencies
99
gem 'activerecord', '>= 6.0', '< 9.0'
1010
gem 'activesupport', '>= 6.0', '< 9.0'
11+
gem 'rails', '>= 7.0.0'
1112

1213
# Development dependencies
1314
gem 'bundler', '>= 2.0'

lib/generators/ruby_llm/install/templates/README.md.tt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,21 @@ This is useful when you need to avoid namespace collisions with existing models
4040

4141
3. **Start using RubyLLM in your code:**
4242
```ruby
43-
# Create a new chat
43+
# In a background job
44+
class ChatJob < ApplicationJob
45+
def perform(chat_id, question)
46+
chat = <%= options[:chat_model_name] %>.find(chat_id)
47+
chat.ask(question) do |chunk|
48+
Turbo::StreamsChannel.broadcast_append_to(
49+
chat, target: "response", partial: "messages/chunk", locals: { chunk: chunk }
50+
)
51+
end
52+
end
53+
end
54+
55+
# Queue the job
4456
chat = <%= options[:chat_model_name] %>.create!(model_id: "gpt-4o-mini")
45-
46-
# Ask a question
47-
response = chat.ask("What's the best Ruby web framework?")
48-
49-
# Get chat history
50-
chat.messages
57+
ChatJob.perform_later(chat.id, "What's your favorite Ruby gem?")
5158
```
5259

5360
4. **For streaming responses** with ActionCable or Turbo:

lib/generators/ruby_llm/install_generator.rb

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ module RubyLLM
77
# Generator for RubyLLM Rails models and migrations
88
class InstallGenerator < Rails::Generators::Base
99
include Rails::Generators::Migration
10-
namespace "ruby_llm:install"
10+
namespace 'ruby_llm:install'
1111

1212
source_root File.expand_path('install/templates', __dir__)
1313

1414
class_option :chat_model_name, type: :string, default: 'Chat',
15-
desc: 'Name of the Chat model class'
15+
desc: 'Name of the Chat model class'
1616
class_option :message_model_name, type: :string, default: 'Message',
17-
desc: 'Name of the Message model class'
17+
desc: 'Name of the Message model class'
1818
class_option :tool_call_model_name, type: :string, default: 'ToolCall',
19-
desc: 'Name of the ToolCall model class'
19+
desc: 'Name of the ToolCall model class'
2020

2121
desc 'Creates model files for Chat, Message, and ToolCall, and creates migrations for RubyLLM Rails integration'
2222

@@ -36,58 +36,46 @@ def postgresql?
3636

3737
def acts_as_chat_declaration
3838
acts_as_chat_params = []
39-
if options[:message_model_name]
40-
acts_as_chat_params << "message_class: \"#{options[:message_model_name]}\""
41-
end
42-
if options[:tool_call_model_name]
43-
acts_as_chat_params << "tool_call_class: \"#{options[:tool_call_model_name]}\""
44-
end
39+
acts_as_chat_params << "message_class: \"#{options[:message_model_name]}\"" if options[:message_model_name]
40+
acts_as_chat_params << "tool_call_class: \"#{options[:tool_call_model_name]}\"" if options[:tool_call_model_name]
4541
if acts_as_chat_params.any?
4642
"acts_as_chat #{acts_as_chat_params.join(', ')}"
4743
else
48-
"acts_as_chat"
44+
'acts_as_chat'
4945
end
5046
end
5147

5248
def acts_as_message_declaration
5349
acts_as_message_params = []
54-
if options[:chat_model_name]
55-
acts_as_message_params << "chat_class: \"#{options[:chat_model_name]}\""
56-
end
50+
acts_as_message_params << "chat_class: \"#{options[:chat_model_name]}\"" if options[:chat_model_name]
5751
if options[:tool_call_model_name]
5852
acts_as_message_params << "tool_call_class: \"#{options[:tool_call_model_name]}\""
5953
end
6054
if acts_as_message_params.any?
6155
"acts_as_message #{acts_as_message_params.join(', ')}"
6256
else
63-
"acts_as_message"
57+
'acts_as_message'
6458
end
6559
end
6660

6761
def acts_as_tool_call_declaration
6862
acts_as_tool_call_params = []
69-
if options[:message_model_name]
70-
acts_as_tool_call_params << "message_class: \"#{options[:message_model_name]}\""
71-
end
63+
acts_as_tool_call_params << "message_class: \"#{options[:message_model_name]}\"" if options[:message_model_name]
7264
if acts_as_tool_call_params.any?
7365
"acts_as_tool_call #{acts_as_tool_call_params.join(', ')}"
7466
else
75-
"acts_as_tool_call"
67+
'acts_as_tool_call'
7668
end
7769
end
7870

7971
def create_migration_files
80-
# Use a fixed timestamp for testing and to ensure they're sequential
81-
# @migration_number = Time.now.utc.strftime('%Y%m%d%H%M%S')
8272
migration_template 'create_chats_migration.rb.tt', "db/migrate/create_#{options[:chat_model_name].tableize}.rb"
8373

84-
# Increment timestamp for the next migration
85-
# @migration_number = (@migration_number.to_i + 1).to_s
86-
migration_template 'create_messages_migration.rb.tt', "db/migrate/create_#{options[:message_model_name].tableize}.rb"
74+
migration_template 'create_messages_migration.rb.tt',
75+
"db/migrate/create_#{options[:message_model_name].tableize}.rb"
8776

88-
# Increment timestamp again for the final migration
89-
# @migration_number = (@migration_number.to_i + 2).to_s
90-
migration_template 'create_tool_calls_migration.rb.tt', "db/migrate/create_#{options[:tool_call_model_name].tableize}.rb"
77+
migration_template 'create_tool_calls_migration.rb.tt',
78+
"db/migrate/create_#{options[:tool_call_model_name].tableize}.rb"
9179
end
9280

9381
def create_model_files

ruby_llm.gemspec

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,11 @@ Gem::Specification.new do |spec|
3131
spec.files = Dir.glob('lib/**/*') + ['README.md', 'LICENSE']
3232
spec.require_paths = ['lib']
3333

34-
# Add generator files
35-
spec.files += Dir.glob('lib/generators/**/*')
36-
3734
# Runtime dependencies
3835
spec.add_dependency 'base64'
3936
spec.add_dependency 'event_stream_parser', '~> 1'
4037
spec.add_dependency 'faraday', '~> 2'
4138
spec.add_dependency 'faraday-multipart', '~> 1'
4239
spec.add_dependency 'faraday-retry', '~> 2'
4340
spec.add_dependency 'zeitwerk', '~> 2'
44-
45-
# Development dependencies
46-
spec.add_development_dependency 'rails', '>= 7.0.0'
4741
end

spec/lib/generators/ruby_llm/install_generator_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
RSpec.describe RubyLLM::InstallGenerator, type: :generator do
88
# Use the actual template directory
9-
let(:template_dir) { File.join(File.dirname(__FILE__), '../../../../lib/generators/ruby_llm/install/templates') }
10-
let(:generator_file) { File.join(File.dirname(__FILE__), '../../../../lib/generators/ruby_llm/install_generator.rb') }
9+
let(:template_dir) { File.join(__dir__, '../../../../lib/generators/ruby_llm/install/templates') }
10+
let(:generator_file) { File.join(__dir__, '../../../../lib/generators/ruby_llm/install_generator.rb') }
1111

1212
describe 'migration templates' do
1313
let(:expected_migration_files) do

0 commit comments

Comments
 (0)