作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !1711
@@ -68,13 +68,12 @@ class ChatLogic extends BaseLogic @@ -68,13 +68,12 @@ class ChatLogic extends BaseLogic
68 header('Connection: keep-alive'); 68 header('Connection: keep-alive');
69 $aiResponse = ''; 69 $aiResponse = '';
70 while (!$stream->eof()) { 70 while (!$stream->eof()) {
71 - $chunk = $stream->read(1); 71 + $chunk = $stream->read(1024);
72 $chunk = str_replace(chr(1), '', $chunk); 72 $chunk = str_replace(chr(1), '', $chunk);
73 if ($chunk !== false) { 73 if ($chunk !== false) {
74 - $chunk = json_decode($chunk,true);  
75 - $aiResponse .= $chunk['text']; // 累积完整的 AI 回复 74 + $aiResponse .= $chunk; // 累积完整的 AI 回复
76 // 使用 en_sse_data 格式化流数据 75 // 使用 en_sse_data 格式化流数据
77 - echo $gptService->en_sse_data(trim($chunk['text'])); 76 + echo $gptService->en_sse_data(trim($chunk));
78 ob_flush(); 77 ob_flush();
79 flush(); 78 flush();
80 } 79 }
@@ -87,6 +86,31 @@ class ChatLogic extends BaseLogic @@ -87,6 +86,31 @@ class ChatLogic extends BaseLogic
87 } 86 }
88 87
89 /** 88 /**
  89 + * @remark :获取一行数据
  90 + * @name :getStreamContentLine
  91 + * @author :lyh
  92 + * @method :post
  93 + * @time :2025/4/2 22:42
  94 + */
  95 + public function getStreamContentLine($stream){
  96 + $text = '';
  97 + while (!$stream->eof()){
  98 + // 读取一个字符串
  99 + $t = $this->stream->read(1);
  100 + $this->body .= $t;
  101 + if($t === "\n"){
  102 + break;
  103 + }
  104 + // 结束了
  105 + if(ord($t)==1){
  106 + break;
  107 + }
  108 + $text .= $t;
  109 + }
  110 + return $text;
  111 + }
  112 +
  113 + /**
90 * @remark :创建一条新会话 114 * @remark :创建一条新会话
91 * @name :saveChat 115 * @name :saveChat
92 * @author :lyh 116 * @author :lyh