Class: EvalRuby::Result
- Inherits:
-
Object
- Object
- EvalRuby::Result
- Defined in:
- lib/eval_ruby/result.rb
Overview
Holds evaluation scores and details for a single sample.
Constant Summary collapse
- METRICS =
%i[faithfulness relevance correctness context_precision context_recall].freeze
Instance Attribute Summary collapse
-
#details ⇒ Hash{Symbol => Hash}
readonly
Metric name to details mapping.
-
#scores ⇒ Hash{Symbol => Float}
readonly
Metric name to score mapping.
Instance Method Summary collapse
-
#initialize(scores: {}, details: {}) ⇒ Result
constructor
A new instance of Result.
-
#overall(weights: nil) ⇒ Float?
Computes a weighted average of all available scores.
-
#to_h ⇒ Hash
Scores plus overall.
-
#to_s ⇒ String
Human-readable summary.
Constructor Details
#initialize(scores: {}, details: {}) ⇒ Result
Returns a new instance of Result.
21 22 23 24 |
# File 'lib/eval_ruby/result.rb', line 21 def initialize(scores: {}, details: {}) @scores = scores @details = details end |
Instance Attribute Details
#details ⇒ Hash{Symbol => Hash} (readonly)
Returns metric name to details mapping.
17 18 19 |
# File 'lib/eval_ruby/result.rb', line 17 def details @details end |
#scores ⇒ Hash{Symbol => Float} (readonly)
Returns metric name to score mapping.
14 15 16 |
# File 'lib/eval_ruby/result.rb', line 14 def scores @scores end |
Instance Method Details
#overall(weights: nil) ⇒ Float?
Computes a weighted average of all available scores.
44 45 46 47 48 49 50 51 |
# File 'lib/eval_ruby/result.rb', line 44 def overall(weights: nil) weights ||= METRICS.each_with_object({}) { |m, h| h[m] = 1.0 } available = @scores.select { |k, v| weights.key?(k) && v } return nil if available.empty? total_weight = available.sum { |k, _| weights[k] } available.sum { |k, v| v * weights[k] } / total_weight end |
#to_h ⇒ Hash
Returns scores plus overall.
54 55 56 |
# File 'lib/eval_ruby/result.rb', line 54 def to_h @scores.merge(overall: overall) end |
#to_s ⇒ String
Returns human-readable summary.
59 60 61 62 63 |
# File 'lib/eval_ruby/result.rb', line 59 def to_s lines = @scores.map { |k, v| " #{k}: #{v&.round(4) || 'N/A'}" } lines << " overall: #{overall&.round(4) || 'N/A'}" "EvalRuby::Result\n#{lines.join("\n")}" end |