From 9878dcc914931cdeb6213c1c840546d21cc0581b Mon Sep 17 00:00:00 2001 From: chengxl0318 <1172937051@qq.com> Date: Thu, 27 Mar 2025 11:22:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=AE=A1=E6=A0=B8=E5=AF=B9?= =?UTF-8?q?=E6=8E=A5OPENAI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Aireview.php | 207 ++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 application/api/controller/Aireview.php diff --git a/application/api/controller/Aireview.php b/application/api/controller/Aireview.php new file mode 100644 index 0000000..f147d3b --- /dev/null +++ b/application/api/controller/Aireview.php @@ -0,0 +1,207 @@ +sApiKey = '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'; + $this->proxy = ''; + $this->sUrl = 'https://api.openai.com/v1/chat/completions'; + + parent::__construct($request); + } + + /** + * CURL 发送请求到 OpenAI + * @param $messages 内容 + * @param $model 模型类型 + */ + protected function curlOpenAI($messages, $model = 'gpt-4o'){ + + $sUrl = $this->sUrl; + + $data = [ + 'model' => $model, + 'messages' => $messages, + 'temperature' => 0.7 + ]; + + $this->curl = curl_init(); + + + // 通用配置 + curl_setopt($this->curl, CURLOPT_URL, $sUrl); + // 设置头信息 + curl_setopt($this->curl, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'Authorization: Bearer ' . $this->sApiKey + ]); + curl_setopt($this->curl, CURLOPT_PROXY,$this->proxy); + curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER,true); + curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST,2); + curl_setopt($this->curl, CURLOPT_POST, true); //设置为POST方式 + curl_setopt($this->curl, CURLOPT_POSTFIELDS,json_encode($data)); + curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE) ; // 获取数据返回 + // curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout); + + $result = curl_exec($this->curl); + + //请求失败 + if (curl_errno($this->curl)){ + $this->sError = curl_errno($this->curl); + curl_close($this->curl); + return FALSE; + } + $this->sResponesData = json_decode($result); + curl_close($this->curl); + return TRUE; + } + + /** + * @title AI审核文章 + * @param article_id 文章ID + * @param abstrart 摘要 + * @param keywords 关键词 + * @param model 接口模型 + * @param stream 是否流式输出 true是false否 + */ + public function review(){ + + + //获取参数 + $aParam = $this->request->post(); + if(empty($aParam['article_id'])){ + exit(json_encode(array('status' => 2,'msg' => 'Please select an article' ))); + } + if(empty($aParam['abstrart'])){ + exit(json_encode(array('status' => 2,'msg' => 'abstrart cannot be empty' ))); + } + if(empty($aParam['keywords'])){ + exit(json_encode(array('status' => 2,'msg' => 'keywords cannot be empty' ))); + } + + //查询文章 + $aArticle = Db::table('t_article')->field('article_id,abstrart,keywords,journal_id')->where('article_id',$aParam['article_id'])->find(); + if(empty($aArticle)){ + exit(json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' ))); + } + + //根据期刊ID查询期刊信息 + $aJournal = Db::table('t_journal')->field('zname')->where('journal_id',$aArticle['journal_id'])->find(); + if(empty($aJournal)){ + exit(json_encode(array('status' => 4,'msg' => 'This article is not associated with a journal' ))); + } + //组织参数 + $sContent = ''; + $sContent .= '摘要:'.$aParam['abstrart']; + $sContent .= '关键词:'.$aParam['keywords']; + $sContent .= '以上这篇文章是否符合'.$aJournal['zname'].'?是否具有科学前沿性和创新性?'; + $messages = [ + [ + 'role' => 'user', //角色:platform:平台;developer:开发者;user:用户;guideline:模型规范“指南”部分:连接:https://model-spec.openai.com/2025-02-12.html#chain_of_command + 'content' => $sContent + ] + ]; + //请求接口 + $sModel = $aParam['api_model'] ?? 'gpt-4o'; + $result = $this->curlOpenAI($messages,$sModel); + if($result == FALSE){ + exit(json_encode(array('status' => 4,'msg' => 'Interface request failed'.$this->sError))); + } + + //处理返回信息 + $data = $this->sResponesData; + if(!is_object($data)){ + exit(json_encode(array('status' => 5,'msg' => 'There is a misunderstanding in the data returned by the interface'))); + } + $data = object_to_array($data); + $aChoices = $data['choices'] ?? []; + if(empty($aChoices)){ + exit(json_encode(array('status' => 6,'msg' => 'OPENAI did not return data'))); + } + $aChoicesInfo = $aChoices[0] ?? []; + $aMessage = $aChoicesInfo['message'] ?? []; + if(empty($aMessage['content'])){ + exit(json_encode(array('status' => 7,'msg' => 'OPENAI returns empty data'))); + } + + //执行数据入库 + $param = []; + $param['content'] = addslashes($aMessage['content']); + $param['article_id'] = $aParam['article_id']; + $aResult = $this->addAiReview($param); + exit(json_encode($aResult)); + + + } + + /** + * @title AI审核内容入库 + * @param article_id 文章ID + * @param content 内容 + */ + protected function addAiReview($aParam = array()){ + + //返回数组 + $aResult = ['status' => 1,'msg' => 'AI review successful']; + //必填参数验证 + $aFields = ['article_id','content']; + $bStatus = true; + foreach($aFields as $val){ + if(empty($aParam[$val])){ + $aResult = ['status' => 2,'msg' => $val.'cannot be empty']; + $bStatus = false; + break; + } + } + if($bStatus == false){ + return $aResult; + } + //执行入库 + $aParam['create_time'] = date('Y-m-d H:i:s'); + if(!Db::name('article_ai_review')->insert($aParam)){ + $aResult = ['status' => 2,'msg' => 'Failed to add AI audit content']; + } + return $aResult; + } + + /** + * @title 文章AI审核内容查询 + * @param article_id 文章ID + */ + public function get(){ + + //获取参数 + $aParam = $this->request->post(); + if(empty($aParam['article_id'])){ + exit(json_encode(array('status' => 2,'msg' => 'Please select an article' ))); + } + + //查询文章 + $aArticle = Db::table('t_article')->field('article_id')->where('article_id',$aParam['article_id'])->find(); + if(empty($aArticle)){ + exit(json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' ))); + } + //查询文章审核内容 + $aAiReview = Db::table('t_article_ai_review')->field('content')->where('article_id',$aParam['article_id'])->find(); + exit(json_encode(array('status' => 1,'msg' => 'Successfully obtained article review content','data' => $aAiReview))); + } +}