Class: NitroIntelligence::Observability::Prompt

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

Constant Summary collapse

VARIABLE_REGEX =
/\{\{([a-zA-Z0-9_]+)\}\}/

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4

def config
  @config
end

#labelsObject (readonly)

Returns the value of attribute labels.



4
5
6
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4

def labels
  @labels
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4

def name
  @name
end

#promptObject (readonly)

Returns the value of attribute prompt.



4
5
6
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4

def prompt
  @prompt
end

#tagsObject (readonly)

Returns the value of attribute tags.



4
5
6
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4

def tags
  @tags
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/nitro_intelligence/observability/prompt.rb', line 4

def type
  @type
end

#versionObject (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 |message|
    message[:content] = replace_variables(message[:content], **replacements)
    message
  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"
    messages.prepend({ role: "system", content: compile(**variables) })
  elsif @type == "chat"
    compile(**variables) + messages
  end
end

#variablesObject



40
41
42
43
44
45
46
# File 'lib/nitro_intelligence/observability/prompt.rb', line 40

def variables
  messages = @type == "text" ? [@prompt] : @prompt.pluck(:content)

  messages.map do |message|
    message.scan(VARIABLE_REGEX).flatten.map(&:to_sym)
  end.flatten
end