Class: RubyLLM::Registry::Prompt
- Inherits:
-
Object
- Object
- RubyLLM::Registry::Prompt
- Defined in:
- lib/ruby_llm/registry/prompt.rb
Overview
Represents a loaded prompt template plus its metadata.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#required_vars ⇒ Object
readonly
Returns the value of attribute required_vars.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #diff(other) ⇒ Object
- #export(format: :markdown) ⇒ Object
-
#initialize(name:, namespace:, version:, body:, source_path:, labels: [], metadata: {}, required_vars: []) ⇒ Prompt
constructor
A new instance of Prompt.
- #path ⇒ Object
- #render(context = nil, **kwargs) ⇒ Object
- #to_h ⇒ Object
- #to_message(role: :system, context: nil) ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/ruby_llm/registry/prompt.rb', line 9 def body @body end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
9 10 11 |
# File 'lib/ruby_llm/registry/prompt.rb', line 9 def labels @labels end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
9 10 11 |
# File 'lib/ruby_llm/registry/prompt.rb', line 9 def @metadata end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/ruby_llm/registry/prompt.rb', line 9 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
9 10 11 |
# File 'lib/ruby_llm/registry/prompt.rb', line 9 def namespace @namespace end |
#required_vars ⇒ Object (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_path ⇒ Object (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 |
#version ⇒ Object (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 |
#path ⇒ Object
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_h ⇒ Object
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 (role: :system, context: nil, **) { role: role, content: render(context, **) } end |