正在显示
1 个修改的文件
包含
12 行增加
和
4 行删除
| @@ -63,10 +63,18 @@ class ChatLogic extends BaseLogic | @@ -63,10 +63,18 @@ class ChatLogic extends BaseLogic | ||
| 63 | } | 63 | } |
| 64 | $data = ['message' => $message]; | 64 | $data = ['message' => $message]; |
| 65 | $stream = $gptService->get_ai_chat($data); // 获取流 | 65 | $stream = $gptService->get_ai_chat($data); // 获取流 |
| 66 | - $streamHelper = new Stream($stream); | ||
| 67 | - $streamHelper->echo(); | ||
| 68 | - $res_message = $streamHelper->getData(); | ||
| 69 | - $this->saveChatItem($chatId, $res_message,1); | 66 | + header('Content-Type: text/event-stream'); |
| 67 | + header('Cache-Control: no-cache'); | ||
| 68 | + header('Connection: keep-alive'); | ||
| 69 | + while (!feof($stream)) { | ||
| 70 | + $chunk = fread($stream, 1024); | ||
| 71 | + if ($chunk !== false) { | ||
| 72 | + echo "data: " . trim($chunk) . "\n\n"; | ||
| 73 | + ob_flush(); | ||
| 74 | + flush(); | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + fclose($stream); | ||
| 70 | return true; | 78 | return true; |
| 71 | } | 79 | } |
| 72 | 80 |
-
请 注册 或 登录 后发表评论