Class: Langfuse::PromptTemplate

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template:, input_variables: []) ⇒ PromptTemplate

Returns a new instance of PromptTemplate.



134
135
136
137
# File 'lib/langfuse/prompt.rb', line 134

def initialize(template:, input_variables: [])
  @template = template
  @input_variables = input_variables
end

Instance Attribute Details

#input_variablesObject (readonly)

Returns the value of attribute input_variables.



132
133
134
# File 'lib/langfuse/prompt.rb', line 132

def input_variables
  @input_variables
end

#templateObject (readonly)

Returns the value of attribute template.



132
133
134
# File 'lib/langfuse/prompt.rb', line 132

def template
  @template
end

Class Method Details

.extract_variables(text) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/langfuse/prompt.rb', line 153

def self.extract_variables(text)
  variables = []

  # Match {{variable}} format
  text.scan(/\{\{(\w+)\}\}/) do |match|
    variables << match[0]
  end

  # Match {variable} format
  text.scan(/\{(\w+)\}/) do |match|
    variables << match[0] unless variables.include?(match[0])
  end

  variables
end

.from_template(template) ⇒ Object



148
149
150
151
# File 'lib/langfuse/prompt.rb', line 148

def self.from_template(template)
  variables = extract_variables(template)
  new(template: template, input_variables: variables)
end

Instance Method Details

#format(variables = {}) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/langfuse/prompt.rb', line 139

def format(variables = {})
  compiled = @template.dup
  variables.each do |key, value|
    compiled.gsub!("{{#{key}}}", value.to_s)
    compiled.gsub!("{#{key}}", value.to_s)
  end
  compiled
end