Class: RubyLLM::Prompt

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

Overview

Resolves prompt templates from the application and registered engine directories.

Defined Under Namespace

Classes: Roots

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Prompt

:nodoc:



41
42
43
44
# File 'lib/ruby_llm/prompt.rb', line 41

def initialize(name) # :nodoc:
  @name = name.to_s
  @filename = @name.end_with?('.txt.erb') ? @name : "#{@name}.txt.erb"
end

Instance Attribute Details

#nameObject (readonly)

:nodoc:



39
40
41
# File 'lib/ruby_llm/prompt.rb', line 39

def name
  @name
end

Class Method Details

.render(name, **locals) ⇒ Object

:nodoc:



56
57
58
# File 'lib/ruby_llm/prompt.rb', line 56

def self.render(name, **locals) # :nodoc:
  new(name).render(**locals)
end

.rootObject

:nodoc:



65
66
67
68
69
70
71
# File 'lib/ruby_llm/prompt.rb', line 65

def self.root # :nodoc:
  if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
    Rails.root.join('app/prompts')
  else
    Pathname.new(Dir.pwd).join('app/prompts')
  end
end

.rootsObject

Returns the ordered prompt directories. Append engine directories with <<.



61
62
63
# File 'lib/ruby_llm/prompt.rb', line 61

def self.roots
  @roots ||= Roots.new { root }
end

Instance Method Details

#pathObject

:nodoc:



46
47
48
# File 'lib/ruby_llm/prompt.rb', line 46

def path # :nodoc:
  @path ||= candidates.find { |candidate| File.exist?(candidate) } || candidates.first
end

#render(**locals) ⇒ Object

:nodoc:



50
51
52
53
54
# File 'lib/ruby_llm/prompt.rb', line 50

def render(**locals) # :nodoc:
  raise PromptNotFoundError, "Prompt file not found: #{path}" unless File.exist?(path)

  ERB.new(File.read(path)).result_with_hash(locals)
end