|
@@ -63,51 +63,11 @@ class ChatLogic extends BaseLogic |
|
@@ -63,51 +63,11 @@ 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
|
- header('Content-Type: text/event-stream');
|
|
|
|
67
|
- header('Cache-Control: no-cache');
|
|
|
|
68
|
- header('Connection: keep-alive');
|
|
|
|
69
|
- $aiResponse = '';
|
|
|
|
70
|
- while (!$stream->eof()) {
|
|
|
|
71
|
- $chunk = $stream->read(1024);
|
|
|
|
72
|
- $chunk = str_replace(chr(1), '', $chunk);
|
|
|
|
73
|
- if ($chunk !== false) {
|
|
|
|
74
|
- $aiResponse .= $chunk; // 累积完整的 AI 回复
|
|
|
|
75
|
- // 使用 en_sse_data 格式化流数据
|
|
|
|
76
|
- echo $gptService->en_sse_data(trim($chunk));
|
|
|
|
77
|
- ob_flush();
|
|
|
|
78
|
- flush();
|
|
|
|
79
|
- }
|
|
|
|
80
|
- }
|
|
|
|
81
|
- // 确保数据不是空的
|
|
|
|
82
|
- if (!empty(trim($aiResponse)) && $chatId) {
|
|
|
|
83
|
- $this->saveChatItem($chatId, trim($aiResponse), 1); // 存入数据库
|
|
|
|
84
|
- }
|
|
|
|
85
|
- return true;
|
|
|
|
86
|
- }
|
|
|
|
87
|
-
|
|
|
|
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;
|
66
|
+ $streamHelper = new Stream($stream);
|
|
|
|
67
|
+ $streamHelper->echo();
|
|
|
|
68
|
+ $res_message = $streamHelper->getData();
|
|
|
|
69
|
+ $this->saveChatItem($chatId, $res_message,1);
|
|
|
|
70
|
+ return $res_message;
|
|
111
|
}
|
71
|
}
|
|
112
|
|
72
|
|
|
113
|
/**
|
73
|
/**
|