From 62faa9326da05958d4d15e1a3bd918c646bbe39d Mon Sep 17 00:00:00 2001 From: wangjinlei <751475802@qq.com> Date: Thu, 6 Mar 2025 09:16:11 +0800 Subject: [PATCH] 1 --- application/api/controller/Aigpt.php | 76 +++++++++++++++++++++++- application/api/controller/Preaccept.php | 25 +++++--- 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/application/api/controller/Aigpt.php b/application/api/controller/Aigpt.php index b1ead3a..00f1bbf 100644 --- a/application/api/controller/Aigpt.php +++ b/application/api/controller/Aigpt.php @@ -4,6 +4,8 @@ namespace app\api\controller; use GuzzleHttp\Client; use Think\Env; +use Tectalic\OpenAi\Authentication; +use Tectalic\OpenAi\Manager; /** * @title 公共管理相关 @@ -17,7 +19,7 @@ class Aigpt extends Base { } public function myTestGpt(){ - $apiKey = Env::get("gpt.api_key");//"你的API密钥"; // 请替换为你的 OpenAI API Key + $apiKey = Env::get("gpt.api_key1");//"你的API密钥"; // 请替换为你的 OpenAI API Key $url = "https://api.openai.com/v1/chat/completions"; $client = new Client(); @@ -33,7 +35,77 @@ class Aigpt extends Base { ] ]); - return json($response->getBody()->getContents()); + $res = object_to_array(json_decode($response->getBody()->getContents())); + + return jsonSuccess($res); + } + + + + public function myTestGpt1(){ + $openaiClient = Manager::build( + new \GuzzleHttp\Client(), + new Authentication(Env::get("gpt.api_key1")) + ); + + $response = $openaiClient->chatCompletions()->create( + new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequest([ + 'model' => 'gpt-4', + 'messages' => [ + [ + 'role' => 'user', + 'content' => 'Will using a well designed and supported third party package save time?用中文回答' + ], + ], + ]) + )->toModel(); + + echo $response->choices[0]->message->content; + } + + + + + + public function myTestGpt2(){ + header('Access-Control-Allow-Origin: *'); // 允许任何来源 + header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); + header('Access-Control-Allow-Headers: Content-Type'); + + header('Content-Type: text/event-stream'); + header('Cache-Control: no-cache'); + header('Connection: keep-alive'); + +// $request = Request::instance(); + $data = $this->request->post(); + $input = $data['message']; +// $input = $this->request->post('message'); // 获取前端传递的用户输入 + + $client = new Client(); + $response = $client->post('https://api.openai.com/v1/chat/completions', [ + 'headers' => [ + 'Authorization' => 'Bearer '.Env::get("gpt.api_key1"), + 'Content-Type' => 'application/json', + ], + 'json' => [ + 'model' => 'gpt-4', + 'messages' => [['role' => 'user', 'content' => $input]], + 'stream' => true, + ], + 'stream' => true + ]); + + $body = $response->getBody(); + + while (!$body->eof()) { + $chunk = $body->read(1024); // 读取流数据 + echo "data: " . json_encode($chunk, JSON_UNESCAPED_UNICODE) . "\n\n"; + ob_flush(); + flush(); + } + + exit; + } diff --git a/application/api/controller/Preaccept.php b/application/api/controller/Preaccept.php index 2b68c77..03ec32b 100644 --- a/application/api/controller/Preaccept.php +++ b/application/api/controller/Preaccept.php @@ -905,17 +905,28 @@ class Preaccept extends Base if(!$rule->check($data)){ return jsonError($rule->getError()); } - - - - - - $this->article_main_obj->where("am_id",$data['am_id'])->update(['state'=>1]); - return jsonSuccess([]); } + + /**批量删除正文的行 + * @return void + */ + public function delMoreArticleMains(){ + $data = $this->request->post(); + $rule = new Validate([ + "ids"=>"require" + ]); + if(!$rule->check($data)){ + return jsonError($rule->getError()); + } + $idList = explode(",",$data['ids']); + $this->article_main_obj->whereIn("am_id",$idList)->update(['state'=>1]); + return jsonSuccess([]); + } + + public function editArticleMainsForAuthor(){ $data = $this->request->post(); $rule = new Validate([