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

Enhancement: Expose Response Headers and Rate Limit Metrics, Efficiency Logging #121

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

mecfletcher
Copy link

@mecfletcher mecfletcher commented Nov 14, 2023

Overview

This pull request introduces several enhancements to the open-ai project. Key changes include exposing response headers, rate limit metrics, and improvements in efficiency logging.

Key Changes

  • Response Headers Exposure: The response headers are now accessible, allowing clients to get more detailed information about their API requests.
  • Rate Limit Metrics: Rate limit information is now available, enabling users to track and manage their API usage more effectively.
  • Efficiency Logging: Added logging for processing time (processingMs), providing insights into the performance and efficiency of API requests.

Files Changed

  • src/OpenAi.php: Major changes include:
    • Addition of private arrays $responseHeaders and $rateLimitInfo.
    • Implementation of methods for setting and getting response headers and rate limit info.
    • Addition of hydrateRateLimitInfo and hydrateProcessingMs methods for processing the corresponding metrics.

Impact

These enhancements will provide users with more control and transparency when interacting with the API. The exposure of rate limits and processing times will aid in efficient API usage and troubleshooting.

Usage

  • getRateLimitInfo(): returns an array of key-value pairs containing all of the ratelimit info provided via the OpenAI Api response headers:

    $rateLimitInfo = $OpenAi->getRateLimitInfo();

    $rateLimitInfo =[
    'ratelimit-limit-requests' => $responseHeaders['x-ratelimit-limit-requests'],
    'ratelimit-limit-tokens' => $responseHeaders['x-ratelimit-limit-tokens'],
    'ratelimit-remaining-requests' => $responseHeaders['x-ratelimit-remaining-requests'],
    'ratelimit-remaining-tokens' => $responseHeaders['x-ratelimit-remaining-tokens'],
    'ratelimit-reset-requests' => $responseHeaders['x-ratelimit-reset-requests'],
    'ratelimit-reset-tokens' => $responseHeaders['x-ratelimit-reset-tokens'],
    ];

  • getProcessingMs(): returns an int equal to the server request/response processing time provided via the OpenAI Api response headers:

    $processingMs = $OpenAi->getProcessingMs()

    $processingMs = $responseHeaders['openai-processing-ms'];

From the OpenAi Docs:

Screenshot 2023-11-14 at 2 25 17 PM

@maximehinnekens
Copy link

This is highly needed!

@orhanerday
Copy link
Owner

Sorry for the delay, @mecfletcher.

Could you please explain to me how I can test this? Do you have any usage examples?

@mecfletcher
Copy link
Author

@orhanerday I apologize for the delay on my end, I've added a usage section to the PR description and added some better formatting! Let me know if you have any other questions.

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

Successfully merging this pull request may close these issues.

3 participants