openai-php / client

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

Repeating response content when using streamed chat API #183

Closed hakimio closed 1 year ago

hakimio commented 1 year ago

Using the following code to stream chat responses:

$stream = OpenAI::chat()->createStreamed([
    'model' => 'gpt-3.5-turbo',
    'messages' => [
        ['role' => 'user', 'content' => 'Say "Hello World"']
    ],
    'max_tokens' => 500,
    'temperature' => 0.5,
    'top_p' => 0.5,
    'frequency_penalty' => 0,
    'presence_penalty' => 0,
    'user' => '<some user id>'
]);

/* @var CreateStreamedResponse $response */
foreach ($stream as $response) {
    if (connection_aborted()) {
        return;
    }

    /* @var CreateStreamedResponseChoice $choice */
    $choice = $response->choices[0];
    $answer = $choice->delta->content;

    $this->sendUpdate($answer);

    $finishReason = $choice->finishReason;
    if (isset($finishReason)) {
        $this->sendUpdate("<$finishReason>");
        return;
    }
}

In response I get the following from the API (ids are sanitized but they are different):

{
    "id": "<sanitized>",
    "object": "chat.completion.chunk",
    "created": 1691674231,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
        {
            "index": 0,
            "delta": {
                "content": "Hello"
            },
            "finish_reason": null
        }
    ]
}

{
    "id": "<sanitized>",
    "object": "chat.completion.chunk",
    "created": 1691674235,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
        {
            "index": 0,
            "delta": {
                "content": "Hello"
            },
            "finish_reason": null
        }
    ]
}

{
    "id": "<sanitized>",
    "object": "chat.completion.chunk",
    "created": 1691674238,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
        {
            "index": 0,
            "delta": {
                "content": "Hello"
            },
            "finish_reason": null
        }
    ]
}

// Ad Infinitum...

Any idea what's happening?

hakimio commented 1 year ago

Not a client library issue.