Class: Foobara::Ai::AnthropicApi::BaseCommand

Inherits:
Command
  • Object
show all
Includes:
HttpApiCommand
Defined in:
foobara-anthropic-api-1.0.5/src/foobara/ai/anthropic_api/base_command.rb

Direct Known Subclasses

CreateMessage, GetPageOfModels, 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.



10
11
12
# File 'src/foobara/ai/anthropic_api/base_command.rb', line 10

def api_token
  @api_token
end

Instance Method Details

#anthropic_versionObject



38
39
40
# File 'src/foobara/ai/anthropic_api/base_command.rb', line 38

def anthropic_version
  inputs[:anthropic_version] || ENV.fetch("ANTHROPIC_VERSION", "2023-06-01")
end

#api_tokenObject



30
31
32
# File 'src/foobara/ai/anthropic_api/base_command.rb', line 30

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

#api_token_from_envObject



34
35
36
# File 'src/foobara/ai/anthropic_api/base_command.rb', line 34

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

#build_request_headersObject



22
23
24
25
26
27
28
# File 'src/foobara/ai/anthropic_api/base_command.rb', line 22

def build_request_headers
  self.request_headers = {
    "Content-Type" => "application/json",
    "X-Api-Key" => api_token,
    "Anthropic-Version" => anthropic_version
  }
end

#issue_http_request(failures = 0) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'src/foobara/ai/anthropic_api/base_command.rb', line 42

def issue_http_request(failures = 0)
  super()

  return if failures > 6

  case response.code
  when "429"
    # TODO: figure out how to test this code path
    # :nocov:
    sleep 2 ** failures
    issue_http_request(failures + 1)
    # :nocov:
  when "529"
    # :nocov:
    failures += 1
    sleep failures
    issue_http_request(failures)
    # :nocov:
  end
end