Won’t work reliably… I tried it. I went back to not streaming, changed my server and PHP settings, and stopped using Cloudflare. Now it works perfectly. But maybe you can find a solution. In my opinion, you can’t manipulate the data in CURLOPT_WRITEFUNCTION directly to get the part after “data:”. The stream return is not a JSON.
My code for streaming curl - works btw:
function get_chat_completion($api_key, $model, $message, $temperature, $max_tokens, $top_p, $frequency_penalty, $presence_penalty, $stop) {
$url = ‘https://api.openai.com/v1/chat/completions’;
$system = “You are a helpful assistant. Do not remind me what I asked you for. Do not apologize. Do not self-reference.”;
$data = array(
‘model’ => $model,
‘temperature’ => $temperature,
‘max_tokens’ => $max_tokens,
‘top_p’ => $top_p,
‘frequency_penalty’ => $frequency_penalty,
‘presence_penalty’ => $presence_penalty,
‘stop’ => $stop,
‘stream’ => true,
‘messages’ => array(
array(
‘role’ => ‘system’,
‘content’ => $system
),
array(
‘role’ => ‘user’,
‘content’ => $message
)
)
);
$headers = array(
‘Content-Type: application/json’,
'Authorization: Bearer ’ . $api_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 300);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 300);
// Use php://memory to store the response
$response_buffer = fopen('php://memory', 'w+');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use ($response_buffer) {
# write the response to the buffer
fwrite($response_buffer, $data);
return strlen($data);
});
$response = curl_exec($ch);
if($response === false){
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
// Get the response from the buffer
rewind($response_buffer);
$response = stream_get_contents($response_buffer);
fclose($response_buffer);
return $response;
}