Skip to content

Commit 6a9c9ed

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

File tree

14 files changed

+33
-27
lines changed

14 files changed

+33
-27
lines changed

lib/middleware/beta.rb

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

lib/openai.rb

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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:)

0 commit comments

Comments
 (0)