From d00dfdc4f8dcd0d171846d99eccb0fe3713bf0b1 Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Wed, 8 Mar 2023 18:39:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=B0=E5=BD=95=E6=97=A5?= =?UTF-8?q?=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);