|
...
|
...
|
@@ -63,51 +63,11 @@ class ChatLogic extends BaseLogic |
|
|
|
}
|
|
|
|
$data = ['message' => $message];
|
|
|
|
$stream = $gptService->get_ai_chat($data); // 获取流
|
|
|
|
header('Content-Type: text/event-stream');
|
|
|
|
header('Cache-Control: no-cache');
|
|
|
|
header('Connection: keep-alive');
|
|
|
|
$aiResponse = '';
|
|
|
|
while (!$stream->eof()) {
|
|
|
|
$chunk = $stream->read(1024);
|
|
|
|
$chunk = str_replace(chr(1), '', $chunk);
|
|
|
|
if ($chunk !== false) {
|
|
|
|
$aiResponse .= $chunk; // 累积完整的 AI 回复
|
|
|
|
// 使用 en_sse_data 格式化流数据
|
|
|
|
echo $gptService->en_sse_data(trim($chunk));
|
|
|
|
ob_flush();
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 确保数据不是空的
|
|
|
|
if (!empty(trim($aiResponse)) && $chatId) {
|
|
|
|
$this->saveChatItem($chatId, trim($aiResponse), 1); // 存入数据库
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :获取一行数据
|
|
|
|
* @name :getStreamContentLine
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2025/4/2 22:42
|
|
|
|
*/
|
|
|
|
public function getStreamContentLine($stream){
|
|
|
|
$text = '';
|
|
|
|
while (!$stream->eof()){
|
|
|
|
// 读取一个字符串
|
|
|
|
$t = $this->stream->read(1);
|
|
|
|
$this->body .= $t;
|
|
|
|
if($t === "\n"){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// 结束了
|
|
|
|
if(ord($t)==1){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$text .= $t;
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
$streamHelper = new Stream($stream);
|
|
|
|
$streamHelper->echo();
|
|
|
|
$res_message = $streamHelper->getData();
|
|
|
|
$this->saveChatItem($chatId, $res_message,1);
|
|
|
|
return $res_message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
...
|
...
|
|