|
2 | 2 |
|
3 | 3 | require "rails/generators" |
4 | 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 |
| 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 |
17 | 26 |
|
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" |
25 | 29 |
|
26 | | - def create_template_directory |
27 | | - empty_directory "app/prompts" |
| 30 | + create_file "app/prompts/.keep", "" |
28 | 31 |
|
29 | | - create_file "app/prompts/.keep", "" |
| 32 | + # Create an example template |
| 33 | + create_example_template |
| 34 | + end |
30 | 35 |
|
31 | | - # Create an example template |
32 | | - create_example_template |
33 | | - end |
| 36 | + def show_readme |
| 37 | + say <<~MESSAGE |
34 | 38 |
|
35 | | - def show_readme |
36 | | - say <<~MESSAGE |
37 | | - |
38 | 39 | RubyLLM Template has been installed! |
39 | | - |
| 40 | +
|
40 | 41 | Prompts directory: app/prompts/ |
41 | 42 | Configuration: config/initializers/ruby_llm_template.rb |
42 | | - |
| 43 | +
|
43 | 44 | Example usage: |
44 | 45 | RubyLLM.chat.with_template(:extract_metadata, document: @document).complete |
45 | | - |
| 46 | +
|
46 | 47 | Template structure: |
47 | 48 | app/prompts/extract_metadata/ |
48 | 49 | ├── system.txt.erb # System message |
49 | 50 | ├── user.txt.erb # User prompt |
50 | 51 | ├── assistant.txt.erb # Assistant message (optional) |
51 | 52 | └── schema.rb # RubyLLM::Schema definition (optional) |
52 | | - |
| 53 | +
|
53 | 54 | Get started by creating your first template! |
54 | | - MESSAGE |
55 | | - end |
| 55 | + MESSAGE |
| 56 | + end |
56 | 57 |
|
57 | | - private |
| 58 | + private |
58 | 59 |
|
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 |
62 | 63 |
|
63 | | - create_file "#{example_dir}/system.txt.erb", <<~ERB |
| 64 | + create_file "#{example_dir}/system.txt.erb", <<~ERB |
64 | 65 | You are an expert document analyzer. Your task is to extract metadata from the provided document. |
65 | | - |
| 66 | +
|
66 | 67 | Please analyze the document carefully and extract relevant information such as: |
67 | 68 | - Document type |
68 | 69 | - Key topics |
69 | 70 | - Important dates |
70 | 71 | - Main entities mentioned |
71 | | - |
| 72 | +
|
72 | 73 | Provide your analysis in a structured format. |
73 | | - ERB |
| 74 | + ERB |
74 | 75 |
|
75 | | - create_file "#{example_dir}/user.txt.erb", <<~ERB |
| 76 | + create_file "#{example_dir}/user.txt.erb", <<~ERB |
76 | 77 | Please analyze the following document and extract its metadata: |
77 | | - |
| 78 | +
|
78 | 79 | <% if defined?(document) && document %> |
79 | 80 | Document: <%= document %> |
80 | 81 | <% else %> |
81 | 82 | [Document content will be provided here] |
82 | 83 | <% end %> |
83 | | - |
| 84 | +
|
84 | 85 | <% if defined?(additional_context) && additional_context %> |
85 | 86 | Additional context: <%= additional_context %> |
86 | 87 | <% end %> |
87 | | - ERB |
| 88 | + ERB |
88 | 89 |
|
89 | | - create_file "#{example_dir}/schema.rb", <<~RUBY |
| 90 | + create_file "#{example_dir}/schema.rb", <<~RUBY |
90 | 91 | # frozen_string_literal: true |
91 | 92 |
|
92 | 93 | # Schema definition using RubyLLM::Schema DSL |
93 | 94 | # See: https://github.com/danielfriis/ruby_llm-schema |
94 | 95 |
|
95 | 96 | RubyLLM::Schema.create do |
96 | 97 | string :document_type, description: "The type of document (e.g., report, article, email)" |
97 | | - |
| 98 | +
|
98 | 99 | array :key_topics, description: "Main topics discussed in the document" do |
99 | 100 | string |
100 | 101 | end |
101 | | - |
| 102 | +
|
102 | 103 | array :important_dates, required: false, description: "Significant dates mentioned in the document" do |
103 | 104 | string format: "date" |
104 | 105 | end |
105 | | - |
| 106 | +
|
106 | 107 | array :entities, required: false, description: "Named entities found in the document" do |
107 | 108 | object do |
108 | 109 | string :name |
109 | 110 | string :type, enum: ["person", "organization", "location", "other"] |
110 | 111 | end |
111 | 112 | end |
112 | 113 | end |
113 | | - RUBY |
| 114 | + RUBY |
| 115 | + end |
114 | 116 | end |
115 | 117 | end |
116 | 118 | end |
|
0 commit comments