Class: Contai::Providers::HTTP

Inherits:
Base
  • Object
show all
Includes:
HTTParty
Defined in:
lib/contai/providers/http.rb

Direct Known Subclasses

N8N

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Contai::Providers::Base

Instance Method Details

#generate(prompt) ⇒ Object



8
9
10
11
12
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
# File 'lib/contai/providers/http.rb', line 8

def generate(prompt)
  url = @options[:url] || raise(ArgumentError, "HTTP provider requires :url option")
  method = (@options[:method] || :post).to_s.downcase
  
  default_headers = Contai.configuration&.default_headers || { "Content-Type" => "application/json" }
  request_options = {
    headers: default_headers.merge(@options[:headers] || {}),
    timeout: @options[:timeout] || Contai.configuration&.timeout || 30
  }

  body_data = @options[:body_template] || { prompt: prompt }
  if body_data.is_a?(Hash)
    body_data = body_data.transform_values { |v| v.is_a?(String) ? v.gsub("{{prompt}}", prompt) : v }
    request_options[:body] = body_data.to_json
  end

  response = case method
            when "get"
              self.class.get(url, request_options)
            when "post"
              self.class.post(url, request_options)
            when "put"
              self.class.put(url, request_options)
            else
              raise ArgumentError, "Unsupported HTTP method: #{method}"
            end

  if response.success?
    content = extract_content(response, @options[:response_path])
    success_result(content)
  else
    error_result("HTTP API error: #{response.code} #{response.message}")
  end
rescue => e
  error_result(e.message)
end