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

Switch between stream and non-stream #210

Closed mrahmadt closed 10 months ago

mrahmadt commented 10 months ago

Hello All

If I created a client with a stream feature, is there any way I can use the client inside my code without streaming?

For example, I want to use streaming for the interaction with the end-user, but sometimes inside my code, I want to send some message to OpenAI chat to do some internal work.

Or do I have to create two clients?

gehrisandro commented 10 months ago

Hi @mrahmadt

You can use a single instance working with streamed and non streamed requests. Just use the methods create or createStreamed.

Hope this helps. Otherwise just let me know.

mrahmadt commented 10 months ago

Thanks @gehrisandro

I'm using this code to create my client (custom configuration)

$yourApiKey = getenv('YOUR_API_KEY');

$client = OpenAI::factory()
    ->withApiKey($yourApiKey)
    ->withOrganization('your-organization') // default: null
    ->withBaseUri('openai.example.com/v1') // default: api.openai.com/v1
    ->withHttpClient($client = new \GuzzleHttp\Client([])) // default: HTTP client found using PSR-18 HTTP Client Discovery
    ->withHttpHeader('X-My-Header', 'foo')
    ->withQueryParam('my-param', 'bar')
    ->withStreamHandler(fn (RequestInterface $request): ResponseInterface => $client->send($request, [
        'stream' => true // Allows to provide a custom stream handler for the http client.
    ]))
    ->make();

If I understand you correctly, I now need to decide between create or createStreamed even after creating factory with "withStreamHandler" ?

Thanks