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

PHP Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: cURL error 28: Operation timed out after #194

Closed iamved777 closed 1 year ago

iamved777 commented 1 year ago

Hi! I am using API like this

 $client = OpenAI::factory()
    ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 90, 'connect_timeout' => 90]))
    ->withApiKey($yourApiKey)
    ->make();
    $response = array();

    $prompt = "Please rewrite this text without adding any placeholder:".
    $body;

    try{
        $result = $client->chat()->create([
          'model' => 'gpt-3.5-turbo',
          'messages' => [
              ['role' => 'user', 'content' => $prompt],
          ],
      ]);

      $content = $result['choices'][0]['message']['content'];

      $response[] = array(
            'status' => true,
            'body' => $content
        );

    }catch(\OpenAI\Exceptions\ErrorException $e){
        $response[] = array(
            'status' => false
        );

    }

Its working fine but sometime I am getting curl error like this and I am not able to handle it.

PHP Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: cURL error 28: Operation timed out after 90000 milliseconds with 0 bytes received (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://api.openai.com/v1/chat/completions in /home/user/public_html/mydomains/crons/5/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:210

Let me know if anyone here can help me for solve the issue.

Thanks!

StarterES-sgaleano commented 1 year ago

Hi, @iamved777 did you managed to fix the issue? Im having the same, mine its dying after 30s. Thanks in advance.

iamved777 commented 1 year ago

@StarterES-sgaleano I am still facing same issue, I think No one interested in solve the issue or share the solution. Still waiting for someone to help us. Thanks!

StarterES-sgaleano commented 1 year ago

@iamved777 , im sure that you maybe already knew about this, but in the config file there its a Key for the time out, maybe if you can manage to increase it from there it helps?

chrisrickard commented 1 year ago

Hey guys, This is not a library issues, but correct behaviour due to the OpenAI's API not returning data within the specified timeout limit set, e.g. 90 seconds as per the code ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 90, 'connect_timeout' => 90]))

You need to manage this when any other API endpoint, and retry, or fail etc, based on your specific use case.

Personally I have a 240 second timeout - but many times its better to keep it short (e.g. default 30 secs) and simply retry.

gehrisandro commented 1 year ago

Agree to @chrisrickard

iamved777 commented 1 year ago

Hi! There no any solution provided in any reply and just closed without proper resolution. Since curl is getting used by OPENAI, we have to use some method to catch error.

@chrisrickard is saying to retry, but how we can know that error occured and so we need to retry? any example? Thanks!