You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to created a streamed response but I am struggling with the documentation and a lack of examples on the web. Nothing I do seems to work, it always waits for the API to complete before displaying the response (rather than streaming the chunks).
I would be grateful if anyone can provide some guidance on implementing a streamed response using the code below. Any pointers would be appreciated.
<?php
require_once 'vendor/autoload.php';
use OpenAI\Client;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$apiKey = getenv('OPENAI_API_KEY');
$client = OpenAI::client($apiKey);
$prompt = $_POST["prompt"];
$stream = $client->chat()->createStreamed([
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'user', 'content' => $prompt],
],
]);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>OpenAI Article Generator</title>
</head>
<body>
<h1>OpenAI Article Generator</h1>
<form method="post">
<label for="prompt">Enter your prompt:</label><br>
<textarea id="prompt" name="prompt" rows="4" cols="50"></textarea><br> //e.g. write a blog post about black holes
<input type="submit" value="Generate Article">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<h2>Generated Article:</h2>";
// Enable output buffering
ob_start();
foreach ($stream as $chunk) {
$content = $chunk->choices[0]->delta->content ?? '';
echo $content;
// Flush the output buffer
ob_flush();
flush();
usleep(50000); // Add a small delay to allow the browser to catch up
}
// Clean up the output buffer
ob_end_flush();
}
?>
</body>
</html>
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am trying to created a streamed response but I am struggling with the documentation and a lack of examples on the web. Nothing I do seems to work, it always waits for the API to complete before displaying the response (rather than streaming the chunks).
I would be grateful if anyone can provide some guidance on implementing a streamed response using the code below. Any pointers would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions