Class: LlmGateway::BaseClient
- Inherits:
-
Object
- Object
- LlmGateway::BaseClient
- Defined in:
- lib/llm_gateway/base_client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_endpoint ⇒ Object
readonly
Returns the value of attribute base_endpoint.
-
#model_key ⇒ Object
readonly
Returns the value of attribute model_key.
Instance Method Summary collapse
- #get(url_part, extra_headers = {}) ⇒ Object
-
#initialize(model_key:, api_key:) ⇒ BaseClient
constructor
A new instance of BaseClient.
- #post(url_part, body = nil, extra_headers = {}) ⇒ Object
- #post_file(url_part, file_contents, filename, purpose: nil, mime_type: "application/octet-stream") ⇒ Object
- #post_stream(url_part, body = nil, extra_headers = {}, &block) ⇒ Object
Constructor Details
#initialize(model_key:, api_key:) ⇒ BaseClient
Returns a new instance of BaseClient.
12 13 14 15 |
# File 'lib/llm_gateway/base_client.rb', line 12 def initialize(model_key:, api_key:) @model_key = model_key @api_key = api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
10 11 12 |
# File 'lib/llm_gateway/base_client.rb', line 10 def api_key @api_key end |
#base_endpoint ⇒ Object (readonly)
Returns the value of attribute base_endpoint.
10 11 12 |
# File 'lib/llm_gateway/base_client.rb', line 10 def base_endpoint @base_endpoint end |
#model_key ⇒ Object (readonly)
Returns the value of attribute model_key.
10 11 12 |
# File 'lib/llm_gateway/base_client.rb', line 10 def model_key @model_key end |
Instance Method Details
#get(url_part, extra_headers = {}) ⇒ Object
17 18 19 20 21 |
# File 'lib/llm_gateway/base_client.rb', line 17 def get(url_part, extra_headers = {}) endpoint = "#{base_endpoint}/#{url_part.sub(%r{^/}, "")}" response = make_request(endpoint, Net::HTTP::Get, nil, extra_headers) process_response(response) end |
#post(url_part, body = nil, extra_headers = {}) ⇒ Object
57 58 59 60 61 |
# File 'lib/llm_gateway/base_client.rb', line 57 def post(url_part, body = nil, extra_headers = {}) endpoint = "#{base_endpoint}/#{url_part.sub(%r{^/}, "")}" response = make_request(endpoint, Net::HTTP::Post, body, extra_headers) process_response(response) end |
#post_file(url_part, file_contents, filename, purpose: nil, mime_type: "application/octet-stream") ⇒ Object
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 49 50 51 52 53 54 55 |
# File 'lib/llm_gateway/base_client.rb', line 23 def post_file(url_part, file_contents, filename, purpose: nil, mime_type: "application/octet-stream") endpoint = "#{base_endpoint}/#{url_part.sub(%r{^/}, "")}" uri = URI.parse(endpoint) file_io = StringIO.new(file_contents) # Create request with full URI (important!) request = Net::HTTP::Post.new(uri) form_data = [ [ "file", file_io, { filename: filename, "Content-Type" => mime_type } ] ] # Add purpose parameter if provided form_data << [ "purpose", purpose ] if purpose request.set_form(form_data, "multipart/form-data") # Headers (excluding Content-Type because set_form already sets it) multipart_headers = build_headers.reject { |k, _| k.downcase == "content-type" } multipart_headers.each { |key, value| request[key] = value } response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http| http.request(request) end process_response(response) end |
#post_stream(url_part, body = nil, extra_headers = {}, &block) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/llm_gateway/base_client.rb', line 63 def post_stream(url_part, body = nil, extra_headers = {}, &block) endpoint = "#{base_endpoint}/#{url_part.sub(%r{^/}, "")}" uri = URI(endpoint) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.read_timeout = 480 http.open_timeout = 10 body.merge!(stream: true) request = Net::HTTP::Post.new(uri) headers = build_headers.merge(extra_headers) headers.each { |key, value| request[key] = value } request.body = body.to_json if body http.request(request) do |response| unless response.code.to_i == 200 # Collect full body for error handling full_body = +"" response.read_body { |chunk| full_body << chunk } # Create a response-like object with the body for handle_error response.instance_variable_set(:@body, full_body) response.instance_variable_set(:@read, true) handle_error(response) end parse_sse_stream(response, &block) end end |