diff --git a/README.md b/README.md index fd1c489a..5dc97b4c 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,26 @@ client = OpenAI::Client.new(access_token: "access_token_goes_here") client.add_headers("X-Proxy-TTL" => "43200") ``` +#### Proxy support + +You can use proxy with Faraday + +```ruby +proxy = { + :uri => 'http://proxy.example.com', + :user => 'foo', + :password => 'bar' +} + +OpenAI::Client.new(access_token: "access_token_goes_here", proxy: proxy) +``` + +Or use with string + +```ruby +OpenAI::Client.new(access_token: "access_token_goes_here", proxy: "foo:bar@http://proxy.example.com") +``` + #### Logging ##### Errors diff --git a/lib/openai.rb b/lib/openai.rb index 0fad1b2e..b68ac276 100644 --- a/lib/openai.rb +++ b/lib/openai.rb @@ -47,6 +47,7 @@ class Configuration :organization_id, :uri_base, :request_timeout, + :proxy, :extra_headers DEFAULT_API_VERSION = "v1".freeze @@ -63,6 +64,7 @@ def initialize @uri_base = DEFAULT_URI_BASE @request_timeout = DEFAULT_REQUEST_TIMEOUT @extra_headers = {} + @proxy = nil end end diff --git a/lib/openai/client.rb b/lib/openai/client.rb index ac05d33f..fb9fc38f 100644 --- a/lib/openai/client.rb +++ b/lib/openai/client.rb @@ -11,6 +11,7 @@ class Client uri_base request_timeout extra_headers + proxy ].freeze attr_reader *CONFIG_KEYS, :faraday_middleware diff --git a/lib/openai/http.rb b/lib/openai/http.rb index d04cca5f..062a47d4 100644 --- a/lib/openai/http.rb +++ b/lib/openai/http.rb @@ -73,6 +73,7 @@ def to_json_stream(user_proc:) def conn(multipart: false) connection = Faraday.new do |f| f.options[:timeout] = @request_timeout + f.proxy = @proxy if @proxy f.request(:multipart) if multipart f.use MiddlewareErrors if @log_errors f.response :raise_error