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
128 changes: 128 additions & 0 deletions src/OpenAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class OpenAi
private string $customUrl = "";
private string $proxy = "";
private array $curlInfo = [];
private array $responseHeaders;
private array $rateLimitInfo;
private int $processingMs = 0;

public function __construct($OPENAI_API_KEY)
{
Expand Down Expand Up @@ -497,6 +500,118 @@ public function setORG(string $org)
}
}

/**
* @return array
*/
private function getResponseHeaders(): array
{
return $this->responseHeaders;
}

/**
* @param array $responseHeaders
* @return void
*/
private function setResponseHeaders(array $responseHeaders)
{
$this->responseHeaders = $responseHeaders;
}

/**
* @return array
*/
public function getRateLimitInfo(): array
{
return $this->rateLimitInfo;
}

/**
* @param array $rateLimitInfo
* @return void
*/
private function setRateLimitInfo(array $rateLimitInfo)
{
$this->rateLimitInfo = $rateLimitInfo;
}

/**
* @return void
*/
private function hydrateRateLimitInfo(): void
{
$responseHeaders = $this->getResponseHeaders();
if(!empty($responseHeaders))
{
$rateLimitInfo = [
'ratelimit-limit-requests' => array_key_exists('x-ratelimit-limit-requests',$responseHeaders) ? $responseHeaders['x-ratelimit-limit-requests'] : null,
'ratelimit-limit-tokens' => array_key_exists('x-ratelimit-limit-tokens',$responseHeaders) ? $responseHeaders['x-ratelimit-limit-tokens'] : null,
'ratelimit-remaining-requests' => array_key_exists('x-ratelimit-remaining-requests',$responseHeaders) ? $responseHeaders['x-ratelimit-remaining-requests'] : null,
'ratelimit-remaining-tokens' => array_key_exists('x-ratelimit-remaining-tokens',$responseHeaders) ? $responseHeaders['x-ratelimit-remaining-tokens'] : null,
'ratelimit-reset-requests' => array_key_exists('x-ratelimit-reset-requests',$responseHeaders) ? $responseHeaders['x-ratelimit-reset-requests'] : null,
'ratelimit-reset-tokens' => array_key_exists('x-ratelimit-reset-tokens',$responseHeaders) ? $responseHeaders['x-ratelimit-reset-tokens'] : null,
];

$this->setRateLimitInfo($rateLimitInfo);
}
}

/**
* @return array
*/
public function getProcessingMs(): int
{
return $this->processingMs;
}

/**
* @param int $processingMs
* @return void
*/
private function setProcessingMs(int $processingMs)
{
$this->processingMs = $processingMs;
}

/**
* @return void
*/
private function hydrateProcessingMs(): void
{
$responseHeaders = $this->getResponseHeaders();
if(!empty($responseHeaders) && array_key_exists('openai-processing-ms',$responseHeaders))
{
$processingMs = $responseHeaders['openai-processing-ms'];

$this->setProcessingMs($processingMs);
}
}

/**
* @param array &$responseHeaders
* @return callable
*/
private function setHeaderFunction(array &$responseHeaders): callable
{
return function ($curl, $header) use (&$responseHeaders) {
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;

$name = strtolower(trim($header[0]));
if (!array_key_exists($name, $responseHeaders)) {
$responseHeaders[$name] = trim($header[1]);
} else {
if (!is_array($responseHeaders[$name])) {
$responseHeaders[$name] = [$responseHeaders[$name]];
}
$responseHeaders[$name][] = trim($header[1]);
}

return $len;
};
}

/**
* @param string $url
* @param string $method
Expand Down Expand Up @@ -524,6 +639,7 @@ private function sendRequest(string $url, string $method, array $opts = [])
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $post_fields,
CURLOPT_HTTPHEADER => $this->headers,
CURLOPT_HEADER => false,
];

if ($opts == []) {
Expand All @@ -540,6 +656,13 @@ private function sendRequest(string $url, string $method, array $opts = [])

$curl = curl_init();

$responseHeaders = [];
curl_setopt(
$curl,
CURLOPT_HEADERFUNCTION,
$this->setHeaderFunction($responseHeaders)
);

curl_setopt_array($curl, $curl_info);
$response = curl_exec($curl);

Expand All @@ -550,6 +673,11 @@ private function sendRequest(string $url, string $method, array $opts = [])

if (!$response) throw new Exception(curl_error($curl));

$this->setResponseHeaders($responseHeaders);

$this->hydrateRateLimitInfo();
$this->hydrateProcessingMs();

return $response;
}

Expand Down