|
...
|
...
|
@@ -62,15 +62,17 @@ class ChatLogic extends BaseLogic |
|
|
|
}
|
|
|
|
$data = ['message' => $message];
|
|
|
|
return response()->stream(function () use ($gptService, $data, $chatId) {
|
|
|
|
$streamFunction = $gptService->get_ai_chat($data);
|
|
|
|
ob_start();
|
|
|
|
$streamFunction();
|
|
|
|
$fullResponse = ob_get_clean(); // 获取完整内容
|
|
|
|
$fullResponse = ''; // 存储完整 AI 回复
|
|
|
|
$stream = $gptService->get_ai_chat($data); // 获取流
|
|
|
|
while (!$stream->eof()) {
|
|
|
|
$chunk = $stream->read(1024); // 逐步读取数据块
|
|
|
|
echo $gptService->en_sse_data($chunk); // **封装 SSE 数据**
|
|
|
|
ob_flush();
|
|
|
|
flush();
|
|
|
|
$fullResponse .= $chunk; // 拼接完整 AI 回复
|
|
|
|
}
|
|
|
|
// **流结束后,保存完整的 AI 回复**
|
|
|
|
$this->saveChatItem($chatId, $fullResponse ?: '服务器繁忙,请重试', 1);
|
|
|
|
echo $fullResponse;
|
|
|
|
ob_flush();
|
|
|
|
flush();
|
|
|
|
}, 200, [
|
|
|
|
"Content-Type" => "text/event-stream",
|
|
|
|
"Cache-Control" => "no-cache",
|
...
|
...
|
|