client icon indicating copy to clipboard operation
client copied to clipboard

Add Timeout Param

Open Justintime50 opened this issue 2 years ago • 3 comments

The official Python library allows a timeout to be set on requests. It would be really helpful for production applications to be able to set up a timeout on requests so we don't keep our web workers hanging if there is hiccups in connections or issues on the OpenAI side.

Justintime50 avatar Jan 28 '23 06:01 Justintime50

Can you make a pull request adding that feature?

nunomaduro avatar Jan 28 '23 12:01 nunomaduro

Hmmm, it doesn't even look like the PSR7 lib that is used here to make HTTP calls supports timeouts at all if I'm looking through their code correctly? Would potentially require a wrapper around the requests to handle this since it's not supported native.

Justintime50 avatar Jan 28 '23 22:01 Justintime50

Might it be possible to use one of these options?

  • https://docs.guzzlephp.org/en/latest/request-options.html#connect-timeout
  • https://docs.guzzlephp.org/en/latest/request-options.html#timeout

Afterall a Guzzle Client is being used by the OpenAI Class

c0nst4ntin avatar Feb 02 '23 10:02 c0nst4ntin

This can now be achieved by using the newly introduced factory:

$client = OpenAI::factory()
    ->withHttpClient(new \GuzzleHttp\Client(['timeout'  => 2.0]))
    ->make();

gehrisandro avatar Mar 17 '23 19:03 gehrisandro