Class: LlmGateway::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_gateway/base_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject (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_endpointObject (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_keyObject (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