Class: Foobara::Ai::OpenAiApi::BaseCommand
- Inherits:
-
Command
- Object
- Command
- Foobara::Ai::OpenAiApi::BaseCommand
show all
- Includes:
- HttpApiCommand
- Defined in:
- foobara-open-ai-api-1.0.1/src/foobara/ai/open_ai_api/base_command.rb
Constant Summary
TruncatedInspect::MAX_LENGTH
Class Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Command
install!
#inspect, truncating
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
Class Attribute Details
.api_token ⇒ Object
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_token ⇒ Object
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_env ⇒ Object
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
|
27
28
29
|
# File 'src/foobara/ai/open_ai_api/base_command.rb', line 27
def
self. = { "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"
sleep 2 ** failures
issue_http_request(failures + 1)
when "529"
failures += 1
sleep failures
issue_http_request(failures)
end
end
|