Class: Langfuse::Evaluators::RegexEvaluator

Inherits:
BaseEvaluator show all
Defined in:
lib/langfuse/evaluation.rb

Instance Method Summary collapse

Constructor Details

#initialize(pattern:, name: 'regex', description: 'Regex evaluator') ⇒ RegexEvaluator

Returns a new instance of RegexEvaluator.



156
157
158
159
# File 'lib/langfuse/evaluation.rb', line 156

def initialize(pattern:, name: 'regex', description: 'Regex evaluator')
  super(name: name, description: description)
  @pattern = pattern.is_a?(Regexp) ? pattern : Regexp.new(pattern)
end

Instance Method Details

#evaluate(_input, output, expected: nil, context: nil) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/langfuse/evaluation.rb', line 161

def evaluate(_input, output, expected: nil, context: nil)
  match = @pattern.match(output.to_s)
  score = match ? 1 : 0

  create_score(
    value: score,
    comment: score == 1 ? 'Regex pattern matched' : 'Regex pattern not matched'
  )
end