Class: AIHub2API::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/aihub2api/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://aihub2api.cloud".freeze
USER_AGENT =
"aihub2api-ruby/#{AIHub2API::VERSION}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV["AIHUB_API_KEY"], base_url: ENV.fetch("AIHUB_BASE_URL", DEFAULT_BASE_URL), open_timeout: 10, read_timeout: 60, http_factory: nil) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
# File 'lib/aihub2api/client.rb', line 12

def initialize(api_key: ENV["AIHUB_API_KEY"], base_url: ENV.fetch("AIHUB_BASE_URL", DEFAULT_BASE_URL),
               open_timeout: 10, read_timeout: 60, http_factory: nil)
  @api_key = api_key.to_s.strip
  @base_url = base_url.to_s.sub(%r{/+\z}, "")
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @http_factory = http_factory
  validate_base_url!
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



10
11
12
# File 'lib/aihub2api/client.rb', line 10

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



10
11
12
# File 'lib/aihub2api/client.rb', line 10

def base_url
  @base_url
end

Instance Method Details

#chat_completions(model:, messages:, **options) ⇒ Object



34
35
36
37
# File 'lib/aihub2api/client.rb', line 34

def chat_completions(model:, messages:, **options)
  payload = options.merge(model: model, messages: messages)
  request(:post, "/v1/chat/completions", body: payload)
end

#healthObject



22
23
24
# File 'lib/aihub2api/client.rb', line 22

def health
  request(:get, "/health", authenticated: false)
end

#model_idsObject



30
31
32
# File 'lib/aihub2api/client.rb', line 30

def model_ids
  Array(models["data"]).filter_map { |model| model["id"] if model.is_a?(Hash) }
end

#modelsObject



26
27
28
# File 'lib/aihub2api/client.rb', line 26

def models
  request(:get, "/v1/models")
end

#request(method, path, body: nil, authenticated: true) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/aihub2api/client.rb', line 44

def request(method, path, body: nil, authenticated: true)
  ensure_api_key! if authenticated
  uri = endpoint_uri(path)
  request = build_request(method, uri, body, authenticated)
  response = perform_request(uri, request)
  parsed_body = parse_body(response.body)

  if response.code.to_i.between?(200, 299)
    parsed_body
  else
    raise APIError.new(
      "AIHub API returned HTTP #{response.code}",
      status: response.code.to_i,
      response_body: parsed_body,
    )
  end
rescue Timeout::Error, SocketError, SystemCallError, IOError => e
  raise TransportError, "AIHub API request failed: #{e.class}"
end

#responses(model:, input:, **options) ⇒ Object



39
40
41
42
# File 'lib/aihub2api/client.rb', line 39

def responses(model:, input:, **options)
  payload = options.merge(model: model, input: input)
  request(:post, "/v1/responses", body: payload)
end