Class: PromptEngine::ParameterParser

Inherits:
Object
  • Object
show all
Defined in:
app/models/prompt_engine/parameter_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ ParameterParser

Returns a new instance of ParameterParser.



5
6
7
# File 'app/models/prompt_engine/parameter_parser.rb', line 5

def initialize(content)
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'app/models/prompt_engine/parameter_parser.rb', line 3

def content
  @content
end

Instance Method Details

#extract_parametersObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/prompt_engine/parameter_parser.rb', line 9

def extract_parameters
  return [] if content.blank?

  parameter_names = content.scan(/\{\{([^}]+)\}\}/).flatten.map(&:strip).uniq

  parameter_names.map do |name|
    {
      name: name,
      placeholder: "{{#{name}}}",
      required: true
    }
  end
end

#has_parameters?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/prompt_engine/parameter_parser.rb', line 35

def has_parameters?
  extract_parameters.any?
end

#replace_parameters(parameters = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/prompt_engine/parameter_parser.rb', line 23

def replace_parameters(parameters = {})
  result = content.dup

  return result if parameters.nil?

  parameters.each do |key, value|
    result.gsub!("{{#{key}}}", value.to_s)
  end

  result
end