Class: LangsmithrbRails::Evaluation::StringEvaluator
- Defined in:
- lib/langsmithrb_rails/evaluation/string_evaluator.rb
Overview
Evaluator for string comparisons
Instance Attribute Summary
Attributes inherited from Evaluator
Instance Method Summary collapse
-
#evaluate(prediction, reference = nil, input = nil) ⇒ Hash
Evaluate a prediction against a reference.
-
#initialize(match_type: :exact, case_sensitive: true, client: nil, project_name: nil, tags: []) ⇒ StringEvaluator
constructor
Initialize a new string evaluator.
Methods inherited from Evaluator
#evaluate_dataset, #evaluate_run, #evaluate_runs
Constructor Details
#initialize(match_type: :exact, case_sensitive: true, client: nil, project_name: nil, tags: []) ⇒ StringEvaluator
Initialize a new string evaluator
15 16 17 18 19 |
# File 'lib/langsmithrb_rails/evaluation/string_evaluator.rb', line 15 def initialize(match_type: :exact, case_sensitive: true, client: nil, project_name: nil, tags: []) super(client: client, project_name: project_name, tags: ) @match_type = match_type @case_sensitive = case_sensitive end |
Instance Method Details
#evaluate(prediction, reference = nil, input = nil) ⇒ Hash
Evaluate a prediction against a reference
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/langsmithrb_rails/evaluation/string_evaluator.rb', line 26 def evaluate(prediction, reference = nil, input = nil) # Extract strings from prediction and reference prediction_str = extract_string(prediction) reference_str = extract_string(reference) # If reference is nil, we can't evaluate if reference_str.nil? return { score: nil, metadata: { error: "No reference provided for evaluation" } } end # Prepare strings for comparison unless @case_sensitive prediction_str = prediction_str.downcase reference_str = reference_str.downcase end # Perform comparison based on match type score, = case @match_type when :exact exact_match(prediction_str, reference_str) when :contains contains_match(prediction_str, reference_str) when :regex regex_match(prediction_str, reference_str) else [0.0, { error: "Unknown match type: #{@match_type}" }] end { score: score, metadata: } end |