ruby_llm-template 0.1.4 → 0.1.6

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: 49596291defa8cac6cb6088736c1403b7e7612aa5948da276dba410ae114ed27
4
- data.tar.gz: '09b7dbfb13a4535aebd3c70df2353a9dc73597eacc160aea2643dc3272774e4b'
3
+ metadata.gz: 8af32c4910100355c57302be6fb145bc6463fa3c39105533bda9a6002709bd1c
4
+ data.tar.gz: a55213fce936c617a83db65eea1aeb54584fcafae203a54a8736c41c17d25c33
5
5
  SHA512:
6
- metadata.gz: ed0c3650f34970ac5ecd0ed30c48196cdcb0a81283828431ab17852cde489591e210c9cb52c68e42e89b12ce5565b6aca399a1838ac38cfe3516d1e5d0912fba
7
- data.tar.gz: 30520cba8a4e3aad1edbf971651310098f8f7c9c2f1367023337010537f6dd2a668aa94b6bfe5a53de2ba2af6e03d3e6d3628db5282d738422928e86d3c88574
6
+ metadata.gz: 8831170d3d2a9dc5feacfb0332cbe9c232a2383c970e1b50de51429e2fe0eddf87ee70cbaf85bc2fec67430082e2a3f0b40d7d2d62187ff454a4198a4d0be7d8
7
+ data.tar.gz: 9a16c7e87550bfd9d5e0cd796543429933a027641fdc99c770337fc97d2e37e940b216404e1a88d19fb3418405ac86417fed1755d0056711fabd2f6261998afc
data/README.md CHANGED
@@ -7,12 +7,6 @@
7
7
  Organize prompts into easy-to-use templates for [RubyLLM](https://github.com/crmne/ruby_llm).
8
8
 
9
9
  ```ruby
10
- chat = RubyLLM.chat
11
- chat.with_template(:extract_metadata, document: @document).complete
12
-
13
- # ----------------------------------
14
- # Retrieves the following files:
15
- # ----------------------------------
16
10
  # prompts/
17
11
  # extract_metadata/
18
12
  # ├── system.txt.erb # System message
@@ -20,6 +14,8 @@ chat.with_template(:extract_metadata, document: @document).complete
20
14
  # ├── assistant.txt.erb # Assistant message (optional)
21
15
  # └── schema.rb # RubyLLM::Schema definition (optional)
22
16
 
17
+ chat = RubyLLM.chat
18
+ chat.with_template(:extract_metadata, document: @document).complete
23
19
  ```
24
20
 
25
21
  ## Features
@@ -115,28 +111,24 @@ end
115
111
  ### 3. Use the Template
116
112
 
117
113
  ```ruby
118
- # Basic usage
119
- RubyLLM.chat.with_template(:extract_metadata, document: @document).complete
114
+ chat = RubyLLM.chat
115
+ chat.with_template(:extract_metadata, document: @document, additional_context: "Focus on technical details").complete
120
116
 
121
- # With context variables
122
- RubyLLM.chat.with_template(:extract_metadata,
123
- document: @document,
124
- additional_context: "Focus on technical details"
125
- ).complete
117
+ # Under the hood, RubyLLM::Template renders the templates with the context variables
118
+ # and applies them to the chat instance using native RubyLLM methods:
126
119
 
127
- # Chaining with other RubyLLM methods
128
- RubyLLM.chat
129
- .with_template(:extract_metadata, document: @document)
130
- .with_model("gpt-4")
131
- .complete
120
+ # chat.add_message(:system, rendered_system_message)
121
+ # chat.add_message(:user, rendered_user_message)
122
+ # chat.add_schema(instantiated_schema)
132
123
  ```
133
124
 
125
+
134
126
  ## Configuration
135
127
 
136
128
  ### Non-Rails Applications
137
129
 
138
130
  ```ruby
139
- RubyLlm::Template.configure do |config|
131
+ RubyLLM::Template.configure do |config|
140
132
  config.template_directory = "/path/to/your/prompts"
141
133
  end
142
134
  ```
@@ -146,7 +138,7 @@ end
146
138
  The gem automatically configures itself to use `Rails.root.join("app", "prompts")`, but you can override this in `config/initializers/ruby_llm_template.rb`:
147
139
 
148
140
  ```ruby
149
- RubyLlm::Template.configure do |config|
141
+ RubyLLM::Template.configure do |config|
150
142
  config.template_directory = Rails.root.join("app", "ai_prompts")
151
143
  end
152
144
  ```
@@ -166,7 +158,13 @@ Templates are processed in order: system → user → assistant → schema
166
158
 
167
159
  All context variables passed to `with_template` are available in your ERB templates:
168
160
 
161
+ ```ruby
162
+ chat = RubyLLM.chat
163
+ chat.with_template(:message, name: "Alice", urgent: true, documents: @documents)
164
+ ```
165
+
169
166
  ```erb
167
+ <!-- /prompts/message/user.txt.erb -->
170
168
  Hello <%= name %>!
171
169
 
172
170
  <% if urgent %>
@@ -249,7 +247,7 @@ The gem provides clear error messages for common issues:
249
247
  ```ruby
250
248
  begin
251
249
  RubyLLM.chat.with_template(:extract_metadata).complete
252
- rescue RubyLlm::Template::Error => e
250
+ rescue RubyLLM::Template::Error => e
253
251
  puts e.message
254
252
  # "Template 'extract_metadata' not found in /path/to/prompts"
255
253
  # "Schema file 'extract_metadata/schema.rb' found but RubyLLM::Schema gem is not installed"
@@ -7,7 +7,7 @@
7
7
  require_relative "../lib/ruby_llm/template"
8
8
 
9
9
  # Configure template directory
10
- RubyLlm::Template.configure do |config|
10
+ RubyLLM::Template.configure do |config|
11
11
  config.template_directory = File.join(__dir__, "prompts")
12
12
  end
13
13
 
@@ -91,7 +91,7 @@ puts "=" * 40
91
91
 
92
92
  # Mock chat object that demonstrates the extension
93
93
  class MockChat
94
- include RubyLlm::Template::ChatExtension
94
+ include RubyLLM::Template::ChatExtension
95
95
 
96
96
  def initialize
97
97
  @messages = []
@@ -135,7 +135,7 @@ begin
135
135
  document: "Q3 Financial Report: Revenue increased 15% to $2.3M. Key challenges include supply chain delays affecting Q4 projections.",
136
136
  additional_context: "Focus on financial metrics and future outlook",
137
137
  focus_areas: ["revenue", "challenges", "projections"]).complete
138
- rescue RubyLlm::Template::Error => e
138
+ rescue RubyLLM::Template::Error => e
139
139
  puts "❌ Error: #{e.message}"
140
140
  end
141
141
 
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+
5
+ module RubyLLM
6
+ module Template
7
+ module Generators
8
+ class InstallGenerator < Rails::Generators::Base
9
+ namespace "ruby_llm_template:install"
10
+ desc "Install RubyLLM Template system"
11
+
12
+ def self.source_root
13
+ @source_root ||= File.expand_path("templates", __dir__)
14
+ end
15
+
16
+ def create_initializer
17
+ create_file "config/initializers/ruby_llm_template.rb", <<~RUBY
18
+ # frozen_string_literal: true
19
+
20
+ RubyLLM::Template.configure do |config|
21
+ # Set the directory where your prompts are stored
22
+ # Default: Rails.root.join("app", "prompts")
23
+ # config.template_directory = Rails.root.join("app", "prompts")
24
+ end
25
+ RUBY
26
+ end
27
+
28
+ def create_template_directory
29
+ empty_directory "app/prompts"
30
+
31
+ create_file "app/prompts/.keep", ""
32
+
33
+ # Create an example template
34
+ create_example_template
35
+ end
36
+
37
+ def show_readme
38
+ say <<~MESSAGE
39
+
40
+ RubyLLM Template has been installed!
41
+
42
+ Prompts directory: app/prompts/
43
+ Configuration: config/initializers/ruby_llm_template.rb
44
+
45
+ Example usage:
46
+ RubyLLM.chat.with_template(:extract_metadata, document: @document).complete
47
+
48
+ Template structure:
49
+ app/prompts/extract_metadata/
50
+ ├── system.txt.erb # System message
51
+ ├── user.txt.erb # User prompt
52
+ ├── assistant.txt.erb # Assistant message (optional)
53
+ └── schema.rb # RubyLLM::Schema definition (optional)
54
+
55
+ Get started by creating your first template!
56
+ MESSAGE
57
+ end
58
+
59
+ private
60
+
61
+ def create_example_template
62
+ example_dir = "app/prompts/extract_metadata"
63
+ empty_directory example_dir
64
+
65
+ create_file "#{example_dir}/system.txt.erb", <<~ERB
66
+ You are an expert document analyzer. Your task is to extract metadata from the provided document.
67
+
68
+ Please analyze the document carefully and extract relevant information such as:
69
+ - Document type
70
+ - Key topics
71
+ - Important dates
72
+ - Main entities mentioned
73
+
74
+ Provide your analysis in a structured format.
75
+ ERB
76
+
77
+ create_file "#{example_dir}/user.txt.erb", <<~ERB
78
+ Please analyze the following document and extract its metadata:
79
+
80
+ <% if defined?(document) && document %>
81
+ Document: <%= document %>
82
+ <% else %>
83
+ [Document content will be provided here]
84
+ <% end %>
85
+
86
+ <% if defined?(additional_context) && additional_context %>
87
+ Additional context: <%= additional_context %>
88
+ <% end %>
89
+ ERB
90
+
91
+ create_file "#{example_dir}/schema.rb", <<~RUBY
92
+ # frozen_string_literal: true
93
+
94
+ # Schema definition using RubyLLM::Schema DSL
95
+ # See: https://github.com/danielfriis/ruby_llm-schema
96
+
97
+ RubyLLM::Schema.create do
98
+ string :document_type, description: "The type of document (e.g., report, article, email)"
99
+
100
+ array :key_topics, description: "Main topics discussed in the document" do
101
+ string
102
+ end
103
+
104
+ array :important_dates, required: false, description: "Significant dates mentioned in the document" do
105
+ string format: "date"
106
+ end
107
+
108
+ array :entities, required: false, description: "Named entities found in the document" do
109
+ object do
110
+ string :name
111
+ string :type, enum: ["person", "organization", "location", "other"]
112
+ end
113
+ end
114
+ end
115
+ RUBY
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module RubyLlm
3
+ module RubyLLM
4
4
  module Template
5
5
  module ChatExtension
6
6
  def with_template(template_name, context = {})
7
- loader = RubyLlm::Template::Loader.new(template_name)
7
+ loader = RubyLLM::Template::Loader.new(template_name)
8
8
 
9
9
  unless loader.template_exists?
10
- raise RubyLlm::Template::Error, "Template '#{template_name}' not found in #{RubyLlm::Template.configuration.template_directory}"
10
+ raise RubyLLM::Template::Error, "Template '#{template_name}' not found in #{RubyLLM::Template.configuration.template_directory}"
11
11
  end
12
12
 
13
13
  # Apply templates in a specific order to maintain conversation flow
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module RubyLlm
3
+ module RubyLLM
4
4
  module Template
5
5
  class Configuration
6
6
  attr_writer :template_directory
@@ -9,14 +9,14 @@ rescue LoadError
9
9
  # RubyLLM::Schema not available, schema.rb files won't work
10
10
  end
11
11
 
12
- module RubyLlm
12
+ module RubyLLM
13
13
  module Template
14
14
  class Loader
15
15
  SUPPORTED_ROLES = %w[system user assistant schema].freeze
16
16
 
17
17
  def initialize(template_name, template_directory: nil)
18
18
  @template_name = template_name.to_s
19
- @template_directory = Pathname.new(template_directory || RubyLlm::Template.configuration.template_directory)
19
+ @template_directory = Pathname.new(template_directory || RubyLLM::Template.configuration.template_directory)
20
20
  @template_path = @template_directory.join(@template_name)
21
21
  end
22
22
 
@@ -1,11 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module RubyLlm
3
+ module RubyLLM
4
4
  module Template
5
5
  class Railtie < Rails::Railtie
6
+ # Register generators
7
+ generators do
8
+ require_relative "../../generators/ruby_llm/template/install_generator"
9
+ end
10
+
6
11
  initializer "ruby_llm_template.configure" do |app|
7
12
  # Set default template directory for Rails applications
8
- RubyLlm::Template.configure do |config|
13
+ RubyLLM::Template.configure do |config|
9
14
  config.template_directory ||= app.root.join("app", "prompts")
10
15
  end
11
16
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module RubyLlm
3
+ module RubyLLM
4
4
  module Template
5
- VERSION = "0.1.4"
5
+ VERSION = "0.1.6"
6
6
  end
7
7
  end
@@ -13,7 +13,7 @@ rescue LoadError
13
13
  # Rails not available
14
14
  end
15
15
 
16
- module RubyLlm
16
+ module RubyLLM
17
17
  module Template
18
18
  class Error < StandardError; end
19
19
 
@@ -41,7 +41,7 @@ begin
41
41
 
42
42
  module RubyLLMChatTemplateExtension
43
43
  def self.extended(base)
44
- base.extend(RubyLlm::Template::ChatExtension)
44
+ base.extend(RubyLLM::Template::ChatExtension)
45
45
  end
46
46
  end
47
47
 
@@ -49,7 +49,7 @@ begin
49
49
  module RubyLLMTemplateHook
50
50
  def chat(*args, **kwargs)
51
51
  chat_instance = super
52
- chat_instance.extend(RubyLlm::Template::ChatExtension)
52
+ chat_instance.extend(RubyLLM::Template::ChatExtension)
53
53
  chat_instance
54
54
  end
55
55
  end
@@ -1,4 +1,4 @@
1
- module RubyLlm
1
+ module RubyLLM
2
2
  module Template
3
3
  VERSION: String
4
4
  # See the writing guide of rbs: https://github.com/ruby/rbs#guides
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_llm-template
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Friis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-09-03 00:00:00.000000000 Z
11
+ date: 2025-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby_llm
@@ -68,7 +68,7 @@ files:
68
68
  - README.md
69
69
  - Rakefile
70
70
  - examples/basic_usage.rb
71
- - lib/generators/ruby_llm_template/install_generator.rb
71
+ - lib/generators/ruby_llm/template/install_generator.rb
72
72
  - lib/ruby_llm/template.rb
73
73
  - lib/ruby_llm/template/chat_extension.rb
74
74
  - lib/ruby_llm/template/configuration.rb
@@ -1,117 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails/generators"
4
-
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
17
-
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
25
-
26
- def create_template_directory
27
- empty_directory "app/prompts"
28
-
29
- create_file "app/prompts/.keep", ""
30
-
31
- # Create an example template
32
- create_example_template
33
- end
34
-
35
- def show_readme
36
- say <<~MESSAGE
37
-
38
- RubyLLM Template has been installed!
39
-
40
- Prompts directory: app/prompts/
41
- Configuration: config/initializers/ruby_llm_template.rb
42
-
43
- Example usage:
44
- RubyLLM.chat.with_template(:extract_metadata, document: @document).complete
45
-
46
- Template structure:
47
- app/prompts/extract_metadata/
48
- ├── system.txt.erb # System message
49
- ├── user.txt.erb # User prompt
50
- ├── assistant.txt.erb # Assistant message (optional)
51
- └── schema.rb # RubyLLM::Schema definition (optional)
52
-
53
- Get started by creating your first template!
54
- MESSAGE
55
- end
56
-
57
- private
58
-
59
- def create_example_template
60
- example_dir = "app/prompts/extract_metadata"
61
- empty_directory example_dir
62
-
63
- create_file "#{example_dir}/system.txt.erb", <<~ERB
64
- You are an expert document analyzer. Your task is to extract metadata from the provided document.
65
-
66
- Please analyze the document carefully and extract relevant information such as:
67
- - Document type
68
- - Key topics
69
- - Important dates
70
- - Main entities mentioned
71
-
72
- Provide your analysis in a structured format.
73
- ERB
74
-
75
- create_file "#{example_dir}/user.txt.erb", <<~ERB
76
- Please analyze the following document and extract its metadata:
77
-
78
- <% if defined?(document) && document %>
79
- Document: <%= document %>
80
- <% else %>
81
- [Document content will be provided here]
82
- <% end %>
83
-
84
- <% if defined?(additional_context) && additional_context %>
85
- Additional context: <%= additional_context %>
86
- <% end %>
87
- ERB
88
-
89
- create_file "#{example_dir}/schema.rb", <<~RUBY
90
- # frozen_string_literal: true
91
-
92
- # Schema definition using RubyLLM::Schema DSL
93
- # See: https://github.com/danielfriis/ruby_llm-schema
94
-
95
- RubyLLM::Schema.create do
96
- string :document_type, description: "The type of document (e.g., report, article, email)"
97
-
98
- array :key_topics, description: "Main topics discussed in the document" do
99
- string
100
- end
101
-
102
- array :important_dates, required: false, description: "Significant dates mentioned in the document" do
103
- string format: "date"
104
- end
105
-
106
- array :entities, required: false, description: "Named entities found in the document" do
107
- object do
108
- string :name
109
- string :type, enum: ["person", "organization", "location", "other"]
110
- end
111
- end
112
- end
113
- RUBY
114
- end
115
- end
116
- end
117
- end