Stream.php
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
namespace App\Helper;
/**
* 流输出
* @author:dc
* @time 2024/1/2 14:46
* Class Stream
* @package GlobalSo\Tool\Gpt\Resource
*/
class Stream extends Resource{
/**
* body内容
* @var string
*/
private $body = '';
/**
* 流输出的文本
* @var string
*/
private $text = '';
/**
* http 状态
* @var int
*/
private $status = 200;
/**
* @var \Psr\Http\Message\StreamInterface
*/
private $stream;
/**
* @var array 使用了多少token
*/
private $usage = [];
/**
* Resource constructor.
* @param \Psr\Http\Message\StreamInterface|array $response
*/
public function __construct($response)
{
if($response instanceof \Psr\Http\Message\StreamInterface){
$this->stream = $response;
}
// 数组,带上下文
elseif(is_array($response)){
$this->stream = false;
// 回答的文本
$this->text = end($response);
// 计算token
$this->usage = [
[
'model'=>'',
]
];
}
}
/**
* 最后一行
* @var array
*/
private $endLine = [];
/**
* 获取流输出内容
* @return null
* @author:dc
* @time 2024/1/2 13:57
*/
public function getStreamContent(\Closure $call)
{
// 文本
if($this->stream===false){
$this->body = $this->text;
$call($this->text);
}
// 流输出
else{
while (!$this->stream->eof()) {
// 获取一行数据
$line = $this->getStreamContentLine();
// 必须要有数据
if($line){
// 解析成数组
$arr = @json_decode($line,true);
// 必须是一个数组
if(is_array($arr)){
// 是否是函数
if(!empty($arr['func'])){
$this->func = $arr['func'] ? : ($arr['tool_calls']??[]);
continue;
}
// 这里是新版本
// 文本
if(isset($arr['text'])){
// 拼接
$this->text .= $arr['text'];
// 调用
$call($arr['text']);
}
// 到了最后一行
if (isset($arr['usage'])){
$this->usage = $arr['usage'];
$this->endLine = $arr;
}
}else{
// 拼接
$this->text .= $line;
// 这里兼容下老版本
$call($line);
}
}
}
// 兼容老版本 老版本没办法获取 实际使用了多少token
if(!$this->usage){
$this->usage = [
[
'model'=>'',
]
];
}
}
}
/**
* 流 读取一行
* @return string
* @author:dc
* @time 2024/1/2 14:16
*/
private function getStreamContentLine(){
$text = '';
while (!$this->stream->eof()){
// 读取一个字符串
$t = $this->stream->read(1);
$this->body .= $t;
if($t === "\n"){
break;
}
// 结束了
if(ord($t)==1){
break;
}
$text .= $t;
}
return $text;
}
/**
* 流输出的所有内容
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* @return int
*/
public function getCode(): int
{
return 200;
}
/**
* 这个是文本内容,就是回答的内容
* @return array|string
* @author:dc
* @time 2024/1/2 14:57
*/
public function getData()
{
return $this->text;
}
/**
* @return string
*/
public function getMessage(): string
{
return '';
}
/**
* @return array
* @author:dc
* @time 2024/1/2 14:57
*/
public function getUsage(): array
{
return $this->usage;
}
/**
* 是否已经输出过头部了
* @var bool
*/
protected static $isHeader = false;
/**
* 是否是sse输出
* @var bool
*/
public static $echoSse = false;
/**
* 设置头部
* @param false $sse
* @author:dc
* @time 2024/5/31 15:02
*/
public static function setStreamHeader(array $header=[]){
// 默认配置的 头信息 输出一次即可
if(!self::$isHeader){
// 流输出 必须的 头信息
if(self::$echoSse) header("Content-Type:event-stream;Charset=UTF-8;");//event-stream 开启这个数据必须是规定格式
header("cache-control:no-cache;"); // 告诉浏览器不要进行数据缓存
header('X-Accel-Buffering: no'); // 关键是加了这一行。告诉浏览器不进行输出的缓冲
header('Access-Control-Expose-Headers: Content-Disposition, Content-Length, X-Content-Range, X-Duration');
header('Content-Type: application/json'); // json数据头
header('Access-Control-Allow-Origin:*'); // 这个是 跨域
self::$isHeader = true;
}
// 输出其他header
foreach ($header as $head){
header($head);
}
}
/**
* 其他地方调用,在ai返回前后都可以调用这个
* @param $data
* @param string $type 数据类型
* @author:dc
* @time 2024/5/31 15:05
*/
public static function echo_flush($data,string $type='text'){
self::setStreamHeader();
echo self::$echoSse ? en_sse_data($data,$type) : $data;
ob_flush();
flush();
}
/**
* 输出 信息到前端
* @author:dc
* @time 2024/5/31 10:16
*/
public function echo(){
// 如果用户断开,继续脚本的运行
ignore_user_abort(1);
set_time_limit(400);
// // 先把之前的内容 也发送到浏览器
// @ob_implicit_flush(); // 开启隐式刷新 使用 echo函数时会立即发送到浏览器 开启后就不需要flush调用了
// 输出内容
$this->getStreamContent(function ($text) {
self::echo_flush($text);
});
if(self::$debugInfo){
self::echo_flush($this->endLine['debug']??[],'debug');
}
}
}