地球人都知道,FPM下面的cur各大AI接口确实也可以一次性返回,也可以SSE流式输出,常用的依靠定时任务生成文章当然没有什么,毕竟不存在什么并发数的。 image.png (60.36 KB, 下载次数: 0) 下载附件 保存到相册 5分钟前 上传 大不了PHP里面多了些慢请求哈。 不过实测并发高峰期间确实会存在网站响应慢的情况。那怎么解决这个痛点呢?实际异步非阻塞才是我们需要的哈。比如小编的冷场定时任务,流式并且还不占用什么服务器资源的。毕竟是非阻塞的。 [ol]private static function chatColdMonitor(){global $public_r;$chatinfo = self::Adminchatconfig();// 读取聊天室后台配置$msgId= bin2hex(random_bytes(10)) . dechex(time());$robotAvatar = $public_r['add_pcurl'] . "/e/extend/chat/img/xiaobing.png";$headers= ['Content-Type' => 'application/json','Authorization' => 'Bearer ' . $public_r['add_siliconflow']];$lastSpeakTime = self::$redis->get('chat:last_msg_time');//最后发言时间$coldLimit=$chatinfo['chatcoldtime'] * 60;// 聊天室冷场默认配置30分钟$nowTime= time();if (!$lastSpeakTime) {self::$redis->set('chat:last_msg_time', $nowTime);return;}$diff = $nowTime - $lastSpeakTime;if ($diff = 6 && $hour = 12 && $hour = 14 && $hour = 18 && $hour $chatinfo['chataimodelname'],'messages'=> [['role' => 'user', 'content' => $prompt]],'stream'=>true,'temperature'=> 1];self::asynccurlRequest('request', self::$aiapiUrl, ['method'=> 'POST','headers' => $headers,'data'=>json_encode($body, JSON_UNESCAPED_UNICODE),'progress' => function ($buffer) use ($robotAvatar, $msgId) { $lines = explode("\n", $buffer);foreach ($lines as $line) {$line = trim($line);if(!$line) continue;if(strpos($line, 'data: ') !== 0) continue;$json = substr($line, 6);if ($json === '[DONE]') {Gateway::sendToAll(json_encode(['type' => 'ai_stream_end','msgid' => $msgId,'userid' =>0,'from_chatuname' => '小冰(Ai老铁)']));continue;}$data = json_decode($json, true);$content = $data['choices'][0]['delta']['content'] ?? '小冰(Ai老铁)累了哦';if(!$content) continue;Gateway::sendToAll(json_encode(['type' => 'ai_stream','msgid' => $msgId,'userid' =>0,'from_chatuname' => '小冰(Ai老铁)','content' => $content,'headerimg' => $robotAvatar,'time' => date('Y-m-d H:i:s')]));} },'success' => function () {},], null, null, true);} [/ol]复制代码总结,原生的FPM下确实不适合做对接AI接口的。占用服务器资源大,阻塞非常严重经常500错误的。 语气, 聊天室