Module: Ask::LLM::HTTP
- Defined in:
- lib/ask/llm/http.rb
Overview
Shared HTTP infrastructure for all providers.
Class Method Summary collapse
-
.connection(base_url, headers: {}, request: {}) ⇒ Object
Build a Faraday connection for a provider.
-
.extract_error_message(body, status) ⇒ Object
Extract a human-readable error message from various provider error formats.
-
.map_error(status, body, provider:) ⇒ Object
Map an HTTP exception or error response to the appropriate Ask::Error.
Class Method Details
.connection(base_url, headers: {}, request: {}) ⇒ Object
Build a Faraday connection for a provider.
8 9 10 11 12 13 14 |
# File 'lib/ask/llm/http.rb', line 8 def self.connection(base_url, headers: {}, request: {}) Faraday.new(url: base_url, headers: headers, request: request) do |f| f.request :json f.response :json, content_type: /\bjson$/ f.adapter Faraday.default_adapter end end |
.extract_error_message(body, status) ⇒ Object
Extract a human-readable error message from various provider error formats.
36 37 38 39 40 41 42 43 44 |
# File 'lib/ask/llm/http.rb', line 36 def self.(body, status) return nil unless body body.dig("error", "message") || body.dig("error", "msg") || body.dig("error", "error") || body["message"] || body.to_s end |
.map_error(status, body, provider:) ⇒ Object
Map an HTTP exception or error response to the appropriate Ask::Error.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ask/llm/http.rb', line 17 def self.map_error(status, body, provider:) = (body, status) || "HTTP #{status} from #{provider}" # Check for context length exceeded regardless of status code if body&.dig("error", "code") == "context_length_exceeded" return Ask::ContextLengthExceeded.new("#{provider}: #{}") end case status when 400 then Ask::ProviderError.new(, status_code: status, response_body: body&.to_json) when 401, 403 then Ask::Unauthorized.new("#{provider}: #{}") when 429 then Ask::RateLimitError.new("#{provider}: #{}") when 500 then Ask::ServerError.new("#{provider}: #{}") when 503 then Ask::ServiceUnavailable.new("#{provider}: #{}") else Ask::ProviderError.new("#{provider}: #{}", status_code: status, response_body: body&.to_json) end end |