Class: Phronomy::PromptTemplate

Inherits:
Object
  • Object
show all
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.

Examples:

Simple human template

t = Phronomy::PromptTemplate.new(template: "Translate to {{lang}}: {{text}}")
t.format(lang: "French", text: "Hello")
# => "Translate to French: Hello"

With a system template

t = Phronomy::PromptTemplate.new(
  template: "{{question}}",
  system_template: "You are a {{role}} assistant."
)
t.format_system(role: "helpful")
# => "You are a helpful assistant."

Constant Summary collapse

PLACEHOLDER =
/\{\{(\w+)\}\}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Runnable

#batch, #stream, #trace

Constructor Details

#initialize(template:, system_template: nil) ⇒ PromptTemplate

Returns a new instance of PromptTemplate.

Parameters:

  • template (String)

    human message template with {var} placeholders

  • system_template (String, nil) (defaults to: nil)

    optional system message template



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_templateObject (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

#templateObject (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.

Parameters:

  • variables (Hash{Symbol => String})

Returns:

  • (String)


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.

Parameters:

  • variables (Hash{Symbol => String})

Returns:

  • (String, nil)


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 }.

Parameters:

  • input (Hash{Symbol => String})

Returns:

  • (Hash)


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

#variablesArray<Symbol>

Returns the list of placeholder names found in both templates.

Returns:

  • (Array<Symbol>)


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