Skip to content

Commit 361af6e

Browse files
committed
Refactor generator to use proper RubyLLM namespace with Zeitwerk inflections
1 parent acd4785 commit 361af6e

File tree

2 files changed

+65
-51
lines changed

2 files changed

+65
-51
lines changed

lib/generators/ruby_llm_template/install_generator.rb renamed to lib/generators/ruby_llm/template/install_generator.rb

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,115 +2,117 @@
22

33
require "rails/generators"
44

5-
module RubyLLMTemplate
6-
module Generators
7-
class InstallGenerator < Rails::Generators::Base
8-
desc "Install RubyLLM Template system"
9-
10-
def self.source_root
11-
@source_root ||= File.expand_path("templates", __dir__)
12-
end
13-
14-
def create_initializer
15-
create_file "config/initializers/ruby_llm_template.rb", <<~RUBY
16-
# frozen_string_literal: true
5+
module RubyLLM
6+
module Template
7+
module Generators
8+
class InstallGenerator < Rails::Generators::Base
9+
desc "Install RubyLLM Template system"
10+
11+
def self.source_root
12+
@source_root ||= File.expand_path("templates", __dir__)
13+
end
14+
15+
def create_initializer
16+
create_file "config/initializers/ruby_llm_template.rb", <<~RUBY
17+
# frozen_string_literal: true
18+
19+
RubyLLM::Template.configure do |config|
20+
# Set the directory where your prompts are stored
21+
# Default: Rails.root.join("app", "prompts")
22+
# config.template_directory = Rails.root.join("app", "prompts")
23+
end
24+
RUBY
25+
end
1726

18-
RubyLLM::Template.configure do |config|
19-
# Set the directory where your prompts are stored
20-
# Default: Rails.root.join("app", "prompts")
21-
# config.template_directory = Rails.root.join("app", "prompts")
22-
end
23-
RUBY
24-
end
27+
def create_template_directory
28+
empty_directory "app/prompts"
2529

26-
def create_template_directory
27-
empty_directory "app/prompts"
30+
create_file "app/prompts/.keep", ""
2831

29-
create_file "app/prompts/.keep", ""
32+
# Create an example template
33+
create_example_template
34+
end
3035

31-
# Create an example template
32-
create_example_template
33-
end
36+
def show_readme
37+
say <<~MESSAGE
3438
35-
def show_readme
36-
say <<~MESSAGE
37-
3839
RubyLLM Template has been installed!
39-
40+
4041
Prompts directory: app/prompts/
4142
Configuration: config/initializers/ruby_llm_template.rb
42-
43+
4344
Example usage:
4445
RubyLLM.chat.with_template(:extract_metadata, document: @document).complete
45-
46+
4647
Template structure:
4748
app/prompts/extract_metadata/
4849
├── system.txt.erb # System message
4950
├── user.txt.erb # User prompt
5051
├── assistant.txt.erb # Assistant message (optional)
5152
└── schema.rb # RubyLLM::Schema definition (optional)
52-
53+
5354
Get started by creating your first template!
54-
MESSAGE
55-
end
55+
MESSAGE
56+
end
5657

57-
private
58+
private
5859

59-
def create_example_template
60-
example_dir = "app/prompts/extract_metadata"
61-
empty_directory example_dir
60+
def create_example_template
61+
example_dir = "app/prompts/extract_metadata"
62+
empty_directory example_dir
6263

63-
create_file "#{example_dir}/system.txt.erb", <<~ERB
64+
create_file "#{example_dir}/system.txt.erb", <<~ERB
6465
You are an expert document analyzer. Your task is to extract metadata from the provided document.
65-
66+
6667
Please analyze the document carefully and extract relevant information such as:
6768
- Document type
6869
- Key topics
6970
- Important dates
7071
- Main entities mentioned
71-
72+
7273
Provide your analysis in a structured format.
73-
ERB
74+
ERB
7475

75-
create_file "#{example_dir}/user.txt.erb", <<~ERB
76+
create_file "#{example_dir}/user.txt.erb", <<~ERB
7677
Please analyze the following document and extract its metadata:
77-
78+
7879
<% if defined?(document) && document %>
7980
Document: <%= document %>
8081
<% else %>
8182
[Document content will be provided here]
8283
<% end %>
83-
84+
8485
<% if defined?(additional_context) && additional_context %>
8586
Additional context: <%= additional_context %>
8687
<% end %>
87-
ERB
88+
ERB
8889

89-
create_file "#{example_dir}/schema.rb", <<~RUBY
90+
create_file "#{example_dir}/schema.rb", <<~RUBY
9091
# frozen_string_literal: true
9192
9293
# Schema definition using RubyLLM::Schema DSL
9394
# See: https://github.com/danielfriis/ruby_llm-schema
9495
9596
RubyLLM::Schema.create do
9697
string :document_type, description: "The type of document (e.g., report, article, email)"
97-
98+
9899
array :key_topics, description: "Main topics discussed in the document" do
99100
string
100101
end
101-
102+
102103
array :important_dates, required: false, description: "Significant dates mentioned in the document" do
103104
string format: "date"
104105
end
105-
106+
106107
array :entities, required: false, description: "Named entities found in the document" do
107108
object do
108109
string :name
109110
string :type, enum: ["person", "organization", "location", "other"]
110111
end
111112
end
112113
end
113-
RUBY
114+
RUBY
115+
end
114116
end
115117
end
116118
end

lib/ruby_llm/template/railtie.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
module RubyLLM
44
module Template
55
class Railtie < Rails::Railtie
6+
# Configure Zeitwerk inflections for proper LLM acronym handling
7+
initializer "ruby_llm_template.inflections", before: :set_autoload_paths do |app|
8+
if app.config.respond_to?(:autoload_inflections)
9+
app.config.autoload_inflections["ruby_llm"] = "RubyLLM"
10+
end
11+
12+
# Also configure ActiveSupport inflections for consistency
13+
ActiveSupport::Inflector.inflections do |inflect|
14+
inflect.acronym "LLM"
15+
end
16+
end
17+
618
initializer "ruby_llm_template.configure" do |app|
719
# Set default template directory for Rails applications
820
RubyLLM::Template.configure do |config|

0 commit comments

Comments
 (0)