Skip to content

Commit 6cfabd6

Browse files
committed
Remove beta client duplication
The current implementation is breaking any attempt of connection pool usage.
1 parent b6317ec commit 6cfabd6

14 files changed

+36
-27
lines changed

lib/middleware/beta.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module OpenAI
4+
class BetaMiddleware < Faraday::Middleware
5+
BETA_REGEX = %r{
6+
\A/#{OpenAI.configuration.api_version}
7+
/(assistants|batches|threads|vector_stores)
8+
}ix.freeze
9+
10+
def on_request(env)
11+
return unless env[:url].path.match?(BETA_REGEX)
12+
13+
env[:request_headers].merge!(
14+
{
15+
"OpenAI-Beta" => "assistants=v2"
16+
}
17+
)
18+
end
19+
end
20+
end

lib/openai.rb

+2
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,5 @@ def self.rough_token_count(content = "")
9090
[1, estimate].max
9191
end
9292
end
93+
94+
require_relative "middleware/beta"

lib/openai/assistants.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
module OpenAI
22
class Assistants
3-
BETA_VERSION = "v2".freeze
4-
53
def initialize(client:)
6-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
75
end
86

97
def list

lib/openai/batches.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module OpenAI
22
class Batches
33
def initialize(client:)
4-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
55
end
66

77
def list(parameters: {})

lib/openai/client.rb

+1-6
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,6 @@ def azure?
107107
@api_type&.to_sym == :azure
108108
end
109109

110-
def beta(apis)
111-
dup.tap do |client|
112-
client.add_headers("OpenAI-Beta": apis.map { |k, v| "#{k}=#{v}" }.join(";"))
113-
end
114-
end
115-
116110
private
117111

118112
attr_reader :connection, :multipart_connection
@@ -122,6 +116,7 @@ def build_connection(multipart: false)
122116
faraday.options[:timeout] = @request_timeout
123117
faraday.request(:multipart) if multipart
124118
faraday.use MiddlewareErrors if @log_errors
119+
faraday.use BetaMiddleware
125120
faraday.response :raise_error
126121
faraday.response :json
127122
end

lib/openai/images.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module OpenAI
22
class Images
3-
def initialize(client: nil)
3+
def initialize(client:)
44
@client = client
55
end
66

lib/openai/messages.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module OpenAI
22
class Messages
33
def initialize(client:)
4-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
55
end
66

77
def list(thread_id:, parameters: {})

lib/openai/run_steps.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module OpenAI
22
class RunSteps
33
def initialize(client:)
4-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
55
end
66

77
def list(thread_id:, run_id:, parameters: {})

lib/openai/runs.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module OpenAI
22
class Runs
33
def initialize(client:)
4-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
55
end
66

77
def list(thread_id:, parameters: {})

lib/openai/threads.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module OpenAI
22
class Threads
33
def initialize(client:)
4-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
55
end
66

77
def retrieve(id:)

lib/openai/vector_store_file_batches.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module OpenAI
22
class VectorStoreFileBatches
33
def initialize(client:)
4-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
55
end
66

77
def list(vector_store_id:, id:, parameters: {})

lib/openai/vector_store_files.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module OpenAI
22
class VectorStoreFiles
33
def initialize(client:)
4-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
55
end
66

77
def list(vector_store_id:, parameters: {})

lib/openai/vector_stores.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module OpenAI
22
class VectorStores
33
def initialize(client:)
4-
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
4+
@client = client
55
end
66

77
def list(parameters: {})

spec/openai/client/client_spec.rb

+3-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
expect(c0.uri_base).to eq(OpenAI::Configuration::DEFAULT_URI_BASE)
4545
expect(c0.send(:headers).values).to include("Bearer #{c0.access_token}")
4646
expect(c0.send(:headers).values).to include(c0.organization_id)
47-
expect(c0.send(:connection).options.timeout).to eq(OpenAI::Configuration::DEFAULT_REQUEST_TIMEOUT)
47+
expect(c0.send(:connection).options.timeout).to eq(
48+
OpenAI::Configuration::DEFAULT_REQUEST_TIMEOUT
49+
)
4850
expect(c0.send(:uri, path: "")).to include(OpenAI::Configuration::DEFAULT_URI_BASE)
4951
expect(c0.send(:headers).values).to include("X-Default")
5052
expect(c0.send(:headers).values).not_to include("X-Test")
@@ -112,14 +114,6 @@
112114
end
113115
end
114116

115-
context "when using beta APIs" do
116-
let(:client) { OpenAI::Client.new.beta(assistants: "v2") }
117-
118-
it "sends the appropriate header value" do
119-
expect(client.send(:headers)["OpenAI-Beta"]).to eq "assistants=v2"
120-
end
121-
end
122-
123117
context "with a block" do
124118
let(:client) do
125119
OpenAI::Client.new do |client|

0 commit comments

Comments
 (0)