Class: Spectre::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre/prompt.rb

Defined Under Namespace

Classes: Context

Constant Summary collapse

PROMPTS_PATH =
File.join(Dir.pwd, 'app', 'spectre', 'prompts')

Class Method Summary collapse

Class Method Details

.render(template:, locals: {}) ⇒ String

Render a prompt by reading and rendering the YAML template

Parameters:

  • template (String)

    The path to the template file, formatted as ‘type/prompt’ (e.g., ‘rag/system’)

  • locals (Hash) (defaults to: {})

    Variables to be passed to the template for rendering

Returns:

  • (String)

    Rendered prompt



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spectre/prompt.rb', line 16

def self.render(template:, locals: {})
  type, prompt = split_template(template)
  file_path = prompt_file_path(type, prompt)

  raise "Prompt file not found: #{file_path}" unless File.exist?(file_path)

  template_content = File.read(file_path)
  erb_template = ERB.new(template_content)

  context = Context.new(locals)
  rendered_prompt = erb_template.result(context.get_binding)

  YAML.safe_load(rendered_prompt)[prompt]
end