Class: Phronomy::Agent::Context::Instruction::PromptTemplate
- Inherits:
-
Object
- Object
- Phronomy::Agent::Context::Instruction::PromptTemplate
- Includes:
- Runnable
- Defined in:
- lib/phronomy/agent/context/instruction/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.
34 35 36 37 |
# File 'lib/phronomy/agent/context/instruction/prompt_template.rb', line 34 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.
29 30 31 |
# File 'lib/phronomy/agent/context/instruction/prompt_template.rb', line 29 def system_template @system_template end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
29 30 31 |
# File 'lib/phronomy/agent/context/instruction/prompt_template.rb', line 29 def template @template end |
Instance Method Details
#format(**variables) ⇒ String
Substitute all {var} placeholders in the human template.
44 45 46 |
# File 'lib/phronomy/agent/context/instruction/prompt_template.rb', line 44 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.
54 55 56 |
# File 'lib/phronomy/agent/context/instruction/prompt_template.rb', line 54 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 }.
64 65 66 67 68 69 70 |
# File 'lib/phronomy/agent/context/instruction/prompt_template.rb', line 64 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.
76 77 78 79 80 |
# File 'lib/phronomy/agent/context/instruction/prompt_template.rb', line 76 def variables names = @template.scan(PLACEHOLDER).flatten names += @system_template.scan(PLACEHOLDER).flatten if @system_template names.map(&:to_sym).uniq end |