Class: LightGptProxy::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/light_gpt_proxy/endpoint.rb

Constant Summary collapse

ApplyDefaultsError =
Class.new(StandardError)
RequestError =
Class.new(StandardError) do |_klass|
  def initialize(response)
    @response = response
    @status = response&.status
    super("Request failed with status #{response.status}: #{response.reason_phrase} => #{response.body}")
  end
  attr_reader :status, :response
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, path:, host:, key:, method: 'get', body_schema: nil, header_options: {}) ⇒ Endpoint

Returns a new instance of Endpoint.



22
23
24
25
26
27
28
29
30
# File 'lib/light_gpt_proxy/endpoint.rb', line 22

def initialize(name:, path:, host:, key:, method: 'get', body_schema: nil, header_options: {})
  @name = name
  @host = host
  @path = path
  @method = method
  @body_schema = body_schema
  @header_options = header_options
  @key = key
end

Instance Attribute Details

#body_schemaObject (readonly)

Returns the value of attribute body_schema.



20
21
22
# File 'lib/light_gpt_proxy/endpoint.rb', line 20

def body_schema
  @body_schema
end

#header_optionsObject (readonly)

Returns the value of attribute header_options.



20
21
22
# File 'lib/light_gpt_proxy/endpoint.rb', line 20

def header_options
  @header_options
end

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/light_gpt_proxy/endpoint.rb', line 20

def host
  @host
end

#keyObject (readonly)

Returns the value of attribute key.



20
21
22
# File 'lib/light_gpt_proxy/endpoint.rb', line 20

def key
  @key
end

#methodObject (readonly)

Returns the value of attribute method.



20
21
22
# File 'lib/light_gpt_proxy/endpoint.rb', line 20

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/light_gpt_proxy/endpoint.rb', line 20

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/light_gpt_proxy/endpoint.rb', line 20

def path
  @path
end

Instance Method Details

#apply_defaults(payload) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/light_gpt_proxy/endpoint.rb', line 41

def apply_defaults(payload)
  return payload unless body_schema

  defaults.perform(payload)
  ensure_system_presence_if_needed(payload) if name == 'completions'
rescue StandardError => e
  raise ApplyDefaultsError, e.message
end

#execute_request(payload = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/light_gpt_proxy/endpoint.rb', line 50

def execute_request(payload = {})
  connection = Faraday.new(url: "#{host}/#{path}") do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    conn.adapter Faraday.default_adapter
  end

  response = case method.upcase
             when 'POST'
               connection.post do |req|
                 req.headers = complete_headers
                 req.body = payload
               end
             when 'PUT'
               connection.put do |req|
                 req.headers = complete_headers
                 req.body = payload
               end
             when 'PATCH'
               connection.patch do |req|
                 req.headers = complete_headers
                 req.body = payload
               end
             when 'DELETE'
               connection.delete do |req|
                 req.headers = complete_headers
               end
             when 'GET'
               connection.get do |req|
                 req.headers = complete_headers
               end
             else
               raise ArgumentError, "Unsupported HTTP method: #{method}"
             end

  handle_response(response)
end

#validate_and_apply_defaults(payload) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/light_gpt_proxy/endpoint.rb', line 32

def validate_and_apply_defaults(payload)
  return payload unless body_schema

  payload = JSON.parse(payload) if payload.is_a?(String)
  apply_defaults(payload)
  validator.validate!(payload)
  payload
end