Class: Foobara::Ai::OpenAiApi::BaseCommand

Inherits:
Command
  • Object
show all
Includes:
HttpApiCommand
Defined in:
foobara-open-ai-api-1.0.1/src/foobara/ai/open_ai_api/base_command.rb

Direct Known Subclasses

CreateChatCompletion, ListModels

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Command

install!

Methods included from TruncatedInspect

#inspect, truncating

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Class Attribute Details

.api_tokenObject

Returns the value of attribute api_token.



8
9
10
# File 'src/foobara/ai/open_ai_api/base_command.rb', line 8

def api_token
  @api_token
end

Instance Method Details

#api_tokenObject



19
20
21
# File 'src/foobara/ai/open_ai_api/base_command.rb', line 19

def api_token
  inputs[:api_token] || BaseCommand.api_token || api_token_from_env
end

#api_token_from_envObject



23
24
25
# File 'src/foobara/ai/open_ai_api/base_command.rb', line 23

def api_token_from_env
  ENV.fetch("OPENAI_API_KEY", nil)
end

#build_request_headersObject



27
28
29
# File 'src/foobara/ai/open_ai_api/base_command.rb', line 27

def build_request_headers
  self.request_headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{api_token}" }
end

#issue_http_request(failures = 0) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'src/foobara/ai/open_ai_api/base_command.rb', line 31

def issue_http_request(failures = 0)
  super()

  return if failures > 6

  case response.code
  when "429"
    # :nocov:
    sleep 2 ** failures
    issue_http_request(failures + 1)
    # :nocov:
  when "529"
    # Do these even happen with openai? or only anthropic?
    # :nocov:
    failures += 1
    sleep failures
    issue_http_request(failures)
    # :nocov:
  end
end