Class: Contai::Providers::HTTP
- Includes:
- HTTParty
- Defined in:
- lib/contai/providers/http.rb
Direct Known Subclasses
Instance Method Summary collapse
Methods inherited from Base
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 = [:url] || raise(ArgumentError, "HTTP provider requires :url option") method = ([:method] || :post).to_s.downcase default_headers = Contai.configuration&.default_headers || { "Content-Type" => "application/json" } = { headers: default_headers.merge([:headers] || {}), timeout: [:timeout] || Contai.configuration&.timeout || 30 } body_data = [: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 } [:body] = body_data.to_json end response = case method when "get" self.class.get(url, ) when "post" self.class.post(url, ) when "put" self.class.put(url, ) else raise ArgumentError, "Unsupported HTTP method: #{method}" end if response.success? content = extract_content(response, [:response_path]) success_result(content) else error_result("HTTP API error: #{response.code} #{response.message}") end rescue => e error_result(e.) end |