I'm working with the ChatGPT API in PHP.
I'm trying to stream the ChatGPT completion text in PHP. When I do it on my local machine it works fine, but my server is using PHP FPM and as a result, it isn't streaming text but is outputting all the text once the request is complete. It seems like it's stored in the output buffer or something and the PHP FPM settings won't allow the text to be streamed, it's only outputting it when its all done.
Anybody know how to get the normal text streaming behavior with PHP FPM?
Code:
<?php // load_ai_stream.php file
// https://github.com/orhanerday/open-ai
require_once("openai.php");
$open_ai = new OpenAi("OpenAIKey");
$opts = [
'prompt' => "Write an essay about the top 5 colleges in the United States",
'temperature' => 0.9,
"max_tokens" => 2000,
"frequency_penalty" => 0,
"presence_penalty" => 0.6,
"stream" => true,
];
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$open_ai->completion($opts, function ($curl_info, $data) {
echo $data . "<br><br>";
echo PHP_EOL;
ob_flush();
flush();
return strlen($data);
});
?>
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Page Title</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="">
<style>
</style>
<script src=""></script>
<body>
<div id="divID">Hello</div>
<script>
var eventSource = new EventSource("/load_ai_stream.php");
var div = document.getElementById('divID');
eventSource.onmessage = function (e) {
if (e.data === "[DONE]") {
div.innerHTML += "<br><br>Hello";
} else {
var data = JSON.parse(e.data);
div.innerHTML += data.choices[0].text;
}
};
eventSource.onerror = function (e) {
console.log(e);
};
</script>
</body>
</html>
Check your php.ini, and make sure that it is saving sessions to a directory writable by the appropriate process (such as /tmp), You can also try setting
zlib.output_compression = Offin the php.ini file.If you're also using nginx, use the header
X-Accel-Buffering: no, which disables buffering in nginx.