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

Undefined array key "transient" #174

Closed contactlutforrahman closed 1 year ago

contactlutforrahman commented 1 year ago

Suddenly I'm getting this error - Undefined array key "transient"

Here's my code:

$client = \OpenAI::client(getenv('OPENAI_API_KEY'));
  $response = $client->audio()->transcribe([
      'model' => 'whisper-1',
      'transient' => false,
      'file' => fopen($filePath, 'r'),
      'response_format' => 'verbose_json',
  ]);
  return $response;
pb30 commented 1 year ago

Are you on 0.6.4+?

HenryNewcomer commented 1 year ago

I was just going to post this issue. This solution was generated by ChatGPT:

It looks like the OpenAI API response may not always include the 'transient' key, leading to a "Undefined array key 'transient'" warning when the PHP client tries to access it directly. Also, the constructor of the TranscriptionResponseSegment class strictly requires a bool for the $transient parameter, leading to a "TypeError" when null is provided if 'transient' key is not present.

Here is an alternative way to handle the absence of the 'transient' key, while also ensuring that $transient is of type bool:

// In the file: /vendor/openai-php/client/src/Responses/Audio/TranscriptionResponseSegment.php

public static function from(array $attributes): self
{
    $transient = $attributes['transient'] ?? false;

    return new self(
        $attributes['id'],
        $attributes['seek'],
        $attributes['start'],
        $attributes['end'],
        $attributes['text'],
        $attributes['tokens'],
        $attributes['temperature'],
        $attributes['avg_logprob'],
        $attributes['compression_ratio'],
        $attributes['no_speech_prob'],
        (bool) $transient
    );
}

This manual adjustment worked on my end. It's a bit ugly, but it works.

pb30 commented 1 year ago

It should also be solved by upgrading