PHP实现chatGPT流式输出代码,OpenAI对接,支持GPT3.5/GPT4

 源码下载地址: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方式实现,我也会继续更新


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部