alexrudall / ruby-openai

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

Include rate limit headers in response #374

Closed JaredShay closed 7 months ago

JaredShay commented 7 months ago

Responses contain a lot of information in headers.

x-ratelimit-limit-requests
x-ratelimit-limit-tokens
x-ratelimit-remaining-requests
x-ratelimit-remaining-tokens
x-ratelimit-reset-requests
x-ratelimit-reset-tokens

These are not accessible as the response objects are json representations of the response body.

Having access to these values would be helpful to implement custom rate limiting without relying on parsing out values from error messages.

There are a few ways to do this:

alexrudall commented 7 months ago

This PR enables this kinda thing as needed I think. eg. if you set

OpenAI::Client.new do |client|
  client.response :logger, ::Logger.new(STDOUT), bodies: true
end

ya get everything including

openai-version: "2020-10-01"
strict-transport-security: "max-age=15724800; includeSubDomains"
x-ratelimit-limit-requests: "3500"
x-ratelimit-limit-tokens: "90000"
x-ratelimit-remaining-requests: "3499"
x-ratelimit-remaining-tokens: "89980"
x-ratelimit-reset-requests: "17ms"
x-ratelimit-reset-tokens: "12ms"
JaredShay commented 7 months ago

That looks great. Allowing the ability to pass in middleware means anything can be modified. I can access the headers and set them as variables, add them to the response, log them, etc.

Absolutely solves my use case. Thanks for the quick reply.

alexrudall commented 7 months ago

@JaredShay Awesome, that's released in v6.3!