Class: LangsmithrbRails::Evals::Checks::Correctness
- Inherits:
-
Object
- Object
- LangsmithrbRails::Evals::Checks::Correctness
- Defined in:
- lib/generators/langsmithrb_rails/evals/templates/checks/correctness.rb
Overview
Simple correctness check for evaluating LLM responses
Class Method Summary collapse
-
.evaluate(input, response, expected) ⇒ Hash
Check if the response is correct.
-
.extract_answer(response) ⇒ String
Extract the answer from the response.
Class Method Details
.evaluate(input, response, expected) ⇒ Hash
Check if the response is correct
13 14 15 16 17 18 19 20 21 22 23 24 25 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 |
# File 'lib/generators/langsmithrb_rails/evals/templates/checks/correctness.rb', line 13 def self.evaluate(input, response, expected) result = { score: 0.0, reasoning: "", passed: false } # Extract the answer from the response answer = extract_answer(response) # Check for exact match if expected["answer"] && answer == expected["answer"] result[:score] = 1.0 result[:reasoning] = "Exact match with expected answer" result[:passed] = true return result end # Check for partial matches using contains if expected["answer_contains"] && expected["answer_contains"].is_a?(Array) matches = expected["answer_contains"].select { |phrase| answer.include?(phrase) } match_ratio = matches.size.to_f / expected["answer_contains"].size result[:score] = match_ratio result[:reasoning] = "Matched #{matches.size}/#{expected["answer_contains"].size} expected phrases" result[:passed] = match_ratio >= 0.5 return result end # Check for code snippets if expected["code_contains"] && expected["code_contains"].is_a?(Array) matches = expected["code_contains"].select { |phrase| answer.include?(phrase) } match_ratio = matches.size.to_f / expected["code_contains"].size result[:score] = match_ratio result[:reasoning] = "Code snippet matched #{matches.size}/#{expected["code_contains"].size} expected elements" result[:passed] = match_ratio >= 0.5 return result end # No match found result[:reasoning] = "No matching criteria found" result end |
.extract_answer(response) ⇒ String
Extract the answer from the response
61 62 63 64 65 66 67 |
# File 'lib/generators/langsmithrb_rails/evals/templates/checks/correctness.rb', line 61 def self.extract_answer(response) return response["answer"] if response["answer"] return response["text"] if response["text"] return response["content"] if response["content"] return response["output"] if response["output"] return response.to_s end |