增加记录日志功能

pull/35/head
dirk1983 4 years ago
parent cf95d3c8ce
commit d00dfdc4f8

@ -0,0 +1,4 @@
127.0.0.1 | 2023-03-08 10:37:47
Q:你好
A:你好!有什么我可以帮助你的吗?
----------------

@ -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);

Loading…
Cancel
Save