openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.7k stars 483 forks source link

[0.6.x] Add access to meta / header information #195

Closed gehrisandro closed 1 year ago

gehrisandro commented 1 year ago

The API does return some meta information in the response header. This PR makes them available on the response object.

Here the relevant part from the update README:

On all response objects you can access the meta information returned by the API via the meta() method.

$response = $client->completions()->create([
    'model' => 'text-davinci-003',
    'prompt' => 'Say this is a test',
]);

$meta = $response->meta();

$meta->requestId; // '574a03e2faaf4e9fd703958e4ddc66f5'

$meta->openai->model; // 'text-davinci-003'
$meta->openai->organization; // 'org-jwe45798ASN82s'
$meta->openai->version; // '2020-10-01'
$meta->openai->processingMs; // 425

$meta->requestLimit->limit; // 3000
$meta->requestLimit->remaining; // 2999
$meta->requestLimit->reset; // '20ms'

$meta->tokenLimit->limit; // 250000
$meta->tokenLimit->remaining; // 249984
$meta->tokenLimit->reset; // '3ms'

The toArray() method returns the meta information in the form originally returned by the API.

$meta->toArray();

// [ 
//   'x-request-id' => '574a03e2faaf4e9fd703958e4ddc66f5',
//   'openai-model' => 'text-davinci-003',
//   'openai-organization' => 'org-jwe45798ASN82s',
//   'openai-processing-ms' => 402,
//   'openai-version' => '2020-10-01',
//   'x-ratelimit-limit-requests' => 3000,
//   'x-ratelimit-remaining-requests' => 2999,
//   'x-ratelimit-reset-requests' => '20ms',
//   'x-ratelimit-limit-tokens' => 250000,
//   'x-ratelimit-remaining-tokens' => 249983,
//   'x-ratelimit-reset-tokens' => '3ms',
// ]

On streaming responses you can access the meta information on the reponse stream object.

$stream = $client->completions()->createStreamed([
    'model' => 'text-davinci-003',
    'prompt' => 'Say this is a test',
]);

$stream->meta(); 
nunomaduro commented 1 year ago

You probably should want to make this 7.x?

gehrisandro commented 1 year ago

You probably should want to make this 7.x?

Technically it is a non-breaking change. But we can make a 0.7 to make it more clear that it has a bigger new feature.

By the way I will not do the release today, as I first want to fix / change some other stuff.

nunomaduro commented 1 year ago

@gehrisandro Yeah - it's fine to do 7.x cuz marketing wise that feature looks sexy.