Skip to content

Async fix #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ source "https://rubygems.org"
# Include gem dependencies from ruby-openai.gemspec
gemspec

gem "async-http-faraday", "~> 0.19.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't be part of gem deps, but will solely be used in tests.

gem "byebug", "~> 11.1.3"
gem "dotenv", "~> 2.8.1"
gem "rake", "~> 13.2"
41 changes: 41 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -12,9 +12,33 @@ GEM
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
async (2.21.0)
console (~> 1.29)
fiber-annotation
io-event (~> 1.6, >= 1.6.5)
async-http (0.84.0)
async (>= 2.10.2)
async-pool (~> 0.9)
io-endpoint (~> 0.14)
io-stream (~> 0.6)
metrics (~> 0.12)
protocol-http (~> 0.43)
protocol-http1 (>= 0.28.1)
protocol-http2 (~> 0.19)
traces (~> 0.10)
async-http-faraday (0.19.0)
async-http (~> 0.42)
faraday
async-pool (0.10.2)
async (>= 1.25)
traces
base64 (0.2.0)
bigdecimal (3.1.8)
byebug (11.1.3)
console (1.29.0)
fiber-annotation
fiber-local (~> 1.1)
json
crack (1.0.0)
bigdecimal
rexml
@@ -28,12 +52,27 @@ GEM
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.0.2)
fiber-annotation (0.2.0)
fiber-local (1.1.0)
fiber-storage
fiber-storage (1.0.0)
hashdiff (1.1.0)
io-endpoint (0.14.0)
io-event (1.7.4)
io-stream (0.6.1)
json (2.6.3)
metrics (0.12.1)
multipart-post (2.3.0)
parallel (1.22.1)
parser (3.2.2.0)
ast (~> 2.4.1)
protocol-hpack (1.5.1)
protocol-http (0.47.0)
protocol-http1 (0.28.1)
protocol-http (~> 0.22)
protocol-http2 (0.21.0)
protocol-hpack (~> 1.4)
protocol-http (~> 0.18)
public_suffix (5.0.5)
rainbow (3.1.1)
rake (13.2.1)
@@ -68,6 +107,7 @@ GEM
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
strscan (3.1.0)
traces (0.14.1)
unicode-display_width (2.4.2)
vcr (6.1.0)
webmock (3.23.1)
@@ -79,6 +119,7 @@ PLATFORMS
ruby

DEPENDENCIES
async-http-faraday (~> 0.19.0)
byebug (~> 11.1.3)
dotenv (~> 2.8.1)
rake (~> 13.2)
1 change: 1 addition & 0 deletions lib/openai/client.rb
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ class Client
uri_base
request_timeout
extra_headers
async
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New config option

].freeze
attr_reader *CONFIG_KEYS, :faraday_middleware

1 change: 1 addition & 0 deletions lib/openai/http.rb
Original file line number Diff line number Diff line change
@@ -78,6 +78,7 @@ def conn(multipart: false)
f.use MiddlewareErrors if @log_errors
f.response :raise_error
f.response :json
f.adapter :async_http if @async
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require users to include async-http-faraday in their app themselves.

end

@faraday_middleware&.call(connection)
4 changes: 3 additions & 1 deletion spec/openai/client/http_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "async/http/faraday"

RSpec.describe OpenAI::HTTP do
describe "with an aggressive timeout" do
let(:timeout_errors) { [Faraday::ConnectionFailed, Faraday::TimeoutError] }
@@ -31,7 +33,7 @@

describe ".json_post" do
let(:response) do
OpenAI::Client.new.chat(parameters: parameters)
OpenAI::Client.new(async: true).chat(parameters: parameters)
end

let(:parameters) do