From 85c75735c45681937f7c2e3a3fc61d6d571716aa Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Mon, 6 Mar 2023 01:02:53 +0800 Subject: [PATCH 01/12] Update stream.php --- stream.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stream.php b/stream.php index 220e552..e4e509b 100644 --- a/stream.php +++ b/stream.php @@ -5,7 +5,7 @@ session_start(); $postData = $_SESSION['data']; $ch = curl_init(); $OPENAI_API_KEY = "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"; -if (isset($_SESSION['key'])) { +if ((isset($_SESSION['key'])) && (!empty($_POST['key']))) { $OPENAI_API_KEY = $_SESSION['key']; } $headers = [ From 9bf8f5c3b0928845ca2c93f41d1c946bd70fa29a Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Mon, 6 Mar 2023 01:03:27 +0800 Subject: [PATCH 02/12] Update setsession.php --- setsession.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setsession.php b/setsession.php index 6cdd4a1..684f962 100644 --- a/setsession.php +++ b/setsession.php @@ -17,7 +17,7 @@ $postData['messages'][] = ['role' => 'user', 'content' => $_POST['message']]; $postData = json_encode($postData); session_start(); $_SESSION['data'] = $postData; -if (isset($_POST['key'])) { +if ((isset($_POST['key'])) && (!empty($_POST['key']))) { $_SESSION['key'] = $_POST['key']; } echo '{"success":true}'; From 7e88942f4b2983b5ffc1593c05483bf0cd1bd0ae Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Mon, 6 Mar 2023 15:47:53 +0800 Subject: [PATCH 03/12] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 05e83b3..93d3bfb 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,10 @@ https://platform.openai.com/docs/api-reference/models/list 对chatgpt感兴趣的同学们欢迎加群讨论。群里有很多大神,有问题可以互相帮助。如果需要在本项目基础上进行二次开发或者其他商务合作,可以加我微信沟通。 -![微信截图_20230302202854](https://user-images.githubusercontent.com/5563148/222429139-f71c6bd3-8145-4038-9cd5-8654c7cf77c1.png) +由于群里人数已超过200,无法直接扫码进群,想进群的朋友可以加热心网友小号,由他帮忙拉进群。有自动拉人进群方案的朋友欢迎随时联系我,方便大家讨论。 + +![微信截图_20230306154434](https://user-images.githubusercontent.com/5563148/223048985-4cac05cb-acf0-4f04-aad5-1c3dcec609d0.png) + 有热心网友建议我放个打赏码,各位如果真的想表达感谢,小额即可。我更希望我的代码可以抛砖引玉,助力各位找到新的业务模式,一年盈利,两年融资,三年上市,走上人生巅峰! From cbaa1fe0744dca449fdb6c7b068f8a29c2a2ba0b Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Mon, 6 Mar 2023 15:51:23 +0800 Subject: [PATCH 04/12] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 93d3bfb..403696c 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ 很多人想要Demo网站中自己输入API-KEY的功能,已经把代码加上了,取消index.php的注释就行了。为了美观可以把上面的“连续对话”部分注释掉,要不然手机访问不是很友好。 -在国内访问OpenAI的新接口会提示超时,如果你本地有HTTP-PROXY,可以把stream.php里面注释掉的“curl_setopt($ch, CURLOPT_PROXY, " http://127.0.0.1:1081 ");”修改一下,这样就可以通过你本地的代理访问openai的接口。如果你自己没代理,可以使用热心网友提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://openai.1rmb.tk/v1/chat/completions '能用多久无法保证哦 +在国内访问OpenAI的新接口会提示超时,如果你本地有HTTP-PROXY,可以把stream.php里面注释掉的“curl_setopt($ch, CURLOPT_PROXY, " http://127.0.0.1:1081 ");”修改一下,这样就可以通过你本地的代理访问openai的接口。 +如果你自己没代理,可以使用热心网友提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://ca.mm1.ltd '能用多久无法保证哦 *测试网址:http://mm1.ltd* From 8f62b066757f0cc51a61d0b03aec59ec80c3b84d Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Mon, 6 Mar 2023 15:52:49 +0800 Subject: [PATCH 05/12] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 403696c..1845da5 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 很多人想要Demo网站中自己输入API-KEY的功能,已经把代码加上了,取消index.php的注释就行了。为了美观可以把上面的“连续对话”部分注释掉,要不然手机访问不是很友好。 在国内访问OpenAI的新接口会提示超时,如果你本地有HTTP-PROXY,可以把stream.php里面注释掉的“curl_setopt($ch, CURLOPT_PROXY, " http://127.0.0.1:1081 ");”修改一下,这样就可以通过你本地的代理访问openai的接口。 -如果你自己没代理,可以使用热心网友提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://ca.mm1.ltd '能用多久无法保证哦 +如果你自己没代理,可以使用我提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://ca.mm1.ltd '网速有限最好自建哦。 *测试网址:http://mm1.ltd* From ddbf61e0c16177bb6700e21bf134d5544a0aa977 Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Mon, 6 Mar 2023 15:53:11 +0800 Subject: [PATCH 06/12] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1845da5..2800fc7 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 很多人想要Demo网站中自己输入API-KEY的功能,已经把代码加上了,取消index.php的注释就行了。为了美观可以把上面的“连续对话”部分注释掉,要不然手机访问不是很友好。 在国内访问OpenAI的新接口会提示超时,如果你本地有HTTP-PROXY,可以把stream.php里面注释掉的“curl_setopt($ch, CURLOPT_PROXY, " http://127.0.0.1:1081 ");”修改一下,这样就可以通过你本地的代理访问openai的接口。 -如果你自己没代理,可以使用我提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://ca.mm1.ltd '网速有限最好自建哦。 +如果你自己没代理,可以使用我提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://ca.mm1.ltd '网速有限最好还是自建哦。 *测试网址:http://mm1.ltd* From 00cd9abc3b4b3afbd2e29435d1a3fcba5b943b08 Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Tue, 7 Mar 2023 09:25:42 +0800 Subject: [PATCH 07/12] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2800fc7..64409bd 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 很多人想要Demo网站中自己输入API-KEY的功能,已经把代码加上了,取消index.php的注释就行了。为了美观可以把上面的“连续对话”部分注释掉,要不然手机访问不是很友好。 在国内访问OpenAI的新接口会提示超时,如果你本地有HTTP-PROXY,可以把stream.php里面注释掉的“curl_setopt($ch, CURLOPT_PROXY, " http://127.0.0.1:1081 ");”修改一下,这样就可以通过你本地的代理访问openai的接口。 -如果你自己没代理,可以使用我提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://ca.mm1.ltd '网速有限最好还是自建哦。 +如果你自己没代理,可以使用热心网友提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://openai.ahao-study.top/ ',不确定那个什么时候会失效,也可以进群再找其他群友求一个。 *测试网址:http://mm1.ltd* From cf95d3c8cece385f1258bdc27e04713e228949cb Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Tue, 7 Mar 2023 09:37:40 +0800 Subject: [PATCH 08/12] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 64409bd..d042a68 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 很多人想要Demo网站中自己输入API-KEY的功能,已经把代码加上了,取消index.php的注释就行了。为了美观可以把上面的“连续对话”部分注释掉,要不然手机访问不是很友好。 在国内访问OpenAI的新接口会提示超时,如果你本地有HTTP-PROXY,可以把stream.php里面注释掉的“curl_setopt($ch, CURLOPT_PROXY, " http://127.0.0.1:1081 ");”修改一下,这样就可以通过你本地的代理访问openai的接口。 -如果你自己没代理,可以使用热心网友提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://openai.ahao-study.top/ ',不确定那个什么时候会失效,也可以进群再找其他群友求一个。 +如果你自己没代理,可以使用热心网友提供的反代地址,把“curl_setopt($ch, CURLOPT_URL, ' https://api.openai.com/v1/chat/completions ');”这行里面的网址改成' https://openai.ahao-study.top/ '或' https://openai.1rmb.tk/v1/chat/completions ',不确定那个什么时候会失效,也可以进群再找其他群友求一个。 *测试网址:http://mm1.ltd* From d00dfdc4f8dcd0d171846d99eccb0fe3713bf0b1 Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Wed, 8 Mar 2023 18:39:12 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chat.txt | 4 ++++ stream.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 chat.txt diff --git a/chat.txt b/chat.txt new file mode 100644 index 0000000..7b0db87 --- /dev/null +++ b/chat.txt @@ -0,0 +1,4 @@ +127.0.0.1 | 2023-03-08 10:37:47 +Q:你好 +A:你好!有什么我可以帮助你的吗? +---------------- diff --git a/stream.php b/stream.php index e4e509b..9fd5ef2 100644 --- a/stream.php +++ b/stream.php @@ -3,6 +3,7 @@ header("Access-Control-Allow-Origin: *"); header("Content-Type: text/event-stream"); session_start(); $postData = $_SESSION['data']; +$_SESSION['response'] = ""; $ch = curl_init(); $OPENAI_API_KEY = "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"; if ((isset($_SESSION['key'])) && (!empty($_POST['key']))) { @@ -27,6 +28,7 @@ $callback = function ($ch, $data) { } } else { echo $data; + $_SESSION['response'] .= $data; } return strlen($data); }; @@ -42,4 +44,21 @@ curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); //curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:1081"); curl_exec($ch); + +$answer = ""; +$responsearr = explode("data: ", $_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"; +$myfile = fopen(__DIR__ . "/chat.txt", "a") or die("Writing file failed."); +fwrite($myfile, $filecontent); +fclose($myfile); curl_close($ch); From ac007ea07bf477a57dcab881c4318f13da7e6955 Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Wed, 8 Mar 2023 21:41:35 +0800 Subject: [PATCH 10/12] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d042a68..ad2f203 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # chatgpt ------ **PHP版调用OpenAI的API接口进行问答的Demo,代码已更新为调用最新的gpt-3.5-turbo模型。 -采用Stream流模式通信,一边生成一边输出,响应速度超过官网。前端采用JS的EventSource,还将Markdown格式文本进行了排版,对代码进行了着色处理。** +采用Stream流模式通信,一边生成一边输出,响应速度超过官网。前端采用JS的EventSource,还将Markdown格式文本进行了排版,对代码进行了着色处理。服务器记录所有访问者的对话日志。** 很多人想要Demo网站中自己输入API-KEY的功能,已经把代码加上了,取消index.php的注释就行了。为了美观可以把上面的“连续对话”部分注释掉,要不然手机访问不是很友好。 From eb681ac62eea8a372462b3de49fbd7d273be59f6 Mon Sep 17 00:00:00 2001 From: Zoumachuan <123653723+Zoumachuan@users.noreply.github.com> Date: Thu, 9 Mar 2023 21:07:01 +0800 Subject: [PATCH 11/12] Add files via upload --- index.php | 246 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 179 insertions(+), 67 deletions(-) diff --git a/index.php b/index.php index f187771..f06b02d 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,4 @@ - - - - - ChatGPT<?= $type ?>专用版 - - - + + + + ChatGPT<?= $type ?>专用版 + + + -
-
- -
-
-
-
-
-
- 采用最新的gpt-3.5-turbo模型,stream模式,秒回 -
-
-
-   连续对话: - - -     默认开启 -
- -
-
    -
-
-
-
-
-
-
- -
-
-

发送

-
-
-
-
-
-
-
-
- - - - - - +
+
+ +
+
+
+
+
+
+ 采用最新的gpt-3.5-turbo模型,stream模式,秒回 +
+
+
+   连续对话: + + +     默认开启 +
+
+   选择预设话术: + +
+
+
    +
+
+
+
+
+
+
+ +
+
+

发送

+
+
+
+
+
+
+
+
+ + + + + + - \ No newline at end of file + From af0ec321231c4021d10d509ce4c7ee865654fa31 Mon Sep 17 00:00:00 2001 From: Zoumachuan <123653723+Zoumachuan@users.noreply.github.com> Date: Thu, 9 Mar 2023 21:18:56 +0800 Subject: [PATCH 12/12] Add files via upload --- index.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.php b/index.php index f06b02d..1791335 100644 --- a/index.php +++ b/index.php @@ -205,6 +205,15 @@ $type = "个人"; + +

+ +