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.



154
155
156
157
# File 'lib/langfuse/evaluation.rb', line 154

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



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

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