Class: LangsmithrbRails::Evaluation::LLMEvaluator
- Defined in:
- lib/langsmithrb_rails/evaluation/llm_evaluator.rb
Overview
Evaluator that uses an LLM to evaluate responses
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(llm:, criteria: nil, client: nil, project_name: nil, tags: []) ⇒ LLMEvaluator
constructor
Initialize a new LLM evaluator.
Methods inherited from Evaluator
#evaluate_dataset, #evaluate_run, #evaluate_runs
Constructor Details
#initialize(llm:, criteria: nil, client: nil, project_name: nil, tags: []) ⇒ LLMEvaluator
Initialize a new LLM evaluator
15 16 17 18 19 |
# File 'lib/langsmithrb_rails/evaluation/llm_evaluator.rb', line 15 def initialize(llm:, criteria: nil, client: nil, project_name: nil, tags: []) super(client: client, project_name: project_name, tags: ) @llm = llm @criteria = criteria || "Evaluate the response for accuracy, relevance, and completeness." 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 |
# File 'lib/langsmithrb_rails/evaluation/llm_evaluator.rb', line 26 def evaluate(prediction, reference = nil, input = nil) # Extract strings prediction_str = extract_string(prediction) reference_str = extract_string(reference) input_str = input.is_a?(Hash) ? input.to_json : input.to_s if input # Create evaluation prompt prompt = create_evaluation_prompt(prediction_str, reference_str, input_str) # Get evaluation from LLM begin evaluation = get_llm_evaluation(prompt) # Parse the evaluation score, feedback = parse_evaluation(evaluation) { score: score, metadata: { feedback: feedback, criteria: @criteria, evaluation: evaluation } } rescue => e { score: nil, metadata: { error: "Evaluation failed: #{e.message}" } } end end |