Skip to content
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

Add header access #514

Open
ethanghunt opened this issue Sep 5, 2024 · 1 comment
Open

Add header access #514

ethanghunt opened this issue Sep 5, 2024 · 1 comment

Comments

@ethanghunt
Copy link

Is your feature request related to a problem? Please describe.
When using OpenAI's API you need to be able to check your rate limit. The only way to do this is by going through the headers. In recent versions of this gem, only the body of the response is returned, and so the rate limit cannot be checked.

Describe the solution you'd like
I don't have a particular method, but there should certainly be an option to see the headers in the response if required.

Describe alternatives you've considered
The alternative to this method is to keep track of exactly how many tokens have been used per minute, which seems like overkill.

Additional context
If there are other solutions, I would love to hear them, but I think this is a good reason to address this issue again aside from being able to see Faraday Errors.

@agnyp
Copy link

agnyp commented Nov 30, 2024

Besides OpenAI not always sending the headers in question (in my case no such headers for 4o-mini), you can rather easily access the headers using a faraday-middleware. Like this:

class HeadersMiddleware < Faraday::Middleware
  def on_complete(env)
     env[:response_headers].each do |k, v|
       next unless k.match(/[xX]-/)
       puts k.upcase + ": " + v
    end
  end
end
# some code
client = OpenAI::Client.new do |f|
  f.use HeadersMiddleware
end

That's it. Maybe you can go from here and extract the headers to your liking for further examination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants