Class: NitroIntelligence::Observability::Prompt
- Inherits:
-
Object
- Object
- NitroIntelligence::Observability::Prompt
- Defined in:
- lib/nitro_intelligence/observability/prompt.rb
Constant Summary collapse
- VARIABLE_REGEX =
/\{\{([a-zA-Z0-9_]+)\}\}/
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#compile(**replacements) ⇒ Object
Returns prompt “content” from API with prompt variables replaced Prompt “content” will either be a string or an array of hashes based on prompt “type” (“text” or “chat”).
-
#initialize(name:, type:, prompt:, version:, **extra_args) ⇒ Prompt
constructor
A new instance of Prompt.
-
#interpolate(messages:, variables:) ⇒ Object
Takes provided chat messages and inserts the compiled prompt into the correct position based on prompt “type” (“text” or “chat”).
- #variables ⇒ Object
Constructor Details
#initialize(name:, type:, prompt:, version:, **extra_args) ⇒ Prompt
Returns a new instance of Prompt.
8 9 10 11 12 13 14 15 16 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 8 def initialize(name:, type:, prompt:, version:, **extra_args) @name = name @type = type @prompt = prompt @version = version @config = extra_args[:config] || {} @labels = extra_args[:labels] || [] @tags = extra_args[:tags] || [] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4 def config @config end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
4 5 6 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4 def labels @labels end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4 def name @name end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
4 5 6 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4 def prompt @prompt end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
4 5 6 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4 def @tags end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
4 5 6 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4 def type @type end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
4 5 6 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4 def version @version end |
Instance Method Details
#compile(**replacements) ⇒ Object
Returns prompt “content” from API with prompt variables replaced Prompt “content” will either be a string or an array of hashes based on prompt “type” (“text” or “chat”)
21 22 23 24 25 26 27 28 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 21 def compile(**replacements) return replace_variables(@prompt, **replacements) if @type == "text" @prompt.map do || [:content] = replace_variables([:content], **replacements) end end |
#interpolate(messages:, variables:) ⇒ Object
Takes provided chat messages and inserts the compiled prompt into the correct position based on prompt “type” (“text” or “chat”)
32 33 34 35 36 37 38 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 32 def interpolate(messages:, variables:) if @type == "text" .prepend({ role: "system", content: compile(**variables) }) elsif @type == "chat" compile(**variables) + end end |
#variables ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/nitro_intelligence/observability/prompt.rb', line 40 def variables = @type == "text" ? [@prompt] : @prompt.pluck(:content) .map do || .scan(VARIABLE_REGEX).flatten.map(&:to_sym) end.flatten end |