error)) { setcookie("errcode", $complete->error->code); setcookie("errmsg", $data); if (strpos($complete->error->message, "Rate limit reached") === 0) { //访问频率超限错误返回的code为空,特殊处理一下 setcookie("errcode", "rate_limit_reached"); } if (strpos($complete->error->message, "Your access was terminated") === 0) { //违规使用,被封禁,特殊处理一下 setcookie("errcode", "access_terminated"); } if (strpos($complete->error->message, "You didn't provide an API key") === 0) { //未提供API-KEY setcookie("errcode", "no_api_key"); } if (strpos($complete->error->message, "You exceeded your current quota") === 0) { //API-KEY余额不足 setcookie("errcode", "insufficient_quota"); } if (strpos($complete->error->message, "That model is currently overloaded") === 0) { //OpenAI服务器超负荷 setcookie("errcode", "model_overloaded"); } } else { echo $data; $_SESSION['response'] .= $data; } return strlen($data); }; curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); if ($configs["proxy"]) { curl_setopt($ch, CURLOPT_PROXY, $configs["proxy"]); } curl_exec($ch); $answer = ""; if (substr(trim($_SESSION['response']), -6) == "[DONE]") { $_SESSION['response'] = substr(trim($_SESSION['response']), 0, -6) . "{"; } $responsearr = explode("}\n\ndata: {", $_SESSION['response']); foreach ($responsearr as $msg) { $contentarr = json_decode("{" . trim($msg) . "}", true); if (isset($contentarr['choices'][0]['delta']['content'])) { $answer .= $contentarr['choices'][0]['delta']['content']; } } $questionarr = json_decode($_SESSION['data'], true); $filecontent = $_SERVER["REMOTE_ADDR"] . " | " . date("Y-m-d H:i:s") . "\n"; $filecontent .= "Q:" . end($questionarr['messages'])['content'] . "\nA:" . trim($answer) . "\n----------------\n"; file_put_contents(__DIR__ . "/log/" . date('Y-m-d') . "_chat.txt", $filecontent, FILE_APPEND); // $myfile = fopen(__DIR__ . "/chat.txt", "a") or die("Writing file failed."); // fwrite($myfile, $filecontent); // fclose($myfile); curl_close($ch);