Class: LangsmithrbRails::Evals::Targets::Ruby

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/langsmithrb_rails/evals/templates/targets/ruby.rb

Overview

Ruby target for evaluating LLM responses via direct Ruby calls

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Ruby

Initialize the target



10
11
12
13
14
15
# File 'lib/generators/langsmithrb_rails/evals/templates/targets/ruby.rb', line 10

def initialize(config = {})
  @class_name = config["class"]
  @method_name = config["method"]
  @args = config["args"] || []
  @kwargs = config["kwargs"] || {}
end

Instance Method Details

#run(input) ⇒ Hash

Run the target with the given input



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
# File 'lib/generators/langsmithrb_rails/evals/templates/targets/ruby.rb', line 20

def run(input)
  # Validate configuration
  unless @class_name && @method_name
    return { error: "Class and method must be specified" }
  end
  
  # Find the class
  klass = find_class(@class_name)
  unless klass
    return { error: "Class #{@class_name} not found" }
  end
  
  # Check if the method exists
  unless klass.respond_to?(@method_name) || klass.instance_methods.include?(@method_name.to_sym)
    return { error: "Method #{@method_name} not found in #{@class_name}" }
  end
  
  # Prepare arguments
  args = prepare_args(input)
  kwargs = prepare_kwargs(input)
  
  # Call the method
  result = call_method(klass, args, kwargs)
  
  # Format the result
  format_result(result)
rescue => e
  { error: e.message, backtrace: e.backtrace&.first(5) }
end