alexrudall / ruby-openai

OpenAI API + Ruby! 🤖❤️ NEW: Assistant Vector Stores
MIT License
2.61k stars 302 forks source link

Add ability to pass Faraday config block to Client #373

Closed alexrudall closed 7 months ago

alexrudall commented 7 months ago

All Submissions:

ErikDeBruijn commented 7 months ago

This is such interesting timing. I was thinking of adding something along these lines, actually, because so far I was monkey patching this in the initializer openai.rb!

OpenAI.configure do |config|
  config.access_token = Rails.application.credentials.open_ai_key
  config.request_timeout = 300
end

module OpenAI
  module HTTP
    def connection_with_timeout(multipart: false)
      original_connection(multipart:).tap do |conn|
        conn.use TimeoutMiddleware, timeout: @request_timeout
      end
    end

    alias_method :original_connection, :conn
    alias_method :conn, :connection_with_timeout
  end
end

Obviously, having such a block to configure the Faraday object is an improvement.

FYI: this was because I was still observing time outs

alexrudall commented 7 months ago

This is such interesting timing. I was thinking of adding something along these lines, actually, because so far I was monkey patching this in the initializer openai.rb!


OpenAI.configure do |config|

  config.access_token = Rails.application.credentials.open_ai_key

  config.request_timeout = 300

end

module OpenAI

  module HTTP

    def connection_with_timeout(multipart: false)

      original_connection(multipart:).tap do |conn|

        conn.use TimeoutMiddleware, timeout: @request_timeout

      end

    end

    alias_method :original_connection, :conn

    alias_method :conn, :connection_with_timeout

  end

end

Obviously, having such a block to configure the Faraday object is an improvement.

FYI: this was because I was still observing time outs

Ah sweet - could you test this PR with that use case? Would be much appreciated 🙏