PHP实现chatGPT流式输出代码,OpenAI对接,支持GPT3.5/GPT4
短信预约 -IT技能 免费直播动态提醒
源码下载地址:https://gitee.com/haoyachengge/chatgpt-speed.git
header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); header('Connection: keep-alive'); header('X-Accel-Buffering: no'); $apiKey = config("open.apiKey"); $apiUrl = config("open.apiHost").'/v1/chat/completions'; //提问数据 $message = $request->param('message')??'hello!'; //分组 $group_id = $request->param('group_id'); //客户端ip $ip = $request->ip(); // 连续对话需要带着上一个问题请求接口 $lastMsg = Db::table('ai_chat_msgs')->where([ ['ip', '=', $ip], ['group_id', '=', $group_id], ['ctime', '>', (time() - 300)] ])->order('id desc')->find(); // 如果超长,就不关联上下文了 if ($lastMsg && (mb_strlen($lastMsg['message']) + mb_strlen($lastMsg['response']) + mb_strlen($message) < 3800)) { $messages[] = [ 'role' => 'user', 'content' => $lastMsg['message'] ]; $messages[] = [ 'role' => 'assistant', 'content' => $lastMsg['response'] ]; } $messages[] = [ 'role'=>'user', 'content'=>$message ]; //返回数据 $response = ''; //不完整的数据 $imperfect = ''; $callback = function($ch, $data) use ($message, $ip, $group_id){ global $response, $imperfect; $dataLength = strlen($data); //有可能会返回不完整的数据 if($imperfect){ $data = $imperfect . $data; $imperfect = ''; }else{ if (substr($data, -1) !== "\n") { $imperfect = $data; return $dataLength; } } $complete = @json_decode($data); if(isset($complete->error)){ echo 'data:'.$complete->error->code."\n\n"; ob_flush();flush(); exit; } $word = $this->parseData($data); $word = str_replace("\n", '
', $word); if($word == 'data: [DONE]' || $word == 'data: [CONTINUE]'){ if (!empty($response)) { Db::table('ai_chat_msgs')->insert([ 'ip' => $ip, 'group_id' => $group_id, 'message' => $message, 'response' => $response, 'ctime' => time() ]); $response = ''; } ob_flush();flush(); }else{ $response .= $word; echo "data:".$word."\n\n"; ob_flush();flush(); } return $dataLength; }; $postData = [ 'model' => config("open.model"), 'messages' => $messages, 'temperature' => config("open.temperature"), 'max_tokens' => config("open.max_tokens"), 'frequency_penalty' => 0, 'presence_penalty' => 0.6, 'stream' => true ]; $headers = [ 'Accept: application/json', 'Content-Type: application/json', 'Authorization: Bearer ' . $apiKey ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_URL, $apiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData)); curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); curl_exec($ch); exit;
本文是sse实现方式,非常的简单。当然也可以用websocket方式实现,我也会继续更新
来源地址:https://blog.csdn.net/qq_35627940/article/details/131052053
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341