Class: Langfuse::Evaluators::LengthEvaluator

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

Instance Method Summary collapse

Constructor Details

#initialize(name: 'length', description: 'Length evaluator', min_length: nil, max_length: nil) ⇒ LengthEvaluator

Returns a new instance of LengthEvaluator.



122
123
124
125
126
# File 'lib/langfuse/evaluation.rb', line 122

def initialize(name: 'length', description: 'Length evaluator', min_length: nil, max_length: nil)
  super(name: name, description: description)
  @min_length = min_length
  @max_length = max_length
end

Instance Method Details

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/langfuse/evaluation.rb', line 128

def evaluate(input, output, expected: nil, context: nil)
  length = output.to_s.length

  if @min_length && @max_length
    score = length >= @min_length && length <= @max_length ? 1 : 0
    comment = score == 1 ? "Length #{length} within range" : "Length #{length} outside range #{@min_length}-#{@max_length}"
  elsif @min_length
    score = length >= @min_length ? 1 : 0
    comment = score == 1 ? "Length #{length} meets minimum" : "Length #{length} below minimum #{@min_length}"
  elsif @max_length
    score = length <= @max_length ? 1 : 0
    comment = score == 1 ? "Length #{length} within maximum" : "Length #{length} exceeds maximum #{@max_length}"
  else
    score = length
    comment = "Length: #{length}"
  end

  create_score(
    value: score,
    data_type: 'NUMERIC',
    comment: comment
  )
end