From 9408070ade141002f74e6b81c23556df3ccfd39c Mon Sep 17 00:00:00 2001 From: dirk1983 Date: Sun, 5 Mar 2023 22:30:00 +0800 Subject: [PATCH] =?UTF-8?q?stream=E6=A8=A1=E5=BC=8F=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E8=AF=A5=E6=96=87=E4=BB=B6=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- message.php | 59 ----------------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 message.php diff --git a/message.php b/message.php deleted file mode 100644 index 3ef3962..0000000 --- a/message.php +++ /dev/null @@ -1,59 +0,0 @@ - "gpt-3.5-turbo", - "messages" => [], -]; -if( !empty( $context ) ) { - $context = array_slice( $context, -5 ); - foreach( $context as $message ) { - $postData['messages'][] = ['role' => 'user', 'content' => str_replace("\n", "\\n", $message[0])]; - $postData['messages'][] = ['role' => 'assistant', 'content' => str_replace("\n", "\\n", $message[1])]; - } -} -$postData['messages'][] = ['role' => 'user', 'content' => $_POST['message']]; - -$ch = curl_init(); -$OPENAI_API_KEY = "sk-PXQ0A35RLCQaImgLujPST3blbkFJ2d7Kaa9aJjUqzvYwwkqd"; -$headers = [ - 'Accept: application/json', - 'Content-Type: application/json', - 'Authorization: Bearer ' . $OPENAI_API_KEY . '' -]; - -$postData = json_encode($postData); - -curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); -curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); -curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions'); -curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); -curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); -curl_setopt($ch, CURLOPT_POST, 1); -curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); - -$result = curl_exec($ch); -$complete = json_decode($result); - -if( isset( $complete->choices[0]->message->content ) ) { - $text = trim(str_replace( "\\n", "\n", $complete->choices[0]->message->content ),"\n"); -} elseif( isset( $complete->error->message ) ) { - $text = "服务器返回错误信息:".$complete->error->message; -} else { - $text = "服务器超时或返回异常消息。"; -} - -echo json_encode( [ - "message" => $text, - "raw_message" => $text, - "status" => "success", - ] ); - -$content2 = $_SERVER["REMOTE_ADDR"]." | ".date("Y-m-d H:i:s")."\n"; -$content2 .= "Q:".$_POST['message']."\nA:".$text."\n----------------\n"; -$myfile = fopen(__DIR__ . "/chat.txt", "a") or die("Writing file failed."); -fwrite($myfile, $content2); -fclose($myfile); -?>