Class: Brute::PromptTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/brute/prompt_template.rb

Overview

An ERB-backed system-prompt object for Middleware::SystemPrompt — the open alternative to Brute::SystemPrompt's built-in section stacks. You bring a template and named values; every keyword becomes an attr_accessor and an ERB local of the same name:

prompt = Brute::PromptTemplate.new(
"prompt.erb",
identity: "You are Pico.",
memory:   -> { File.read("memory/MEMORY.md") },   # zero-arity proc
env:      ->(ctx) { Brute::Prompts::Environment.call(ctx) },
)
prompt.identity = "You are Brute."   # attr_accessor per section

Brute.agent.use(Brute::Middleware::SystemPrompt, system_prompt: prompt)

Proc values are re-evaluated on every prepare (zero-arity procs are called with no arguments, others receive the turn ctx), and a template path is re-read from disk each time — so file-backed sections hot-reload between turns. The prepare(ctx) -> Result(#empty?, #to_s) contract is what Middleware::SystemPrompt expects.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(template, **sections) ⇒ PromptTemplate

Returns a new instance of PromptTemplate.



34
35
36
37
38
# File 'lib/brute/prompt_template.rb', line 34

def initialize(template, **sections)
  @template = template
  @section_keys = []
  sections.each { |key, value| self[key] = value }
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
# File 'lib/brute/prompt_template.rb', line 40

def [](key)
  public_send(key)
end

#[]=(key, value) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/brute/prompt_template.rb', line 44

def []=(key, value)
  unless respond_to?(key)
    singleton_class.class_eval { attr_accessor key }
    @section_keys << key
  end
  public_send("#{key}=", value)
end

#prepare(ctx = {}) ⇒ Object

Called once per turn by Middleware::SystemPrompt.



53
54
55
# File 'lib/brute/prompt_template.rb', line 53

def prepare(ctx = {})
  Result.new(render(locals(ctx)))
end