From bbffb5ac7db2a134cfe86401dd7eb74740dbced7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=A7=8B=E5=AE=9E?= <974568302@qq.com> Date: Thu, 11 May 2023 14:21:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=85=8D=E7=BD=AE=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++++ README.md | 17 +++++++++++++++++ chat.txt | 4 ---- config.json_example | 5 +++++ index.php | 11 +++++++---- js/chat.js | 2 +- log/此文件夹用于存放日志文件.txt | 2 ++ stream.php | 21 +++++++++++++++++---- 8 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 .gitignore delete mode 100644 chat.txt create mode 100644 config.json_example create mode 100644 log/此文件夹用于存放日志文件.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9595c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/log/* +!/log/此文件夹用于存放日志文件.txt +config.json +favicon.ico diff --git a/README.md b/README.md index 4a2ed45..8098de5 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,23 @@ ChatGPT的横空出世真的改变了世界,用过的人都知道ChatGPT完全 ![t2](https://user-images.githubusercontent.com/5563148/232330566-c6ea7fb3-474f-45e4-adda-37f3db27b92a.jpg) +------ +**调整部分内容为配置文件形式优化拉取代码配置信息冲突** +拉取代码后请复制config.json_example 为 config.json +linux下 进入项目目录执行 cp config.json_example config.json + +配置定义如下 +// 控制是否显示私人密钥输入框 +"personalKey": false, +//设置全局密钥 开启私人密钥输入框后此key将不启用 +"key": "sk-replace_with_your_api_key_dude", +//代理服务器未设置则不启用代理示例 http://127.0.0.1 +"proxy": "http://127.0.0.1" + +------ +**log调整每日为单独文件** +**注意请赋予站点修改log文件夹内内容权限** + ------ **本项目常见问题:** diff --git a/chat.txt b/chat.txt deleted file mode 100644 index 7b0db87..0000000 --- a/chat.txt +++ /dev/null @@ -1,4 +0,0 @@ -127.0.0.1 | 2023-03-08 10:37:47 -Q:你好 -A:你好!有什么我可以帮助你的吗? ----------------- diff --git a/config.json_example b/config.json_example new file mode 100644 index 0000000..7c98b5d --- /dev/null +++ b/config.json_example @@ -0,0 +1,5 @@ +{ + "personalKey": false, + "key": "sk-replace_with_your_api_key_dude", + "proxy": "" +} \ No newline at end of file diff --git a/index.php b/index.php index e387627..03c73fa 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,9 @@ 

请点击右上角,选择”在浏览器打开“

"; @@ -35,6 +39,7 @@ $type = "个人"; +
@@ -50,13 +55,11 @@ $type = "个人";
-
-
  连续对话: diff --git a/js/chat.js b/js/chat.js index 454c582..7585fc3 100644 --- a/js/chat.js +++ b/js/chat.js @@ -163,7 +163,7 @@ $(document).ready(function () { }); function send_post() { - if (($('#key').length) && ($('#key').val().length != 51)) { + if ($("#keydiv").attr("style").indexOf("display:none") < 0 && ($('#key').length) && ($('#key').val().length != 51)) { layer.msg("请输入正确的API-KEY", { icon: 5 }); return; } diff --git a/log/此文件夹用于存放日志文件.txt b/log/此文件夹用于存放日志文件.txt new file mode 100644 index 0000000..df82dda --- /dev/null +++ b/log/此文件夹用于存放日志文件.txt @@ -0,0 +1,2 @@ +此文件夹用于存放日志文件.txt +若无法自动生成YYYY-mm-dd_chat.txt的日志文件请给php允许着log文件夹修改权限 或 设置log文件夹为所有用户皆可修改 \ No newline at end of file diff --git a/stream.php b/stream.php index d683107..2487db2 100644 --- a/stream.php +++ b/stream.php @@ -6,11 +6,15 @@ session_start(); $postData = $_SESSION['data']; $_SESSION['response'] = ""; $ch = curl_init(); -$OPENAI_API_KEY = "sk-replace_with_your_api_key_dude"; +// 读取JSON配置文件 +$json = file_get_contents('config.json'); +// 解码JSON配置文件数据 +$configs = json_decode($json, true); +$OPENAI_API_KEY = $configs["key"]; if (isset($_SESSION['key'])) { $OPENAI_API_KEY = $_SESSION['key']; } -$headers = [ +$headers = [ 'Accept: application/json', 'Content-Type: application/json', 'Authorization: Bearer ' . $OPENAI_API_KEY @@ -54,7 +58,9 @@ 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_PROXY, "http://127.0.0.1:1081"); +if($configs["proxy"]){ + curl_setopt($ch, CURLOPT_PROXY, $configs["proxy"]); +} curl_exec($ch); @@ -71,10 +77,17 @@ foreach ($responsearr as $msg) { } } +// $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); +// curl_close($ch); + + $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"; $myfile = fopen(__DIR__ . "/chat.txt", "a") or die("Writing file failed."); fwrite($myfile, $filecontent); fclose($myfile); -curl_close($ch); +curl_close($ch); \ No newline at end of file