Class: LangsmithrbRails::Evals::Targets::Http

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

Overview

HTTP target for evaluating LLM responses via API calls

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Http

Initialize the target



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/generators/langsmithrb_rails/evals/templates/targets/http.rb', line 14

def initialize(config = {})
  @url = config["url"] || ENV["LANGSMITH_EVAL_TARGET_URL"]
  @headers = config["headers"] || {}
  @method = (config["method"] || "post").downcase
  @timeout = config["timeout"] || 30
  
  # Add default headers
  @headers["Content-Type"] ||= "application/json"
  @headers["Accept"] ||= "application/json"
  
  # Add authorization if provided
  if config["api_key"]
    @headers["Authorization"] ||= "Bearer #{config["api_key"]}"
  end
end

Instance Method Details

#run(input) ⇒ Hash

Run the target with the given input



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/generators/langsmithrb_rails/evals/templates/targets/http.rb', line 33

def run(input)
  # Validate URL
  unless @url
    return { error: "No target URL specified" }
  end
  
  # Create URI
  uri = URI.parse(@url)
  
  # Create HTTP client
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = @timeout
  http.read_timeout = @timeout
  
  # Create request
  request = create_request(uri, input)
  
  # Add headers
  @headers.each do |key, value|
    request[key] = value
  end
  
  # Send request
  response = http.request(request)
  
  # Parse response
  parse_response(response)
rescue => e
  { error: e.message }
end