Class: Luo::PromptTemplate

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = nil, text: nil) ⇒ PromptTemplate

Returns a new instance of PromptTemplate.



5
6
7
8
9
10
11
12
# File 'lib/luo/prompt_template.rb', line 5

def initialize(file_path = nil, text: nil)
  @file_path = file_path
  if text.nil? && !@file_path.nil?
    @template = ERB.new(File.read(@file_path), trim_mode: '-')
  else
    @template = ERB.new(text, trim_mode: '-')
  end
end

Class Method Details

.create(file_path) ⇒ Object



33
34
35
# File 'lib/luo/prompt_template.rb', line 33

def create(file_path)
  self.new(file_path)
end

.load_template(file_name) ⇒ Object



37
38
39
# File 'lib/luo/prompt_template.rb', line 37

def load_template(file_name)
  self.new File.join(__dir__, '../../templates', 'prompts', file_name)
end

Instance Method Details

#render(data = {}) ⇒ Object



14
15
16
17
# File 'lib/luo/prompt_template.rb', line 14

def render(data = {})
  context = data.is_a?(Hash) ? data : data.each_slice(2).to_h
  @template.result(context_binding(context))
end

#resultObject



19
20
21
# File 'lib/luo/prompt_template.rb', line 19

def result()
  @template.result(binding)
end