openai-php / client

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

Implement timeOut #209

Closed Zillion01 closed 10 months ago

Zillion01 commented 10 months ago

I noticed OpenAI (for example Chat) is sometimes unresponsive.

A Symfony command for example will hang then.

I would suggest to implement a timeout. Since this is easy with Guzzle. It would be nice to be able to do:

$client = \OpenAI::client($apiKey, $organization, $timeOut);

Also see https://docs.guzzlephp.org/en/stable/request-options.html#timeout

Thanks!

gehrisandro commented 10 months ago

Hi @Zillion01

You can already achieve this by passing your customised Guzzle client to the factory.

OpenAI::factory()
    ->withApiKey($apiKey)
    ->withOrganization($organization)
    ->withHttpClient(new \GuzzleHttp\Client(['timeout' => $timeout]))
    ->make();
Zillion01 commented 10 months ago

Hi @gehrisandro

Thanks a lot, works great! 👍

I need to start using factory also :)

Jafo232 commented 7 months ago

I agree that the workaround suggested does work, but would it not be better to implement it the other way so it is compatible with future versions? For example, now you have to add "->withHttpHeader('OpenAI-Beta', 'assistants=v1')" for this code to work. It would be great if we could pass more options when creating the client.