Skip to content

Commit 3fe0996

Browse files
committed
Get specs passing
1 parent 1cd9216 commit 3fe0996

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

lib/openai/client.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ module OpenAI
22
class Client
33
include OpenAI::HTTP
44

5-
attr_reader :access_token, :organization_id, :uri_base, :request_timeout, :extra_headers
5+
CONFIG_KEYS = %i[
6+
api_type
7+
api_version
8+
access_token
9+
organization_id
10+
uri_base
11+
request_timeout
12+
extra_headers
13+
].freeze
14+
attr_reader *CONFIG_KEYS
615

7-
def initialize(access_token: nil, organization_id: nil, uri_base: nil,
8-
request_timeout: nil, extra_headers: {})
9-
@access_token = access_token || OpenAI.configuration.access_token
10-
@organization_id = organization_id || OpenAI.configuration.organization_id
11-
@request_timeout = request_timeout || OpenAI.configuration.request_timeout
12-
@uri_base = uri_base || OpenAI.configuration.uri_base
13-
@extra_headers = extra_headers
16+
def initialize(config = {})
17+
CONFIG_KEYS.each do |key|
18+
# Set instance variables like api_type & access_token. Fall back to global config
19+
# if not present.
20+
instance_variable_set("@#{key}", config[key] || OpenAI.configuration.send(key))
21+
end
1422
end
1523

1624
def chat(parameters: {})
@@ -56,5 +64,9 @@ def transcribe(parameters: {})
5664
def translate(parameters: {})
5765
multipart_post(path: "/audio/translations", parameters: parameters)
5866
end
67+
68+
def azure?
69+
@api_type == :azure
70+
end
5971
end
6072
end

lib/openai/http.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,34 +70,34 @@ def conn(multipart: false)
7070
end
7171

7272
def uri(path:)
73-
if OpenAI.configuration.api_type == :azure
73+
if azure?
7474
base = File.join(@uri_base, path)
75-
"#{base}?api-version=#{OpenAI.configuration.api_version}"
75+
"#{base}?api-version=#{@api_version}"
7676
else
77-
File.join(@uri_base, OpenAI.configuration.api_version, path)
77+
File.join(@uri_base, @api_version, path)
7878
end
7979
end
8080

8181
def headers
82-
if OpenAI.configuration.api_type == :azure
82+
if azure?
8383
azure_headers
8484
else
8585
openai_headers
86-
end.merge(OpenAI.configuration.extra_headers || {})
86+
end.merge(@extra_headers || {})
8787
end
8888

8989
def openai_headers
9090
{
9191
"Content-Type" => "application/json",
9292
"Authorization" => "Bearer #{@access_token}",
9393
"OpenAI-Organization" => @organization_id
94-
}.merge(@extra_headers)
94+
}
9595
end
9696

9797
def azure_headers
9898
{
9999
"Content-Type" => "application/json",
100-
"api-key" => OpenAI.configuration.access_token
100+
"api-key" => @access_token
101101
}
102102
end
103103

0 commit comments

Comments
 (0)