-
-
Notifications
You must be signed in to change notification settings - Fork 190
Rails Generator for RubyLLM Models #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 36 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
487d4e4
feat: add Rails generator for RubyLLM models
kieranklaassen 3577f31
test: add generator template tests and fix zeitwerk warning
kieranklaassen 24467aa
feat: add cross-database support for JSON columns
kieranklaassen 2d50909
fix: ensure proper migration order for database schema
kieranklaassen 6e68d36
Add README template for generator output
kieranklaassen dacb148
Remove unnecessary comments from create_tool_calls_migration.rb
kieranklaassen f469f60
Update generator templates to use .tt extension
kieranklaassen 844614b
Improve test organization and fix style issues
kieranklaassen 131e51c
chore: memory
kieranklaassen f40d1b4
Delete CLAUDE.md
kieranklaassen 5eeedf9
Update CONTRIBUTING.md
kieranklaassen 9d8c2d6
chore: clean up whitespace and add install generator tests
kieranklaassen 61d66ab
first run at options for model names
jamster a239b34
fixing generator specs with updated `options` setup
jamster 8316ce5
Merge pull request #1 from jamster/generators
kieranklaassen cf4a560
Merge branch 'main' into generators
kieranklaassen 492188f
Merge branch 'main' into generators
crmne 52d761a
Merge branch 'main' into generators
kieranklaassen cc3d23f
Addresses comments in PR
jamster 9eba7bc
Merge pull request #2 from jamster/generators
kieranklaassen 7cded01
Merge branch 'main' into generators
kieranklaassen 21c4ee0
Merge branch 'main' into generators
kieranklaassen fda4df1
docs
kieranklaassen 6270f22
docs: Update Rails integration guide to include generator usage and i…
kieranklaassen f5454aa
docs: Update Rails integration guide with user association in chat re…
kieranklaassen 5be36c1
docs: Add ChatStreamJob implementation to Rails integration guide
kieranklaassen 1f8d3b0
Merge branch 'main' into generators
kieranklaassen a350738
Merge branch 'main' into generators
crmne 1ed3c2e
Address PR comments: rename README.md.tt, fix migration order, update…
kieranklaassen f16858a
Delete .cursor/rules/add_new_provider.mdc
kieranklaassen 26bcf50
Delete .cursor/rules/ruby_llm_conventions.mdc
kieranklaassen c352d5f
Delete .cursor/rules/ruby_style_guide.mdc
kieranklaassen b064713
Delete .cursor/rules/ruby_llm_philosophy.mdc
kieranklaassen 5a9b3b8
fix: address PR comments - remove sleep calls and fix formatting
kieranklaassen 224b24e
fix: revert rails.md to previous version as requested in PR comments
kieranklaassen 1fe4320
fix: remove empty line in rails.md as requested in PR comments
kieranklaassen 882f3fc
fix: revert rails.md to original state as requested in PR review
kieranklaassen 4789f80
test: update generator specs to match INSTALL_INFO rename
kieranklaassen d2196bd
Merge branch 'main' of https://github.com/crmne/ruby_llm into generators
kieranklaassen bf2244a
docs: update Rails guide to include quick setup instructions with gen…
kieranklaassen 145d2d2
Merge branch 'main' into generators
crmne 5982876
fix: improve line length formatting in migration templates and specs
crmne 50befbe
docs: update README to streamline chat model persistence instructions
crmne 9d92db3
fix: update tool_calls migration for database compatibility and indexing
crmne 4cc4054
refactor: remove rake tasks loading from Railtie
crmne 3ecfeaf
docs: update INSTALL_INFO.md.tt to enhance setup instructions and add…
crmne d66c305
docs: update installation instructions to highlight generator availab…
crmne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
lib/generators/ruby_llm/install/templates/INSTALL_INFO.md.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# RubyLLM Rails Setup Complete! | ||
|
||
Thanks for installing RubyLLM in your Rails application. Here's what was created: | ||
|
||
## Models | ||
|
||
- `<%= options[:chat_model_name] %>` - Stores chat sessions and their associated model ID | ||
- `<%= options[:message_model_name] %>` - Stores individual messages in a chat | ||
- `<%= options[:tool_call_model_name] %>` - Stores tool calls made by language models | ||
|
||
## Configuration Options | ||
|
||
The generator supports the following options to customize model names: | ||
|
||
```bash | ||
rails generate ruby_llm:install \ | ||
--chat-model-name=Conversation \ | ||
--message-model-name=ChatMessage \ | ||
--tool-call-model-name=FunctionCall | ||
``` | ||
|
||
This is useful when you need to avoid namespace collisions with existing models in your application. Table names will be automatically derived from the model names following Rails conventions. | ||
|
||
## Next Steps | ||
|
||
1. **Run migrations:** | ||
```bash | ||
rails db:migrate | ||
``` | ||
|
||
2. **Set your API keys** in `config/initializers/ruby_llm.rb` or using environment variables: | ||
```ruby | ||
# config/initializers/ruby_llm.rb | ||
RubyLLM.configure do |config| | ||
config.openai_api_key = ENV["OPENAI_API_KEY"] | ||
config.anthropic_api_key = ENV["ANTHROPIC_API_KEY"] | ||
# etc. | ||
end | ||
``` | ||
|
||
3. **Start using RubyLLM in your code:** | ||
```ruby | ||
# In a background job | ||
class ChatJob < ApplicationJob | ||
def perform(chat_id, question) | ||
chat = <%= options[:chat_model_name] %>.find(chat_id) | ||
chat.ask(question) do |chunk| | ||
Turbo::StreamsChannel.broadcast_append_to( | ||
chat, target: "response", partial: "messages/chunk", locals: { chunk: chunk } | ||
) | ||
end | ||
end | ||
end | ||
|
||
# Queue the job | ||
chat = <%= options[:chat_model_name] %>.create!(model_id: "gpt-4o-mini") | ||
ChatJob.perform_later(chat.id, "What's your favorite Ruby gem?") | ||
``` | ||
|
||
4. **For streaming responses** with ActionCable or Turbo: | ||
```ruby | ||
chat.ask("Tell me about Ruby on Rails") do |chunk| | ||
Turbo::StreamsChannel.broadcast_append_to( | ||
chat, target: "response", partial: "messages/chunk", locals: { chunk: chunk } | ||
) | ||
end | ||
``` | ||
|
||
## Advanced Usage | ||
|
||
- Add more fields to your models as needed | ||
- Customize the views to match your application design | ||
- Create a controller for chat interactions | ||
|
||
For more information, visit the [RubyLLM Documentation](https://github.com/crmne/ruby_llm) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class <%= options[:chat_model_name] %> < ApplicationRecord | ||
<%= acts_as_chat_declaration %> | ||
end |
8 changes: 8 additions & 0 deletions
8
lib/generators/ruby_llm/install/templates/create_chats_migration.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Create<%= options[:chat_model_name].pluralize %> < ActiveRecord::Migration<%= migration_version %> | ||
def change | ||
create_table :<%= options[:chat_model_name].tableize %> do |t| | ||
t.string :model_id | ||
t.timestamps | ||
end | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
lib/generators/ruby_llm/install/templates/create_messages_migration.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Migration for creating messages table with references to chats and tool_calls | ||
class Create<%= options[:message_model_name].pluralize %> < ActiveRecord::Migration<%= migration_version %> | ||
def change | ||
create_table :<%= options[:message_model_name].tableize %> do |t| | ||
t.references :<%= options[:chat_model_name].tableize.singularize %>, null: false, foreign_key: true | ||
t.string :role | ||
t.text :content | ||
t.string :model_id | ||
t.integer :input_tokens | ||
t.integer :output_tokens | ||
t.references :<%= options[:tool_call_model_name].tableize.singularize %> | ||
t.timestamps | ||
end | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
lib/generators/ruby_llm/install/templates/create_tool_calls_migration.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<%#- # Migration for creating tool_calls table with database-specific JSON handling -%> | ||
class Create<%= options[:tool_call_model_name].pluralize %> < ActiveRecord::Migration<%= migration_version %> | ||
def change | ||
create_table :<%= options[:tool_call_model_name].tableize %> do |t| | ||
t.references :<%= options[:message_model_name].tableize.singularize %>, null: false, foreign_key: true | ||
t.string :tool_call_id, null: false | ||
t.string :name, null: false | ||
t.<%= postgresql? ? 'jsonb' : 'json' %> :arguments, default: {} | ||
t.timestamps | ||
end | ||
|
||
add_index :<%= options[:tool_call_model_name].tableize %>, :tool_call_id | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
lib/generators/ruby_llm/install/templates/initializer.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# RubyLLM configuration | ||
RubyLLM.configure do |config| | ||
# Set your API keys here or use environment variables | ||
# config.openai_api_key = ENV["OPENAI_API_KEY"] | ||
# config.anthropic_api_key = ENV["ANTHROPIC_API_KEY"] | ||
# config.gemini_api_key = ENV["GEMINI_API_KEY"] | ||
# config.deepseek_api_key = ENV["DEEPSEEK_API_KEY"] | ||
|
||
# Uncomment to set a default model | ||
# config.default_model = "gpt-4o-mini" | ||
|
||
# Uncomment to set default options | ||
# config.default_options = { temperature: 0.7 } | ||
end |
3 changes: 3 additions & 0 deletions
3
lib/generators/ruby_llm/install/templates/message_model.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class <%= options[:message_model_name] %> < ApplicationRecord | ||
<%= acts_as_message_declaration %> | ||
end |
3 changes: 3 additions & 0 deletions
3
lib/generators/ruby_llm/install/templates/tool_call_model.rb.tt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class <%= options[:tool_call_model_name] %> < ApplicationRecord | ||
<%= acts_as_tool_call_declaration %> | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.