openai-php / client

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

PHP Fatal error: Uncaught Symfony\\Component\\HttpClient\\Exception\\TimeoutException #129

Closed afeno closed 1 year ago

afeno commented 1 year ago

Hello, I am getting high number of timeout errors like the one below:

PHP Fatal error: Uncaught Symfony\\Component\\HttpClient\\Exception\\TimeoutException: Idle timeout reached for "https://api.openai.com/v1/chat/completions". in /var/www/html/gpt/vendor/symfony/http-client/Chunk/ErrorChunk.php:56\nStack trace:\

Is this normal? I mean, can I expect so many timeouts from OpenAI API? The internet connection is stable. What is the default time-out?

In my php.ini, I have default_socket_timeout = 60

Any suggestion?

pb30 commented 1 year ago

In general the OpenAI API has been pretty slow and/or flaky recently, there's a lot of posts about it on their community forums.

gehrisandro commented 1 year ago

@afeno This is a common issue, especially when you are using the GPT-4 model.

The timeouts are depending on the default values your HTTP client has. If you want you can provide a HTTP client manually via factory to adjust the timeouts:

$client = OpenAI::factory()
    ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 15, 'connect_timeout' => 15])
    ->make();
EvKoh commented 1 year ago

Thank you @gehrisandro !! :100:

afeno commented 1 year ago

Thank you! Sorry to ask this basic question but How should I use the code above? If I use that code in my php file Im getting the following error: PHP Fatal error: Uncaught Error: Class "GuzzleHttp\\Client" not found

Should I chage that in the soruce code? Should I import another library?

This is my only definition of $client that I have right now: $client = OpenAI::client($yourApiKey);

Thank you.

gehrisandro commented 1 year ago

Given your first message, it looks like you are using the Symphony HTTP client. There you have to adopt my example to the Symphony client.

verstratenbram commented 1 year ago

I tried changing it to the Symfony client but I get the error: Expected parameter of type '\Psr\Http\Client\ClientInterface', '\Symfony\Contracts\HttpClient\HttpClientInterface' provided

leobeal commented 1 year ago

@afeno

 $this->client = OpenAI::factory()
   ->withHttpClient(new Client(['timeout' => 90, 'connect_timeout' => 90]))
  ->withApiKey('yourkey')
  ->make();
apexdivision commented 11 months ago

Combined all pieces from this post, after lots of trial and error, finally I am using the below. I know 300 is too high, but otherwise it just doesn't work!

$AI_client = OpenAI::factory()
                           ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 300, 'connect_timeout' => 300]))
                           ->withApiKey($AIApiKey)
                           ->make();

My code uses chat mode, ~15 prompt-response rounds, each consuming 700 to 1500 tokens, staring with low and more tokens as 'chat' grows. I am using gpt-3.5-turbo model.

Works fine so far!

ap-mcleod commented 1 month ago

For anyone else experiencing this problem, @apexdivision's answer may require guzzle to be installed:

$ composer require guzzlehttp/guzzle

In my case, I didn't have GuzzleHttp installed and it left me scratching my head because it's referenced in symfony, but the only reason I had symfony installed is because it was as a bundled requirement with openai-php/client.