openai-php / client

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

Help: Unable to test fake streamed response #324

Closed RhydianJenkins closed 5 months ago

RhydianJenkins commented 5 months ago

I'm currently trying to create a fake streamed response which should return some string like "This is a test response".

Here's my what I've tried:

<?php

use OpenAI\Responses\Chat\CreateStreamedResponse;
use OpenAI\Testing\ClientFake;

require __DIR__ . '/vendor/autoload.php';

$handle = fopen('php://memory', 'r+');
fwrite($handle, "This is a test response.\n");
rewind($handle);

$client = new ClientFake([
    CreateStreamedResponse::fake($handle),
]);

$client->chat()->createStreamed([
    'model' => 'gpt-3.5-turbo',
    'messages' => [
        [
            'role' => 'user',
            'content' => 'Hello',
        ],
    ],
]);

For some reason, the fake streamed response is swallowing the "This is a test response\n" and not outputting anything.

Is this intended behaviour? Thanks

RhydianJenkins commented 5 months ago

Solved.

I needed to provide the full json encoded strong from the open ai response :facepalm: