Class: Phronomy::PromptTemplate
- Inherits:
-
Object
- Object
- Phronomy::PromptTemplate
- Includes:
- Runnable
- Defined in:
- lib/phronomy/prompt_template.rb
Overview
A prompt template that substitutes {variable} placeholders in a string.
As a Runnable, #invoke accepts a Hash of variables and returns a Hash with :prompt (and optionally :system) keys.
Constant Summary collapse
- PLACEHOLDER =
/\{\{(\w+)\}\}/
Instance Attribute Summary collapse
-
#system_template ⇒ Object
readonly
Returns the value of attribute system_template.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
-
#format(**variables) ⇒ String
Substitute all {var} placeholders in the human template.
-
#format_system(**variables) ⇒ String?
Substitute all {var} placeholders in the system template.
-
#initialize(template:, system_template: nil) ⇒ PromptTemplate
constructor
A new instance of PromptTemplate.
-
#invoke(input, config: {}) ⇒ Hash
Runnable interface: accepts a Hash of variable values.
-
#variables ⇒ Array<Symbol>
Returns the list of placeholder names found in both templates.
Methods included from Runnable
Constructor Details
#initialize(template:, system_template: nil) ⇒ PromptTemplate
Returns a new instance of PromptTemplate.
30 31 32 33 |
# File 'lib/phronomy/prompt_template.rb', line 30 def initialize(template:, system_template: nil) @template = template @system_template = system_template end |
Instance Attribute Details
#system_template ⇒ Object (readonly)
Returns the value of attribute system_template.
26 27 28 |
# File 'lib/phronomy/prompt_template.rb', line 26 def system_template @system_template end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
26 27 28 |
# File 'lib/phronomy/prompt_template.rb', line 26 def template @template end |
Instance Method Details
#format(**variables) ⇒ String
Substitute all {var} placeholders in the human template.
39 40 41 |
# File 'lib/phronomy/prompt_template.rb', line 39 def format(**variables) substitute(@template, variables) end |
#format_system(**variables) ⇒ String?
Substitute all {var} placeholders in the system template. Returns nil when no system template was set.
48 49 50 |
# File 'lib/phronomy/prompt_template.rb', line 48 def format_system(**variables) @system_template && substitute(@system_template, variables) end |
#invoke(input, config: {}) ⇒ Hash
Runnable interface: accepts a Hash of variable values. Returns { prompt: String, system: String|nil }.
57 58 59 60 61 62 63 |
# File 'lib/phronomy/prompt_template.rb', line 57 def invoke(input, config: {}) vars = normalize_input(input) result = {prompt: format(**vars)} sys = format_system(**vars) result[:system] = sys if sys result end |
#variables ⇒ Array<Symbol>
Returns the list of placeholder names found in both templates.
68 69 70 71 72 |
# File 'lib/phronomy/prompt_template.rb', line 68 def variables names = @template.scan(PLACEHOLDER).flatten names += @system_template.scan(PLACEHOLDER).flatten if @system_template names.map(&:to_sym).uniq end |