源码下载地址:https://gitee.com/haoyachengge/chatgpt-speed.git文章来源:https://www.toymoban.com/news/detail-619326.html
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", '<br/>', $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://www.toymoban.com/news/detail-619326.html
到了这里,关于PHP实现chatGPT流式输出代码,OpenAI对接,支持GPT3.5/GPT4的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!