diff --git a/apikey.php b/apikey.php new file mode 100644 index 0000000..2396078 --- /dev/null +++ b/apikey.php @@ -0,0 +1,4 @@ + +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 +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 +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 diff --git a/getpicture.php b/getpicture.php new file mode 100644 index 0000000..0309ab7 --- /dev/null +++ b/getpicture.php @@ -0,0 +1,75 @@ +\n"; +$line = 0; +$handle = fopen(__DIR__ . "/apikey.php", "r") or die("Writing file failed."); +if ($handle) { + while (($buffer = fgets($handle)) !== false) { + $line++; + if ($line == 2) { + $OPENAI_API_KEY = str_replace("\n", "", $buffer); + } + if ($line > 2) { + $content .= $buffer; + } + } + fclose($handle); +} +$content .= $OPENAI_API_KEY . "\n"; +$handle = fopen(__DIR__ . "/apikey.php", "w") or die("Writing file failed."); +if ($handle) { + fwrite($handle, $content); + fclose($handle); +} + +//如果首页开启了输入自定义apikey,则采用用户输入的apikey +if (isset($_SESSION['key'])) { + $OPENAI_API_KEY = $_SESSION['key']; +} +session_write_close(); +$headers = [ + 'Accept: application/json', + 'Content-Type: application/json', + 'Authorization: Bearer ' . $OPENAI_API_KEY +]; + +setcookie("errcode", ""); //EventSource无法获取错误信息,通过cookie传递 +setcookie("errmsg", ""); + +$ch = curl_init(); +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); +curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); +curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/images/generations'); +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_CONNECTTIMEOUT, 120); // 设置连接超时时间为30秒 +curl_setopt($ch, CURLOPT_MAXREDIRS, 3); // 设置最大重定向次数为3次 +curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 允许自动重定向 +curl_setopt($ch, CURLOPT_AUTOREFERER, true); // 自动设置Referer +//curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:1081"); +$responsedata = curl_exec($ch); +echo $responsedata; +curl_close($ch); + + +session_start(); +$questionarr = json_decode($postData, true); +$answer = json_decode($responsedata, true); +$goodanswer = '![IMG](' . $answer['data'][0]['url'] . ')'; +$filecontent = $_SERVER["REMOTE_ADDR"] . " | " . date("Y-m-d H:i:s") . "\n"; +$filecontent .= "Q:" . $questionarr['prompt'] . "\nA:" . trim($goodanswer) . "\n----------------\n"; +$myfile = fopen(__DIR__ . "/chat.txt", "a") or die("Writing file failed."); +fwrite($myfile, $filecontent); +fclose($myfile); diff --git a/js/chat.js b/js/chat.js index 44374fb..2abf867 100644 --- a/js/chat.js +++ b/js/chat.js @@ -181,6 +181,23 @@ $(document).ready(function () { time: false //取消自动关闭 }); + function draw() { + $.get("getpicture.php", function (data) { + layer.close(loading); + layer.msg("处理成功!"); + answer = randomString(16); + $("#article-wrapper").append('
  • '); + for (var j = 0; j < prompt.length; j++) { + $("#q" + answer).children('pre').text($("#q" + answer).children('pre').text() + prompt[j]); + } + $("#article-wrapper").append('
  • '); + $("#kw-target").val(""); + $("#kw-target").attr("disabled", false); + autoresize(); + $("#ai-btn").html('发送'); + if (!isMobile()) $("#kw-target").focus(); + }, "json"); + } function streaming() { var es = new EventSource("stream.php"); var isstarted = true; @@ -307,20 +324,37 @@ $(document).ready(function () { } - $.ajax({ - cache: true, - type: "POST", - url: "setsession.php", - data: { - message: prompt, - context: (!($("#keep").length) || ($("#keep").prop("checked"))) ? JSON.stringify(contextarray) : '[]', - key: ($("#key").length) ? ($("#key").val()) : '', - }, - dataType: "json", - success: function (results) { - streaming(); - } - }); + if (prompt.charAt(0) === '画') { + $.ajax({ + cache: true, + type: "POST", + url: "setsession.php", + data: { + message: prompt, + context: '[]', + key: ($("#key").length) ? ($("#key").val()) : '', + }, + dataType: "json", + success: function (results) { + draw(); + } + }); + } else { + $.ajax({ + cache: true, + type: "POST", + url: "setsession.php", + data: { + message: prompt, + context: (!($("#keep").length) || ($("#keep").prop("checked"))) ? JSON.stringify(contextarray) : '[]', + key: ($("#key").length) ? ($("#key").val()) : '', + }, + dataType: "json", + success: function (results) { + streaming(); + } + }); + } } diff --git a/key.php b/key.php new file mode 100644 index 0000000..e60ea22 --- /dev/null +++ b/key.php @@ -0,0 +1,265 @@ +\n" . $_POST["message"]); + fclose($handle); + exit; + } + } elseif ($_POST["action"] == "check") { + $lines = explode("\n", $_POST["message"]); + $i = 0; + $validkey = ""; + $invalidkey = ""; + while ($i < count($lines)) { + $line = $lines[$i]; + $headers = [ + 'Accept: application/json', + 'Content-Type: application/json', + 'Authorization: Bearer ' . $line + ]; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/models/gpt-3.5-turbo'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $response = curl_exec($ch); + curl_close($ch); + $complete = json_decode($response); + if (isset($complete->error)) { + $invalidkey .= $line . "\n"; + } else { + $validkey .= $line . "\n"; + } + $i++; + } + echo $validkey; + exit; + } + } + $line = 0; + $handle = @fopen(__DIR__ . "/apikey.php", "r"); + if ($handle) { + while (($buffer = fgets($handle)) !== false) { + $line++; + if ($line > 1) { + $content .= $buffer; + } + } + fclose($handle); + } +?> + + + + + + + + API_KEY配置信息 + + + + + + +
    +

    API_KEY配置信息

    + + +
    + + + + + + + + + + + + + 登录页面 + + + + + + + +
    +

    API_KEY管理后台

    +

    +

    +

    +
    + + + \ No newline at end of file diff --git a/pictureproxy.php b/pictureproxy.php new file mode 100644 index 0000000..e564df0 --- /dev/null +++ b/pictureproxy.php @@ -0,0 +1,8 @@ + "gpt-3.5-turbo", - "temperature" => 0, - "stream" => true, - "messages" => [], -]; -if (!empty($context)) { - $context = array_slice($context, -5); - foreach ($context as $message) { - $postData['messages'][] = ['role' => 'user', 'content' => str_replace("\n", "\\n", $message[0])]; - $postData['messages'][] = ['role' => 'assistant', 'content' => str_replace("\n", "\\n", $message[1])]; +if (mb_substr($_POST["message"], 0, 1, 'UTF-8') === '画') { + $postData = [ + "prompt" => $_POST['message'], + "n" => 1, + "size" => "1024x1024" + ]; +} else { + $postData = [ + "model" => "gpt-3.5-turbo", + "temperature" => 0, + "stream" => true, + "messages" => [], + ]; + if (!empty($context)) { + $context = array_slice($context, -5); + foreach ($context as $message) { + $postData['messages'][] = ['role' => 'user', 'content' => str_replace("\n", "\\n", $message[0])]; + $postData['messages'][] = ['role' => 'assistant', 'content' => str_replace("\n", "\\n", $message[1])]; + } } + $postData['messages'][] = ['role' => 'user', 'content' => $_POST['message']]; } -$postData['messages'][] = ['role' => 'user', 'content' => $_POST['message']]; $postData = json_encode($postData); session_start(); $_SESSION['data'] = $postData; diff --git a/stream.php b/stream.php index b6e364e..b86b6d4 100644 --- a/stream.php +++ b/stream.php @@ -1,14 +1,42 @@ \n"; +$line = 0; +$handle = fopen(__DIR__ . "/apikey.php", "r") or die("Writing file failed."); +if ($handle) { + while (($buffer = fgets($handle)) !== false) { + $line++; + if ($line == 2) { + $OPENAI_API_KEY = str_replace("\n", "", $buffer); + } + if ($line > 2) { + $content .= $buffer; + } + } + fclose($handle); +} +$content .= $OPENAI_API_KEY . "\n"; +$handle = fopen(__DIR__ . "/apikey.php", "w") or die("Writing file failed."); +if ($handle) { + fwrite($handle, $content); + fclose($handle); +} + +//如果首页开启了输入自定义apikey,则采用用户输入的apikey if (isset($_SESSION['key'])) { $OPENAI_API_KEY = $_SESSION['key']; } +session_write_close(); $headers = [ 'Accept: application/json', 'Content-Type: application/json', @@ -19,6 +47,7 @@ setcookie("errcode", ""); //EventSource无法获取错误信息,通过cookie setcookie("errmsg", ""); $callback = function ($ch, $data) { + global $responsedata; $complete = json_decode($data); if (isset($complete->error)) { setcookie("errcode", $complete->error->code); @@ -35,12 +64,14 @@ $callback = function ($ch, $data) { 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服务器超负荷 + if (strpos($complete->error->message, "That model is currently overloaded") === 0) { //OpenAI模型超负荷 setcookie("errcode", "model_overloaded"); } + $responsedata = $data; } else { echo $data; - $_SESSION['response'] .= $data; + $responsedata .= $data; + flush(); } return strlen($data); }; @@ -53,15 +84,20 @@ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); +curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // 设置连接超时时间为30秒 +curl_setopt($ch, CURLOPT_MAXREDIRS, 3); // 设置最大重定向次数为3次 +curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 允许自动重定向 +curl_setopt($ch, CURLOPT_AUTOREFERER, true); // 自动设置Referer //curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:1081"); curl_exec($ch); +curl_close($ch); $answer = ""; -if (substr(trim($_SESSION['response']), -6) == "[DONE]") { - $_SESSION['response'] = substr(trim($_SESSION['response']), 0, -6) . "{"; +if (substr(trim($responsedata), -6) == "[DONE]") { + $responsedata = substr(trim($responsedata), 0, -6) . "{"; } -$responsearr = explode("}\n\ndata: {", $_SESSION['response']); +$responsearr = explode("}\n\ndata: {", $responsedata); foreach ($responsearr as $msg) { $contentarr = json_decode("{" . trim($msg) . "}", true); @@ -69,11 +105,9 @@ foreach ($responsearr as $msg) { $answer .= $contentarr['choices'][0]['delta']['content']; } } - -$questionarr = json_decode($_SESSION['data'], true); +$questionarr = json_decode($postData, true); $filecontent = $_SERVER["REMOTE_ADDR"] . " | " . date("Y-m-d H:i:s") . "\n"; $filecontent .= "Q:" . end($questionarr['messages'])['content'] . "\nA:" . trim($answer) . "\n----------------\n"; $myfile = fopen(__DIR__ . "/chat.txt", "a") or die("Writing file failed."); fwrite($myfile, $filecontent); fclose($myfile); -curl_close($ch);