Class: RubyLLM::Registry::Prompt

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

Overview

Represents a loaded prompt template plus its metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, namespace:, version:, body:, source_path:, labels: [], metadata: {}, required_vars: []) ⇒ Prompt

Returns a new instance of Prompt.



11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_llm/registry/prompt.rb', line 11

def initialize(name:, namespace:, version:, body:, source_path:, labels: [], metadata: {}, required_vars: [])
  @name = name.to_s
  @namespace = namespace.to_s
  @version = version.is_a?(Version) ? version : Version.parse(version)
  @body = body.to_s
  @source_path = source_path
  @labels = Array(labels).compact.map(&:to_sym)
  @metadata = symbolize_keys()
  @required_vars = Array(required_vars).compact.map(&:to_sym)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/ruby_llm/registry/prompt.rb', line 9

def body
  @body
end

#labelsObject (readonly)

Returns the value of attribute labels.



9
10
11
# File 'lib/ruby_llm/registry/prompt.rb', line 9

def labels
  @labels
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/ruby_llm/registry/prompt.rb', line 9

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/ruby_llm/registry/prompt.rb', line 9

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



9
10
11
# File 'lib/ruby_llm/registry/prompt.rb', line 9

def namespace
  @namespace
end

#required_varsObject (readonly)

Returns the value of attribute required_vars.



9
10
11
# File 'lib/ruby_llm/registry/prompt.rb', line 9

def required_vars
  @required_vars
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



9
10
11
# File 'lib/ruby_llm/registry/prompt.rb', line 9

def source_path
  @source_path
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/ruby_llm/registry/prompt.rb', line 9

def version
  @version
end

Instance Method Details

#diff(other) ⇒ Object



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

def diff(other)
  other_prompt = other.is_a?(Prompt) ? other : Importer.new(other, format: :auto).to_prompt
  Comparison.new(self, other_prompt)
end

#export(format: :markdown) ⇒ Object



36
37
38
# File 'lib/ruby_llm/registry/prompt.rb', line 36

def export(format: :markdown)
  Exporter.new(self).public_send(exporter_method(format))
end

#pathObject



22
23
24
# File 'lib/ruby_llm/registry/prompt.rb', line 22

def path
  [namespace, name].reject(&:empty?).join("/")
end

#render(context = nil, **kwargs) ⇒ Object



26
27
28
29
30
# File 'lib/ruby_llm/registry/prompt.rb', line 26

def render(context = nil, **kwargs)
  values = normalize_context(context, kwargs)
  validate_required_vars!(values)
  ERB.new(body, trim_mode: "-").result(RenderContext.new(values).instance_eval { binding })
end

#to_hObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_llm/registry/prompt.rb', line 45

def to_h
  {
    path: path,
    namespace: namespace,
    name: name,
    version: version.to_s,
    labels: labels,
    metadata: ,
    required_vars: required_vars,
    source_path: source_path,
    body: body
  }
end

#to_message(role: :system, context: nil) ⇒ Object



32
33
34
# File 'lib/ruby_llm/registry/prompt.rb', line 32

def to_message(role: :system, context: nil, **)
  { role: role, content: render(context, **) }
end