diff --git a/README.md b/README.md index 05e83b3..ad2f203 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,12 @@ # chatgpt ------ **PHP版调用OpenAI的API接口进行问答的Demo,代码已更新为调用最新的gpt-3.5-turbo模型。 -采用Stream流模式通信,一边生成一边输出,响应速度超过官网。前端采用JS的EventSource,还将Markdown格式文本进行了排版,对代码进行了着色处理。** +采用Stream流模式通信,一边生成一边输出,响应速度超过官网。前端采用JS的EventSource,还将Markdown格式文本进行了排版,对代码进行了着色处理。服务器记录所有访问者的对话日志。** 很多人想要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://openai.ahao-study.top/ '或' https://openai.1rmb.tk/v1/chat/completions ',不确定那个什么时候会失效,也可以进群再找其他群友求一个。 *测试网址:http://mm1.ltd* @@ -39,7 +40,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) + 有热心网友建议我放个打赏码,各位如果真的想表达感谢,小额即可。我更希望我的代码可以抛砖引玉,助力各位找到新的业务模式,一年盈利,两年融资,三年上市,走上人生巅峰! 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/index.php b/index.php index f187771..1791335 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 + 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}'; diff --git a/stream.php b/stream.php index 220e552..9fd5ef2 100644 --- a/stream.php +++ b/stream.php @@ -3,9 +3,10 @@ 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'])) { +if ((isset($_SESSION['key'])) && (!empty($_POST['key']))) { $OPENAI_API_KEY = $_SESSION['key']; } $headers = [ @@ -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);